add basic param parsing
This commit is contained in:
parent
7ba140dd73
commit
89e386d2d4
3 changed files with 30 additions and 11 deletions
|
@ -1 +1 @@
|
|||
fn main() {}
|
||||
fn main( a int) {}
|
||||
|
|
19
src/ast.zig
19
src/ast.zig
|
@ -3,7 +3,7 @@ const tokens = @import("tokens.zig");
|
|||
const Token = tokens.Token;
|
||||
|
||||
pub const NodeList = std.ArrayList(*Node);
|
||||
pub const ParamList = std.ArrayList(*ParamDecl);
|
||||
pub const ParamList = std.ArrayList(ParamDecl);
|
||||
|
||||
// TODO convert FnCall to something like PrefixOp / InfixOp / SuffixOp
|
||||
pub const NodeType = enum {
|
||||
|
@ -14,10 +14,10 @@ pub const NodeType = enum {
|
|||
};
|
||||
|
||||
pub const ParamDecl = struct {
|
||||
param_name: Token,
|
||||
name: Token,
|
||||
|
||||
// TODO types
|
||||
param_type: Token,
|
||||
typ: Token,
|
||||
};
|
||||
|
||||
pub const FnDecl = struct {
|
||||
|
@ -55,8 +55,17 @@ fn print(ident: usize, comptime fmt: []const u8, args: ...) void {
|
|||
|
||||
pub fn printNode(node: *Node, ident: usize) void {
|
||||
switch (node.*) {
|
||||
.FnDecl => |proto| {
|
||||
print(ident, "FnDecl name='{}'\n", proto.func_name.lexeme);
|
||||
.FnDecl => |decl| {
|
||||
print(ident, "FnDecl name='{}'\n", decl.func_name.lexeme);
|
||||
|
||||
for (decl.params.toSlice()) |param| {
|
||||
print(
|
||||
ident + 1,
|
||||
"param: '{}' {}\n",
|
||||
param.name.lexeme,
|
||||
param.typ.lexeme,
|
||||
);
|
||||
}
|
||||
},
|
||||
.Root => {
|
||||
for (node.Root.toSlice()) |child| {
|
||||
|
|
|
@ -231,19 +231,29 @@ pub const Parser = struct {
|
|||
fn topDecl(self: *@This()) !?*Node {
|
||||
return switch (self.peek().ttype) {
|
||||
.Fn => blk: {
|
||||
var param_list = ast.ParamList.init(self.allocator);
|
||||
errdefer param_list.deinit();
|
||||
|
||||
_ = try self.consumeSingle(.Fn);
|
||||
var name = try self.consumeSingle(.Identifier);
|
||||
|
||||
_ = try self.consumeSingle(.LeftParen);
|
||||
// TODO paramlist
|
||||
|
||||
while (self.peek().ttype != .RightParen) {
|
||||
const param_name = try self.consumeSingle(.Identifier);
|
||||
const param_type = try self.consumeSingle(.Identifier);
|
||||
|
||||
try param_list.append(ast.ParamDecl{ .name = param_name, .typ = param_type });
|
||||
}
|
||||
|
||||
_ = try self.consumeSingle(.RightParen);
|
||||
|
||||
_ = try self.consumeSingle(.LeftBrace);
|
||||
// TODO block
|
||||
_ = try self.consumeSingle(.RightBrace);
|
||||
|
||||
std.debug.warn("!fn name: {}\n", name);
|
||||
break :blk try self.mkFnDecl(
|
||||
name,
|
||||
ast.ParamList.init(self.allocator),
|
||||
);
|
||||
break :blk try self.mkFnDecl(name, param_list);
|
||||
},
|
||||
else => |ttype| blk: {
|
||||
self.doError("expected fn, got {}\n", ttype);
|
||||
|
|
Loading…
Reference in a new issue