diff --git a/src/02_syntax/expression.zig b/src/02_syntax/expression.zig index f5aa43e..03a1be4 100644 --- a/src/02_syntax/expression.zig +++ b/src/02_syntax/expression.zig @@ -10,8 +10,9 @@ pub const Expression = union(enum) { /// Attempts to parse an expression from a token stream. /// - /// Receives a pointer to the memory for initialization - pub fn init(self: *@This(), tokens: *const std.ArrayList(Token), pos: usize) ?void { + /// Receives a pointer to the memory for initialization, + /// returns the position of the next token + pub fn init(self: *@This(), tokens: *const std.ArrayList(Token), pos: usize) ?usize { std.debug.assert(pos < tokens.items.len); const t = tokens.items[pos]; @@ -22,6 +23,7 @@ pub const Expression = union(enum) { self.* = .{ .number = &t, }; + return pos + 1; } }; diff --git a/src/02_syntax/variable.zig b/src/02_syntax/variable.zig index 8265f0d..f401679 100644 --- a/src/02_syntax/variable.zig +++ b/src/02_syntax/variable.zig @@ -33,11 +33,13 @@ pub const VariableBinding = struct { // check there is still input if (pos + 1 >= tokens.items.len) { // return error + // TODO: populate error information return ParseError.Error; } // try to parse an identifier const identifier = if (utils.expect_token_type(lexic.TokenType.Identifier, &tokens.items[pos + 1])) |i| i else { + // TODO: populate error information return ParseError.Error; }; @@ -51,14 +53,12 @@ pub const VariableBinding = struct { // parse expression if (pos + 3 >= tokens.items.len) return ParseError.Error; - const exp = allocator.create(expression.Expression) catch { - return ParseError.OutOfMemory; - }; + const exp = try allocator.create(expression.Expression); errdefer allocator.destroy(exp); - const res = exp.init(tokens, pos + 3); - if (res == null) { + const next_pos = if (exp.init(tokens, pos + 3)) |x| x else { + // TODO: populate error information return ParseError.Error; - } + }; // return target.* = .{ @@ -68,8 +68,7 @@ pub const VariableBinding = struct { .expression = exp, .alloc = allocator, }; - // TODO: when expression parses more than one token this will break. - return pos + 4; + return next_pos; } pub fn deinit(self: @This()) void {