Compare commits
2 commits
e3fdf5399b
...
89e386d2d4
Author | SHA1 | Date | |
---|---|---|---|
89e386d2d4 | |||
7ba140dd73 |
4 changed files with 60 additions and 11 deletions
|
@ -1 +1 @@
|
||||||
fn main() {}
|
fn main( a int) {}
|
||||||
|
|
40
src/ast.zig
40
src/ast.zig
|
@ -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 {
|
||||||
|
@ -43,3 +43,37 @@ pub fn mkRoot(allocator: *std.mem.Allocator) !*Node {
|
||||||
node.* = Node{ .Root = NodeList.init(allocator) };
|
node.* = Node{ .Root = NodeList.init(allocator) };
|
||||||
return node;
|
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);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -231,13 +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);
|
||||||
|
|
||||||
|
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);
|
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);
|
||||||
|
|
|
@ -48,9 +48,8 @@ pub const Runner = struct {
|
||||||
|
|
||||||
var it = root.Root.iterator();
|
var it = root.Root.iterator();
|
||||||
|
|
||||||
while (it.next()) |node| {
|
std.debug.warn("parse tree\n");
|
||||||
std.debug.warn("{}\n", node.*);
|
ast.printNode(root, 0);
|
||||||
}
|
|
||||||
|
|
||||||
return Result.Ok;
|
return Result.Ok;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue