Rename statement -> ControlBlock

This commit is contained in:
jaina heartles 2022-11-18 01:59:27 -08:00
parent 1a7db9a834
commit 52bb8739f8

View file

@ -26,7 +26,7 @@ fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args:
}; };
} }
fn executeStatement(writer: anytype, comptime stmt: ExecutableStatement, args: anytype, captures: anytype) !void { fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void {
switch (stmt) { switch (stmt) {
.expression => |expr| { .expression => |expr| {
const val = evaluateExpression(expr, args, captures); const val = evaluateExpression(expr, args, captures);
@ -142,9 +142,9 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
items = items ++ [_]TemplateItem{.{ .text = current_text }}; items = items ++ [_]TemplateItem{.{ .text = current_text }};
current_text = ""; current_text = "";
} }
const result = parseStatement(iter); const result = parseControlBlock(iter);
iter = result.new_iter; iter = result.new_iter;
const stmt = result.item.executable; const stmt = result.item.statement;
if (stmt == .end_for) { if (stmt == .end_for) {
if (template_type == .for_block) break :parse_loop else @compileError("Unexpected end statement"); if (template_type == .for_block) break :parse_loop else @compileError("Unexpected end statement");
} else if (stmt == .end_if) { } else if (stmt == .end_if) {
@ -203,12 +203,12 @@ fn parseExpression(comptime tokens: TokenIter) ParseResult(Expression) {
} }
} }
fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) { fn parseControlBlock(comptime tokens: TokenIter) ParseResult(ControlBlock) {
comptime { comptime {
var iter = tokens; var iter = tokens;
var first_token: bool = true; var first_token: bool = true;
var strip_before: bool = false; var strip_before: bool = false;
var stmt: ExecutableStatement = while (iter.next()) |token| { var stmt: Statement = while (iter.next()) |token| {
defer first_token = false; defer first_token = false;
switch (token) { switch (token) {
.equals => { .equals => {
@ -230,7 +230,7 @@ fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
return .{ return .{
.new_iter = result.new_iter, .new_iter = result.new_iter,
.item = .{ .item = .{
.executable = .{ .for_loop = result.item }, .statement = .{ .for_loop = result.item },
.strip_before = false, .strip_before = false,
.strip_after = false, .strip_after = false,
}, },
@ -241,7 +241,7 @@ fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
return .{ return .{
.new_iter = result.new_iter, .new_iter = result.new_iter,
.item = .{ .item = .{
.executable = .{ .if_statement = result.item }, .statement = .{ .if_statement = result.item },
.strip_before = false, .strip_before = false,
.strip_after = false, .strip_after = false,
}, },
@ -288,7 +288,7 @@ fn parseStatement(comptime tokens: TokenIter) ParseResult(Statement) {
.close_bracket => return .{ .close_bracket => return .{
.new_iter = iter, .new_iter = iter,
.item = .{ .item = .{
.executable = stmt, .statement = stmt,
.strip_before = strip_before, .strip_before = strip_before,
.strip_after = strip_after, .strip_after = strip_after,
}, },
@ -315,7 +315,7 @@ fn skipWhitespace(comptime tokens: TokenIter) TokenIter {
} }
} }
fn endStatement(comptime tokens: TokenIter) TokenIter { fn endControlBlock(comptime tokens: TokenIter) TokenIter {
comptime { comptime {
var iter = skipWhitespace(tokens); var iter = skipWhitespace(tokens);
@ -348,7 +348,7 @@ fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) {
const token = iter.next() orelse @compileError("Unexpected end of template"); const token = iter.next() orelse @compileError("Unexpected end of template");
if (token != .pipe) @compileError("Unexpected token"); if (token != .pipe) @compileError("Unexpected token");
} }
iter = endStatement(iter); iter = endControlBlock(iter);
const subtemplate = parseTemplate(iter, .for_block); const subtemplate = parseTemplate(iter, .for_block);
@ -363,7 +363,7 @@ fn parseForLoop(comptime tokens: TokenIter) ParseResult(ForLoop) {
fn parseIfStatement(comptime tokens: TokenIter) ParseResult(IfStatement) { fn parseIfStatement(comptime tokens: TokenIter) ParseResult(IfStatement) {
comptime { comptime {
const condition = parseExpression(tokens); const condition = parseExpression(tokens);
var iter = endStatement(condition.new_iter); var iter = endControlBlock(condition.new_iter);
const subtemplate = parseTemplate(iter, .if_block); const subtemplate = parseTemplate(iter, .if_block);
@ -407,7 +407,7 @@ fn ParseResult(comptime T: type) type {
const TemplateItem = union(enum) { const TemplateItem = union(enum) {
text: []const u8, text: []const u8,
statement: ExecutableStatement, statement: Statement,
}; };
const Expression = union(enum) { const Expression = union(enum) {
@ -426,8 +426,7 @@ const IfStatement = struct {
condition: Expression, condition: Expression,
}; };
const ExecutableStatement = const Statement = union(enum) {
union(enum) {
expression: Expression, expression: Expression,
for_loop: ForLoop, for_loop: ForLoop,
end_for: void, end_for: void,
@ -435,8 +434,8 @@ const ExecutableStatement =
end_if: void, end_if: void,
}; };
const Statement = struct { const ControlBlock = struct {
executable: ExecutableStatement, statement: Statement,
strip_before: bool, strip_before: bool,
strip_after: bool, strip_after: bool,
}; };