split pre-method into parsePreMethod

This commit is contained in:
Luna 2019-08-26 20:31:15 -03:00
parent a72346b888
commit e0d712cd9f
2 changed files with 41 additions and 14 deletions

View file

@ -22,11 +22,18 @@ pub const ParamDecl = struct {
typ: Token, typ: Token,
}; };
pub const MethodData = struct {
variable: Token,
typ: Token,
mutable: bool,
};
pub const FnDecl = struct { pub const FnDecl = struct {
func_name: Token, func_name: Token,
params: ParamList, params: ParamList,
return_type: Token, return_type: Token,
body: StmtList, body: StmtList,
method: ?*MethodData,
}; };
pub const SingleConst = struct { pub const SingleConst = struct {

View file

@ -139,6 +139,7 @@ pub const Parser = struct {
params: ast.ParamList, params: ast.ParamList,
return_type: Token, return_type: Token,
block: ast.StmtList, block: ast.StmtList,
method: ?*ast.MethodData,
) !*ast.Node { ) !*ast.Node {
var node = try self.allocator.create(Node); var node = try self.allocator.create(Node);
node.* = Node{ node.* = Node{
@ -147,6 +148,7 @@ pub const Parser = struct {
.params = params, .params = params,
.return_type = return_type, .return_type = return_type,
.body = block, .body = block,
.method = method,
}, },
}; };
return node; return node;
@ -389,22 +391,12 @@ pub const Parser = struct {
var param_list = ast.ParamList.init(self.allocator); var param_list = ast.ParamList.init(self.allocator);
errdefer param_list.deinit(); errdefer param_list.deinit();
var method: ?*ast.MethodData = null;
_ = try self.consumeSingle(.Fn); _ = try self.consumeSingle(.Fn);
if (self.check(.LeftParen)) { if (self.check(.LeftParen)) {
_ = try self.consumeSingle(.LeftParen); method = try self.parsePreMethod();
var mutable_ref: bool = false;
const method_var = try self.consumeSingle(.Identifier);
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
mutable_ref = true;
}
const method_typ = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.RightParen);
} }
const name = try self.consumeSingle(.Identifier); const name = try self.consumeSingle(.Identifier);
@ -435,7 +427,35 @@ 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); return try self.mkFnDecl(name, param_list, return_type, block_node.Block, method);
}
/// parse the (v [mut] T) part of the method (defined here
/// as a premethod)
fn parsePreMethod(self: *@This()) !?*ast.MethodData {
_ = try self.consumeSingle(.LeftParen);
var mutable_ref: bool = false;
const variable = try self.consumeSingle(.Identifier);
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
mutable_ref = true;
}
const typ = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.RightParen);
// create method data and assign the values we got into it
var method = try self.allocator.create(ast.MethodData);
method.* = ast.MethodData{
.variable = variable,
.typ = typ,
.mutable = mutable_ref,
};
return method;
} }
fn parseConstDecl(self: *@This()) !*Node { fn parseConstDecl(self: *@This()) !*Node {