Create ParseResult

This commit is contained in:
jaina heartles 2022-11-15 22:11:16 -08:00
parent 7191ad8f27
commit d24957b7a0

View file

@ -20,7 +20,7 @@ pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !v
_ = comptime iter.next(); _ = comptime iter.next();
} else { } else {
const result = comptime parseStatement(iter); const result = comptime parseStatement(iter);
try executeStatement(writer, result.statement, args); try executeStatement(writer, result.item, args);
iter = result.new_iter; iter = result.new_iter;
} }
}, },
@ -54,7 +54,7 @@ fn argDeref(writer: anytype, comptime names: []const []const u8, arg: anytype) !
return argDeref(writer, names[1..], @field(arg, names[0])); return argDeref(writer, names[1..], @field(arg, names[0]));
} }
fn parseStatement(comptime tokens: TokenIter) StatementResult { fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
comptime { comptime {
var iter = tokens; var iter = tokens;
while (iter.next()) |token| switch (token) { while (iter.next()) |token| switch (token) {
@ -77,8 +77,8 @@ fn parseStatement(comptime tokens: TokenIter) StatementResult {
.close_bracket => break, .close_bracket => break,
else => @compileError("TODO"), else => @compileError("TODO"),
}; };
return .{ .new_iter = iter, .statement = .{ return .{ .new_iter = iter, .item = .{
.expression = expr.expression, .expression = .{ .arg_deref = expr.item },
} }; } };
}, },
else => @compileError(""), else => @compileError(""),
@ -88,7 +88,7 @@ fn parseStatement(comptime tokens: TokenIter) StatementResult {
} }
} }
fn parseArgDeref(comptime tokens: TokenIter) ExpressionResult { fn parseArgDeref(comptime tokens: TokenIter) ParseResult([]const []const u8) {
comptime { comptime {
var iter = tokens; var iter = tokens;
var fields: []const []const u8 = &.{}; var fields: []const []const u8 = &.{};
@ -107,7 +107,7 @@ fn parseArgDeref(comptime tokens: TokenIter) ExpressionResult {
}, },
else => if (wants == .period) return .{ else => if (wants == .period) return .{
.new_iter = iter, .new_iter = iter,
.expression = .{ .arg_deref = fields }, .item = fields,
} else @compileError("Unexpected token"), } else @compileError("Unexpected token"),
} }
_ = iter.next(); _ = iter.next();
@ -115,15 +115,17 @@ fn parseArgDeref(comptime tokens: TokenIter) ExpressionResult {
} }
} }
fn ParseResult(comptime T: type) type {
return struct {
new_iter: TokenIter,
item: T,
};
}
const Expression = union(enum) { const Expression = union(enum) {
arg_deref: []const []const u8, arg_deref: []const []const u8,
}; };
const ExpressionResult = struct {
new_iter: TokenIter,
expression: Expression,
};
const Statement = union(enum) { const Statement = union(enum) {
expression: Expression, expression: Expression,
for_loop: struct { for_loop: struct {
@ -134,11 +136,6 @@ const Statement = union(enum) {
}, },
}; };
const StatementResult = struct {
new_iter: TokenIter,
statement: Statement,
};
const State = enum { const State = enum {
text, text,
text_close_bracket, text_close_bracket,