work towards FnDecl ast node
This commit is contained in:
parent
b591ecdf9b
commit
e2438f143b
2 changed files with 27 additions and 9 deletions
|
@ -10,7 +10,7 @@ pub const NodeType = enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const FnDecl = struct {
|
pub const FnDecl = struct {
|
||||||
func_name: []const u8,
|
func_name: Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Node = union(NodeType) {
|
pub const Node = union(NodeType) {
|
||||||
|
|
|
@ -90,26 +90,44 @@ pub const Parser = struct {
|
||||||
return Result.CompileError;
|
return Result.CompileError;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mkFnDecl(self: *Parser, name: []const u8) !*ast.Node {
|
fn consumeSingle(self: *Parser, ttype: TokenType) !Token {
|
||||||
var node = try self.allocator.create(Node.FnDecl);
|
if (self.check(ttype)) return self.advance();
|
||||||
node.* = Node.FnDecl{ .name = name };
|
|
||||||
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn functionDecl(self: *Parser) !*ast.Node {
|
fn functionDecl(self: *Parser) !*ast.Node {
|
||||||
// get the name
|
// get the name
|
||||||
var name = try self.consume(.Identifier, "expected function name");
|
var name = try self.consume(.Identifier, "expected function name");
|
||||||
|
_ = try self.consumeSingle(.LeftParen);
|
||||||
return try self.mkFnDecl(name);
|
return try self.mkFnDecl(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn processToken(self: *Parser, token: Token) Result!ast.Node {
|
fn processToken(self: *Parser, token: Token) !*ast.Node {
|
||||||
switch (token.ttype) {
|
var node = switch (token.ttype) {
|
||||||
//.Fn => try self.functionDecl(),
|
.Fn => try self.functionDecl(),
|
||||||
else => blk: {
|
else => blk: {
|
||||||
try self.doError("TODO handle {}\n", token.ttype);
|
try self.doError("TODO handle {}\n", token.ttype);
|
||||||
return Result.CompileError;
|
return Result.CompileError;
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(self: *Parser) !*ast.Node {
|
pub fn parse(self: *Parser) !*ast.Node {
|
||||||
|
@ -128,7 +146,7 @@ pub const Parser = struct {
|
||||||
if (token.ttype == .EOF) break;
|
if (token.ttype == .EOF) break;
|
||||||
|
|
||||||
var node = try self.processToken(token);
|
var node = try self.processToken(token);
|
||||||
try root.Root.append(&node);
|
try root.Root.append(node);
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue