token/parser: var keyword

pull/4417/head
Alexander Medvednikov 2020-04-15 01:45:27 +02:00
parent 4068cbc6eb
commit efaec8be8e
5 changed files with 32 additions and 23 deletions

View File

@ -386,9 +386,9 @@ pub fn (c mut Checker) call_fn(call_expr mut ast.CallExpr) table.Type {
// check for arg (var) of fn type
if !found {
scope := c.file.scope.innermost(call_expr.pos.pos)
if var := scope.find_var(fn_name) {
if var.typ != 0 {
vts := c.table.get_type_symbol(var.typ)
if v := scope.find_var(fn_name) {
if v.typ != 0 {
vts := c.table.get_type_symbol(v.typ)
if vts.kind == .function {
info := vts.info as table.FnType
f = info.func
@ -1018,14 +1018,14 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
}
ast.Assoc {
scope := c.file.scope.innermost(it.pos.pos)
var := scope.find_var(it.var_name) or {
v := scope.find_var(it.var_name) or {
panic(err)
}
for i, _ in it.fields {
c.expr(it.exprs[i])
}
it.typ = var.typ
return var.typ
it.typ = v.typ
return v.typ
}
ast.BoolLiteral {
return table.bool_type

View File

@ -827,16 +827,16 @@ fn (g mut Gen) free_scope_vars(pos int) {
// // TODO why 0?
// continue
// }
var := *it
sym := g.table.get_type_symbol(var.typ)
is_optional := table.type_is(var.typ, .optional)
v := *it
sym := g.table.get_type_symbol(v.typ)
is_optional := table.type_is(v.typ, .optional)
if sym.kind == .array && !is_optional {
g.writeln('array_free($var.name); // autofreed')
g.writeln('array_free($v.name); // autofreed')
}
if sym.kind == .string && !is_optional {
// Don't free simple string literals.
t := typeof(var.expr)
match var.expr {
t := typeof(v.expr)
match v.expr {
ast.StringLiteral {
g.writeln('// str literal')
continue
@ -848,7 +848,7 @@ fn (g mut Gen) free_scope_vars(pos int) {
continue
}
}
g.writeln('string_free($var.name); // autofreed')
g.writeln('string_free($v.name); // autofreed')
}
}
else {}

View File

@ -102,10 +102,16 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
mut rec_mut := false
mut args := []table.Arg
if p.tok.kind == .lpar {
p.next() // (
is_method = true
p.next()
rec_mut = p.tok.kind == .key_var
if rec_mut {
p.next() // `var`
}
rec_name = p.check_name()
rec_mut = p.tok.kind == .key_mut
if !rec_mut {
rec_mut = p.tok.kind == .key_mut
}
is_amp := p.peek_tok.kind == .amp
// if rec_mut {
// p.check(.key_mut)

View File

@ -371,7 +371,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
pos: assert_pos
}
}
.key_mut, .key_static {
.key_mut, .key_static, .key_var {
return p.assign_stmt()
}
.key_for {
@ -1081,7 +1081,7 @@ fn (p mut Parser) for_stmt() ast.Stmt {
pos: pos
is_inf: true
}
} else if p.tok.kind == .key_mut {
} else if p.tok.kind in [.key_mut, .key_var] {
p.error('`mut` is not required in for loops')
} else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
// `for i := 0; i < 10; i++ {`
@ -1733,9 +1733,9 @@ fn (p mut Parser) return_stmt() ast.Return {
fn (p mut Parser) parse_assign_lhs() []ast.Ident {
mut idents := []ast.Ident
for {
is_mut := p.tok.kind == .key_mut
is_mut := p.tok.kind == .key_mut || p.tok.kind == .key_var
if is_mut {
p.check(.key_mut)
p.next()
}
is_static := p.tok.kind == .key_static
if is_static {
@ -1785,7 +1785,8 @@ fn (p mut Parser) assign_stmt() ast.Stmt {
is_decl := op == .decl_assign
for i, ident in idents {
if op == .decl_assign && scanner.contains_capital(ident.name) {
p.error_with_pos('variable names cannot contain uppercase letters, use snake_case instead', ident.pos)
p.error_with_pos('variable names cannot contain uppercase letters, use snake_case instead',
ident.pos)
}
known_var := p.scope.known_var(ident.name)
if !is_decl && !known_var {
@ -1864,7 +1865,7 @@ fn (p mut Parser) global_decl() ast.GlobalDecl {
fn (p mut Parser) match_expr() ast.MatchExpr {
match_first_pos := p.tok.position()
p.check(.key_match)
is_mut := p.tok.kind == .key_mut
is_mut := p.tok.kind in [.key_mut, .key_var]
mut is_sum_type := false
if is_mut {
p.next()
@ -2069,7 +2070,7 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
fn (p mut Parser) assoc() ast.Assoc {
var_name := p.check_name()
pos := p.tok.position()
var := p.scope.find_var(var_name) or {
v := p.scope.find_var(var_name) or {
p.error('unknown variable `$var_name`')
return ast.Assoc{}
}
@ -2094,7 +2095,7 @@ fn (p mut Parser) assoc() ast.Assoc {
fields: fields
exprs: vals
pos: pos
typ: var.typ
typ: v.typ
}
}

View File

@ -120,6 +120,7 @@ pub enum Kind {
key_pub
key_static
key_unsafe
key_var
keyword_end
_end_
}
@ -245,6 +246,7 @@ fn build_token_str() []string {
s[Kind.key_select] = 'select'
s[Kind.key_none] = 'none'
s[Kind.key_offsetof] = '__offsetof'
s[Kind.key_var] = 'var'
return s
}