table: use ?Var in find_const()

pull/2047/head
Alexander Medvednikov 2019-09-19 14:19:44 +03:00
parent f629069572
commit ad6ab39287
2 changed files with 22 additions and 18 deletions

View File

@ -1591,16 +1591,14 @@ fn (p mut Parser) name_expr() string {
return cfn.typ return cfn.typ
} }
// Constant // Constant
c := p.table.find_const(name) for {
if c.name != '' && ptr && !c.is_global { c := p.table.find_const(name) or { break }
p.error('cannot take the address of constant `$c.name`') if ptr && !c.is_global {
} p.error('cannot take the address of constant `$c.name`')
if c.name.len != 0 { } else if ptr && c.is_global {
if ptr {
// c.ptr = true // c.ptr = true
p.gen('& /*const*/ ') p.gen('& /*const*/ ')
} }
p.log('calling var expr')
mut typ := p.var_expr(c) mut typ := p.var_expr(c)
if ptr { if ptr {
typ += '*' typ += '*'
@ -2636,11 +2634,19 @@ fn (p mut Parser) array_init() string {
mut is_integer := p.tok == .number // for `[10]int` mut is_integer := p.tok == .number // for `[10]int`
// fixed length arrays with a const len: `nums := [N]int`, same as `[10]int` basically // fixed length arrays with a const len: `nums := [N]int`, same as `[10]int` basically
mut is_const_len := false mut is_const_len := false
if p.tok == .name { if p.tok == .name && !p.inside_const {
c := p.table.find_const(p.prepend_mod(p.lit)) const_name := p.prepend_mod(p.lit)
if c.name != '' && c.typ == 'int' && p.peek() == .rsbr && !p.inside_const { if p.table.known_const(const_name) {
is_integer = true c := p.table.find_const(const_name) or {
is_const_len = true //p.error('unknown const `$p.lit`')
exit(1)
}
if c.typ == 'int' && p.peek() == .rsbr { //&& !p.inside_const {
is_integer = true
is_const_len = true
} else {
p.error('bad fixed size array const `$p.lit`')
}
} }
} }
lit := p.lit lit := p.lit

View File

@ -332,9 +332,8 @@ fn (t &Table) known_fn(name string) bool {
} }
fn (t &Table) known_const(name string) bool { fn (t &Table) known_const(name string) bool {
v := t.find_const(name) _ := t.find_const(name) or { return false }
// TODO use optional return true
return v.name.len > 0
} }
fn (t mut Table) register_type(typ string) { fn (t mut Table) register_type(typ string) {
@ -672,15 +671,14 @@ fn (t &Table) main_exists() bool {
return false return false
} }
// TODO use `?Var` fn (t &Table) find_const(name string) ?Var {
fn (t &Table) find_const(name string) Var {
//println('find const l=$t.consts.len') //println('find const l=$t.consts.len')
for c in t.consts { for c in t.consts {
if c.name == name { if c.name == name {
return c return c
} }
} }
return Var{} return none
} }
// ('s', 'string') => 'string s' // ('s', 'string') => 'string s'