parser: mut in receivers

pull/4534/head^2
Alexander Medvednikov 2020-04-21 05:07:49 +02:00
parent cc0e0ed281
commit 1bf094fbca
2 changed files with 16 additions and 12 deletions

View File

@ -24,9 +24,19 @@ const (
'int' 'int'
'long' 'long'
'register' 'register'
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned' 'restrict'
'short'
'signed'
'sizeof'
'static'
'switch'
'typedef'
'union'
'unsigned'
'void' 'void'
'volatile', 'while'] 'volatile'
'while'
]
) )
fn foo(t token.Token) { fn foo(t token.Token) {

View File

@ -107,11 +107,11 @@ fn (var p Parser) fn_decl() ast.FnDecl {
var rec_mut := false var rec_mut := false
var args := []table.Arg var args := []table.Arg
if p.tok.kind == .lpar { if p.tok.kind == .lpar {
p.next() // ( p.next() // (
is_method = true is_method = true
rec_mut = p.tok.kind == .key_var rec_mut = p.tok.kind in [.key_var, .key_mut]
if rec_mut { if rec_mut {
p.next() // `var` p.next() // `var`
} }
rec_name = p.check_name() rec_name = p.check_name()
if !rec_mut { if !rec_mut {
@ -146,7 +146,7 @@ fn (var p Parser) fn_decl() ast.FnDecl {
} }
} }
if p.tok.kind in [.plus, .minus, .mul, .div, .mod] { if p.tok.kind in [.plus, .minus, .mul, .div, .mod] {
name = p.tok.kind.str() // op_to_fn_name() name = p.tok.kind.str() // op_to_fn_name()
p.next() p.next()
} }
// <T> // <T>
@ -240,9 +240,7 @@ fn (var p Parser) anon_fn() ast.AnonFn {
pos := p.tok.position() pos := p.tok.position()
p.open_scope() p.open_scope()
p.check(.key_fn) p.check(.key_fn)
// TODO generics // TODO generics
args, is_variadic := p.fn_args() args, is_variadic := p.fn_args()
for arg in args { for arg in args {
p.scope.register(arg.name, ast.Var{ p.scope.register(arg.name, ast.Var{
@ -250,19 +248,16 @@ fn (var p Parser) anon_fn() ast.AnonFn {
typ: arg.typ typ: arg.typ
}) })
} }
var return_type := table.void_type var return_type := table.void_type
if p.tok.kind.is_start_of_type() { if p.tok.kind.is_start_of_type() {
return_type = p.parse_type() return_type = p.parse_type()
} }
var stmts := []ast.Stmt var stmts := []ast.Stmt
no_body := p.tok.kind != .lcbr no_body := p.tok.kind != .lcbr
if p.tok.kind == .lcbr { if p.tok.kind == .lcbr {
stmts = p.parse_block() stmts = p.parse_block()
} }
p.close_scope() p.close_scope()
func := table.Fn{ func := table.Fn{
args: args args: args
is_variadic: is_variadic is_variadic: is_variadic
@ -271,7 +266,6 @@ fn (var p Parser) anon_fn() ast.AnonFn {
idx := p.table.find_or_register_fn_type(func, false) idx := p.table.find_or_register_fn_type(func, false)
typ := table.new_type(idx) typ := table.new_type(idx)
name := p.table.get_type_name(typ) name := p.table.get_type_name(typ)
return ast.AnonFn{ return ast.AnonFn{
decl: ast.FnDecl{ decl: ast.FnDecl{
name: name name: name