add loop parsing

This commit is contained in:
Luna 2019-08-25 17:14:25 -03:00
parent 19b79514b4
commit 4f0c43865f
3 changed files with 41 additions and 0 deletions

View File

@ -20,4 +20,14 @@ fn main(a int) int {
a && b
a || b
loop {}
loop {
println('Ballse')
}
loop a > 2 {}
loop a > 2 {
println('skirts')
}
}

View File

@ -327,6 +327,20 @@ pub fn printStmt(ident: usize, stmt: *Stmt) void {
std.debug.warn(")\n");
},
.Loop => |loop| {
std.debug.warn("(loop ");
if (loop.condition) |cond| {
printExpr(cond);
} else {
std.debug.warn("true");
}
std.debug.warn(" ");
printBlock(ident + 1, loop.then_branch, false);
std.debug.warn(")\n");
},
else => std.debug.warn("UnknownStmt-{}", @tagName(stmt.*)),
}
}

View File

@ -449,6 +449,7 @@ pub const Parser = struct {
fn parseStmt(self: *@This()) anyerror!*Stmt {
return switch (self.peek().ttype) {
.If => try self.parseIfStmt(),
.Loop => try self.parseLoop(),
.Println => try self.parsePrintln(),
// TODO make newlines tokens and consume newline?
@ -495,6 +496,22 @@ pub const Parser = struct {
);
}
fn parseLoop(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Loop);
var expr: ?*Expr = null;
var body: ast.Block = undefined;
// infinite loop
if (self.check(.LeftBrace)) {
body = try self.parseStmtBlock();
} else {
expr = (try self.parseExpr()).Expr;
body = try self.parseStmtBlock();
}
return try Stmt.mkLoop(self.allocator, expr, body);
}
fn parsePrintln(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Println);