feat: begin parsing

This commit is contained in:
Araozu 2024-12-09 08:15:00 -05:00
parent 0ff3e94620
commit 095aebcad5
2 changed files with 53 additions and 2 deletions

View File

@ -10,8 +10,8 @@ const string = @import("string.zig");
const grouping = @import("grouping.zig"); const grouping = @import("grouping.zig");
const punctuation = @import("punctiation.zig"); const punctuation = @import("punctiation.zig");
const TokenType = token.TokenType; pub const TokenType = token.TokenType;
const Token = token.Token; pub const Token = token.Token;
/// Creates an array list of tokens. The caller is responsible of /// Creates an array list of tokens. The caller is responsible of
/// calling `deinit` to free the array list /// calling `deinit` to free the array list

View File

@ -1,5 +1,56 @@
const std = @import("std"); const std = @import("std");
const lexic = @import("lexic");
const Token = lexic.Token;
const TokenType = lexic.TokenType;
const TokenStream = std.ArrayList(Token);
const ParseError = error{
Unmatched,
Error,
};
const Statement = union(enum) {
VariableBinding: u8,
};
const VariableBinding = struct {
is_mutable: bool,
datatype: ?*Token,
identifier: *Token,
expression: Expression,
fn parse() !@This() {}
};
const Expression = union(enum) {
number: *const Token,
/// Attempts to parse an expression from a token stream.
fn parse(tokens: *const TokenStream, pos: usize) ParseError!@This() {
std.debug.assert(pos < tokens.items.len);
const t = tokens.items[pos];
if (t.token_type != TokenType.Int) {
return ParseError.Unmatched;
}
return .{
.number = &t,
};
}
};
test { test {
std.testing.refAllDecls(@This()); std.testing.refAllDecls(@This());
} }
test "should parse expression" {
const input = "322";
const tokens = try lexic.tokenize(input, std.testing.allocator);
defer tokens.deinit();
const expr = try Expression.parse(&tokens, 0);
try std.testing.expectEqualDeep("322", expr.number.value);
try std.testing.expectEqualDeep(TokenType.Int, expr.number.token_type);
}