work towards FnDecl ast node

This commit is contained in:
Luna 2019-07-01 15:25:07 -03:00
parent b591ecdf9b
commit e2438f143b
2 changed files with 27 additions and 9 deletions

View File

@ -10,7 +10,7 @@ pub const NodeType = enum {
};
pub const FnDecl = struct {
func_name: []const u8,
func_name: Token,
};
pub const Node = union(NodeType) {

View File

@ -90,26 +90,44 @@ pub const Parser = struct {
return Result.CompileError;
}
fn mkFnDecl(self: *Parser, name: []const u8) !*ast.Node {
var node = try self.allocator.create(Node.FnDecl);
node.* = Node.FnDecl{ .name = name };
fn consumeSingle(self: *Parser, ttype: TokenType) !Token {
if (self.check(ttype)) return self.advance();
var buf_main: [1000]u8 = undefined;
var buf = try std.fmt.bufPrint(
buf_main[0..],
"expected {}, got {}",
ttype,
self.peek().ttype,
);
try self.tokenError(self.peek(), buf);
return Result.CompileError;
}
fn mkFnDecl(self: *Parser, name: Token) !*ast.Node {
var node = try self.allocator.create(Node);
node.* = Node{ .FnDecl = ast.FnDecl{ .func_name = name } };
return node;
}
fn functionDecl(self: *Parser) !*ast.Node {
// get the name
var name = try self.consume(.Identifier, "expected function name");
_ = try self.consumeSingle(.LeftParen);
return try self.mkFnDecl(name);
}
fn processToken(self: *Parser, token: Token) Result!ast.Node {
switch (token.ttype) {
//.Fn => try self.functionDecl(),
fn processToken(self: *Parser, token: Token) !*ast.Node {
var node = switch (token.ttype) {
.Fn => try self.functionDecl(),
else => blk: {
try self.doError("TODO handle {}\n", token.ttype);
return Result.CompileError;
},
}
};
return node;
}
pub fn parse(self: *Parser) !*ast.Node {
@ -128,7 +146,7 @@ pub const Parser = struct {
if (token.ttype == .EOF) break;
var node = try self.processToken(token);
try root.Root.append(&node);
try root.Root.append(node);
} else {
continue;
}