ast: rename IfBranch to Block, add LoopStmt

This commit is contained in:
Luna 2019-08-25 16:57:22 -03:00
parent 460055e186
commit 19b79514b4
2 changed files with 43 additions and 20 deletions

View file

@ -97,12 +97,17 @@ pub const Expr = union(ExprType) {
Grouping: *Expr,
};
pub const IfBranch = std.ArrayList(*Stmt);
pub const Block = std.ArrayList(*Stmt);
pub const IfStmt = struct {
condition: *Expr,
then_branch: IfBranch,
else_branch: ?IfBranch,
then_branch: Block,
else_branch: ?Block,
};
pub const LoopStmt = struct {
condition: ?*Expr,
then_branch: Block,
};
pub const Stmt = union(enum) {
@ -110,28 +115,46 @@ pub const Stmt = union(enum) {
Println: *Expr,
If: IfStmt,
Loop: LoopStmt,
pub fn mkPrintln(allocator: *std.mem.Allocator, expr: *Expr) !*Stmt {
var println = try allocator.create(Stmt);
println.* = Stmt{ .Println = expr };
return println;
var stmt = try allocator.create(Stmt);
stmt.* = Stmt{ .Println = expr };
return stmt;
}
pub fn mkIfStmt(
allocator: *std.mem.Allocator,
condition: *Expr,
then: IfBranch,
else_branch: ?IfBranch,
then: Block,
else_branch: ?Block,
) !*Stmt {
var println = try allocator.create(Stmt);
println.* = Stmt{
var stmt = try allocator.create(Stmt);
stmt.* = Stmt{
.If = IfStmt{
.condition = condition,
.then_branch = then,
.else_branch = else_branch,
},
};
return println;
return stmt;
}
pub fn mkLoop(
allocator: *std.mem.Allocator,
condition: ?*Expr,
then: Block,
) !*Stmt {
var stmt = try allocator.create(Stmt);
stmt.* = Stmt{
.Loop = LoopStmt{
.condition = condition,
.then_branch = then,
},
};
return stmt;
}
};

View file

@ -456,35 +456,35 @@ pub const Parser = struct {
};
}
/// Copy of parseBlock for if branches
fn parseIfBranch(self: *@This()) !ast.IfBranch {
var branch = ast.IfBranch.init(self.allocator);
errdefer branch.deinit();
/// Copy of parseBlock for blocks in statements
fn parseStmtBlock(self: *@This()) !ast.Block {
var block = ast.Block.init(self.allocator);
errdefer block.deinit();
_ = try self.consumeSingle(.LeftBrace);
while (self.peek().ttype != .RightBrace) {
var stmt = try self.parseDecl();
ast.printNode(try self.mkStmt(stmt), 0);
try branch.append(stmt);
try block.append(stmt);
}
_ = try self.consumeSingle(.RightBrace);
return branch;
return block;
}
fn parseIfStmt(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.If);
var condition = (try self.parseExpr()).Expr;
const then_branch = try self.parseIfBranch();
const then_branch = try self.parseStmtBlock();
var else_branch: ?ast.IfBranch = null;
var else_branch: ?ast.Block = null;
if (self.check(.Else)) {
_ = try self.consumeSingle(.Else);
else_branch = try self.parseIfBranch();
else_branch = try self.parseStmtBlock();
}
return try Stmt.mkIfStmt(