allow optional return types on functions
This commit is contained in:
parent
9eaa71cbd6
commit
e2f7343242
2 changed files with 22 additions and 3 deletions
|
@ -52,3 +52,5 @@ fn main(a int) int {
|
||||||
|
|
||||||
v()()()
|
v()()()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn voidfunc() {}
|
||||||
|
|
|
@ -375,6 +375,16 @@ pub const Parser = struct {
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Copy a token with a different lexeme.
|
||||||
|
fn mkToken(self: *@This(), ttype: TokenType, lexeme: []const u8, line: usize) !Token {
|
||||||
|
const owned_lexeme = try std.mem.dupe(self.allocator, u8, lexeme);
|
||||||
|
return Token{
|
||||||
|
.ttype = ttype,
|
||||||
|
.lexeme = owned_lexeme,
|
||||||
|
.line = line,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn parseFnDecl(self: *@This()) !*Node {
|
fn parseFnDecl(self: *@This()) !*Node {
|
||||||
var param_list = ast.ParamList.init(self.allocator);
|
var param_list = ast.ParamList.init(self.allocator);
|
||||||
errdefer param_list.deinit();
|
errdefer param_list.deinit();
|
||||||
|
@ -387,7 +397,7 @@ pub const Parser = struct {
|
||||||
while (self.peek().ttype != .RightParen) {
|
while (self.peek().ttype != .RightParen) {
|
||||||
const param_name = try self.consumeSingle(.Identifier);
|
const param_name = try self.consumeSingle(.Identifier);
|
||||||
|
|
||||||
// TODO dedicated function to consume a type
|
// TODO dedicated function to consume a type?
|
||||||
const param_type = try self.consumeSingle(.Identifier);
|
const param_type = try self.consumeSingle(.Identifier);
|
||||||
|
|
||||||
try param_list.append(ast.ParamDecl{
|
try param_list.append(ast.ParamDecl{
|
||||||
|
@ -398,8 +408,15 @@ pub const Parser = struct {
|
||||||
|
|
||||||
_ = try self.consumeSingle(.RightParen);
|
_ = try self.consumeSingle(.RightParen);
|
||||||
|
|
||||||
// TODO dedicated function to consume a type
|
// the return type is default void if a type
|
||||||
const return_type = try self.consumeSingle(.Identifier);
|
// is not provided
|
||||||
|
var return_type: Token = undefined;
|
||||||
|
if (self.check(.Identifier)) {
|
||||||
|
return_type = try self.consumeSingle(.Identifier);
|
||||||
|
} else {
|
||||||
|
return_type = try self.mkToken(.Identifier, "void", name.line);
|
||||||
|
}
|
||||||
|
|
||||||
var block_node = try self.parseBlock();
|
var block_node = try self.parseBlock();
|
||||||
return try self.mkFnDecl(name, param_list, return_type, block_node.Block);
|
return try self.mkFnDecl(name, param_list, return_type, block_node.Block);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue