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

View File

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

View File

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

View File

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

View File

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