move Parser.mkFnDecl => ast.Node.mkFnDecl

This commit is contained in:
Luna 2019-09-20 13:54:52 -03:00
parent d69365ba92
commit 250631ff41
3 changed files with 31 additions and 23 deletions

View file

@ -7,5 +7,5 @@ fn add(a: i32, b: i32) i32 {
// type is void by default // type is void by default
fn main() { fn main() {
std.fmt.print("piss\n"); std.fmt.print("piss\n");
// std.fmt.print("2 + 2 = %d\n", add(1, 2)); std.fmt.print("2 + 2 = %d\n", add(1, 2));
} }

View file

@ -284,6 +284,28 @@ pub const Node = union(NodeType) {
return node; return node;
} }
pub fn mkFnDecl(
allocator: *std.mem.Allocator,
name: Token,
params: ParamList,
return_type: Token,
block: StmtList,
method: ?*MethodData,
) !*Node {
var node = try allocator.create(Node);
node.* = Node{
.FnDecl = FnDecl{
.func_name = name,
.params = params,
.return_type = return_type,
.body = block,
.method = method,
},
};
return node;
}
pub fn mkStructDecl(allocator: *std.mem.Allocator, name: Token, fields: FieldList) !*Node { pub fn mkStructDecl(allocator: *std.mem.Allocator, name: Token, fields: FieldList) !*Node {
var node = try allocator.create(Node); var node = try allocator.create(Node);
node.* = Node{ node.* = Node{

View file

@ -175,27 +175,6 @@ pub const Parser = struct {
// TODO maybe move helper functions to ast_helper.zig? // TODO maybe move helper functions to ast_helper.zig?
fn mkFnDecl(
self: *Parser,
name: Token,
params: ast.ParamList,
return_type: Token,
block: ast.StmtList,
method: ?*ast.MethodData,
) !*ast.Node {
var node = try self.allocator.create(Node);
node.* = Node{
.FnDecl = ast.FnDecl{
.func_name = name,
.params = params,
.return_type = return_type,
.body = block,
.method = method,
},
};
return node;
}
fn mkConstDecl(self: *Parser, consts: ast.ConstList) !*ast.Node { fn mkConstDecl(self: *Parser, consts: ast.ConstList) !*ast.Node {
var node = try self.allocator.create(Node); var node = try self.allocator.create(Node);
node.* = Node{ .ConstDecl = consts }; node.* = Node{ .ConstDecl = consts };
@ -486,7 +465,14 @@ pub const Parser = struct {
} }
var block_node = try self.parseBlock(); var block_node = try self.parseBlock();
return try self.mkFnDecl(name, param_list, return_type, block_node.Block, method); return try Node.mkFnDecl(
self.allocator,
name,
param_list,
return_type,
block_node.Block,
method,
);
} }
/// parse the (v [mut] T) part of the method (defined here /// parse the (v [mut] T) part of the method (defined here