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();
} else {
const result = comptime parseStatement(iter);
try executeStatement(writer, result.statement, args);
try executeStatement(writer, result.item, args);
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]));
}
fn parseStatement(comptime tokens: TokenIter) StatementResult {
fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
comptime {
var iter = tokens;
while (iter.next()) |token| switch (token) {
@ -77,8 +77,8 @@ fn parseStatement(comptime tokens: TokenIter) StatementResult {
.close_bracket => break,
else => @compileError("TODO"),
};
return .{ .new_iter = iter, .statement = .{
.expression = expr.expression,
return .{ .new_iter = iter, .item = .{
.expression = .{ .arg_deref = expr.item },
} };
},
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 {
var iter = tokens;
var fields: []const []const u8 = &.{};
@ -107,7 +107,7 @@ fn parseArgDeref(comptime tokens: TokenIter) ExpressionResult {
},
else => if (wants == .period) return .{
.new_iter = iter,
.expression = .{ .arg_deref = fields },
.item = fields,
} else @compileError("Unexpected token"),
}
_ = 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) {
arg_deref: []const []const u8,
};
const ExpressionResult = struct {
new_iter: TokenIter,
expression: Expression,
};
const Statement = union(enum) {
expression: Expression,
for_loop: struct {
@ -134,11 +136,6 @@ const Statement = union(enum) {
},
};
const StatementResult = struct {
new_iter: TokenIter,
statement: Statement,
};
const State = enum {
text,
text_close_bracket,