Compare commits

..

No commits in common. "89e386d2d4cddcc6c07c92905d7238695f992b1a" and "e3fdf5399b0f0d69835ed0eb8fefa9b77953949a" have entirely different histories.

4 changed files with 11 additions and 60 deletions

View file

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

View file

@ -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 {
name: Token,
param_name: Token,
// TODO types
typ: Token,
param_type: Token,
};
pub const FnDecl = struct {
@ -43,37 +43,3 @@ pub fn mkRoot(allocator: *std.mem.Allocator) !*Node {
node.* = Node{ .Root = NodeList.init(allocator) };
return node;
}
fn print(ident: usize, comptime fmt: []const u8, args: ...) void {
var i: usize = 0;
while (i < ident) : (i += 1) {
std.debug.warn("\t");
}
std.debug.warn(fmt, args);
}
pub fn printNode(node: *Node, ident: usize) void {
switch (node.*) {
.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| {
printNode(child, ident + 1);
}
},
else => {
print(ident, "unknown node: {}\n", node);
},
}
}

View file

@ -231,29 +231,13 @@ 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);
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, param_list);
break :blk try self.mkFnDecl(
name,
ast.ParamList.init(self.allocator),
);
},
else => |ttype| blk: {
self.doError("expected fn, got {}\n", ttype);

View file

@ -48,8 +48,9 @@ pub const Runner = struct {
var it = root.Root.iterator();
std.debug.warn("parse tree\n");
ast.printNode(root, 0);
while (it.next()) |node| {
std.debug.warn("{}\n", node.*);
}
return Result.Ok;
}