add basic param parsing

This commit is contained in:
Luna 2019-08-23 15:52:04 -03:00
parent 7ba140dd73
commit 89e386d2d4
3 changed files with 30 additions and 11 deletions

View File

@ -1 +1 @@
fn main() {} fn main( a int) {}

View File

@ -3,7 +3,7 @@ const tokens = @import("tokens.zig");
const Token = tokens.Token; const Token = tokens.Token;
pub const NodeList = std.ArrayList(*Node); 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 // TODO convert FnCall to something like PrefixOp / InfixOp / SuffixOp
pub const NodeType = enum { pub const NodeType = enum {
@ -14,10 +14,10 @@ pub const NodeType = enum {
}; };
pub const ParamDecl = struct { pub const ParamDecl = struct {
param_name: Token, name: Token,
// TODO types // TODO types
param_type: Token, typ: Token,
}; };
pub const FnDecl = struct { 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 { pub fn printNode(node: *Node, ident: usize) void {
switch (node.*) { switch (node.*) {
.FnDecl => |proto| { .FnDecl => |decl| {
print(ident, "FnDecl name='{}'\n", proto.func_name.lexeme); 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 => { .Root => {
for (node.Root.toSlice()) |child| { for (node.Root.toSlice()) |child| {

View File

@ -231,19 +231,29 @@ pub const Parser = struct {
fn topDecl(self: *@This()) !?*Node { fn topDecl(self: *@This()) !?*Node {
return switch (self.peek().ttype) { return switch (self.peek().ttype) {
.Fn => blk: { .Fn => blk: {
var param_list = ast.ParamList.init(self.allocator);
errdefer param_list.deinit();
_ = try self.consumeSingle(.Fn); _ = try self.consumeSingle(.Fn);
var name = try self.consumeSingle(.Identifier); var name = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.LeftParen); _ = 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(.RightParen);
_ = try self.consumeSingle(.LeftBrace); _ = try self.consumeSingle(.LeftBrace);
// TODO block // TODO block
_ = try self.consumeSingle(.RightBrace); _ = try self.consumeSingle(.RightBrace);
std.debug.warn("!fn name: {}\n", name); std.debug.warn("!fn name: {}\n", name);
break :blk try self.mkFnDecl( break :blk try self.mkFnDecl(name, param_list);
name,
ast.ParamList.init(self.allocator),
);
}, },
else => |ttype| blk: { else => |ttype| blk: {
self.doError("expected fn, got {}\n", ttype); self.doError("expected fn, got {}\n", ttype);