vardecl: add VarDecl statement and Variable Expr
- ast: simplify stmt printing from printNode - parser: create variables on parsePrimary
This commit is contained in:
parent
02c2055601
commit
d93adbbd3e
2 changed files with 27 additions and 15 deletions
34
src/ast.zig
34
src/ast.zig
|
@ -58,18 +58,31 @@ pub const Expr = union(enum) {
|
|||
Binary: BinaryExpr,
|
||||
Unary: UnaryExpr,
|
||||
Literal: LiteralExpr,
|
||||
Variable: Token,
|
||||
Grouping: *Expr,
|
||||
};
|
||||
|
||||
pub const VarDecl = struct {
|
||||
name: Token,
|
||||
initializer: *Expr,
|
||||
};
|
||||
|
||||
pub const Stmt = union(enum) {
|
||||
Expr: *Expr,
|
||||
Println: *Expr,
|
||||
VarDecl: VarDecl,
|
||||
|
||||
pub fn mkPrintln(allocator: *std.mem.Allocator, expr: *Expr) !*Stmt {
|
||||
var println = try allocator.create(Stmt);
|
||||
println.* = Stmt{ .Println = expr };
|
||||
return println;
|
||||
}
|
||||
|
||||
pub fn mkVarDecl(allocator: *std.mem.Allocator, name: Token, init: *Expr) !*Stmt {
|
||||
var vardecl = try allocator.create(Stmt);
|
||||
vardecl.* = Stmt{ .VarDecl = VarDecl{ .name = name, .initializer = init } };
|
||||
return vardecl;
|
||||
}
|
||||
};
|
||||
|
||||
pub const Node = union(NodeType) {
|
||||
|
@ -149,20 +162,9 @@ pub fn printNode(node: *Node, ident: usize) void {
|
|||
},
|
||||
|
||||
.Stmt => |stmt| blk: {
|
||||
switch (stmt.*) {
|
||||
.Println => |expr| blk2: {
|
||||
printIdent(ident);
|
||||
std.debug.warn("println(");
|
||||
printExpr(expr);
|
||||
std.debug.warn("\n");
|
||||
},
|
||||
|
||||
.Expr => |expr| {
|
||||
printIdent(ident);
|
||||
printExpr(expr);
|
||||
std.debug.warn("\n");
|
||||
},
|
||||
}
|
||||
printIdent(ident);
|
||||
printStmt(stmt);
|
||||
std.debug.warn("\n");
|
||||
},
|
||||
|
||||
else => {
|
||||
|
@ -197,7 +199,7 @@ pub fn printExpr(expr: *Expr) void {
|
|||
}
|
||||
},
|
||||
|
||||
else => std.debug.warn("unknown"),
|
||||
else => std.debug.warn("EXPR_UNKNOWN"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -205,5 +207,7 @@ pub fn printStmt(stmt: *Stmt) void {
|
|||
switch (stmt.*) {
|
||||
.Println => |expr| parenthetize("println", &[_]*Expr{expr}),
|
||||
.Expr => |expr| printExpr(expr),
|
||||
|
||||
else => std.debug.warn("STMT_UNKNOWN"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,6 +240,13 @@ pub const Parser = struct {
|
|||
return expr;
|
||||
}
|
||||
|
||||
fn mkVariable(self: *Parser, variable: Token) !*ast.Expr {
|
||||
var expr = try self.allocator.create(Expr);
|
||||
expr.* = Expr{ .Variable = variable };
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
pub fn parse(self: *Parser) !*ast.Node {
|
||||
self.tokens = try self.allocator.alloc(Token, 0);
|
||||
var root = try ast.mkRoot(self.allocator);
|
||||
|
@ -511,6 +518,7 @@ pub const Parser = struct {
|
|||
.Integer => try self.mkInteger(lexeme),
|
||||
.Float => try self.mkFloat(lexeme),
|
||||
.String => try self.mkString(lexeme),
|
||||
.Identifier => try self.mkVariable(self.peek()),
|
||||
|
||||
.LeftParen => blk: {
|
||||
_ = try self.nextToken();
|
||||
|
|
Loading…
Reference in a new issue