diff --git a/examples/hello.ry b/examples/hello.ry index 8c6d510..d7eaae6 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -7,5 +7,5 @@ fn add(a: i32, b: i32) i32 { // type is void by default fn main() { 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)); } diff --git a/src/ast.zig b/src/ast.zig index d5f8346..95d59c0 100644 --- a/src/ast.zig +++ b/src/ast.zig @@ -284,6 +284,28 @@ pub const Node = union(NodeType) { 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 { var node = try allocator.create(Node); node.* = Node{ diff --git a/src/parsers.zig b/src/parsers.zig index 3542261..d31c611 100644 --- a/src/parsers.zig +++ b/src/parsers.zig @@ -175,27 +175,6 @@ pub const Parser = struct { // 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 { var node = try self.allocator.create(Node); node.* = Node{ .ConstDecl = consts }; @@ -486,7 +465,14 @@ pub const Parser = struct { } 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