From 37add9804382792f091a18c4e0cdcca65fc14045 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 17 Dec 2019 05:04:50 +0300 Subject: [PATCH] fix programs without fn main --- vlib/compiler/parser.v | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index ec5932996f..4a00d15ba6 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -55,6 +55,7 @@ mut: calling_c bool cur_fn Fn local_vars []Var // local function variables + global_vars []Var // only for "script" programs without "fn main" var_idx int returns bool vroot string @@ -546,17 +547,19 @@ fn (p mut Parser) parse(pass Pass) { p.clear_vars() } } - mut start := p.cgen.lines.len + start := p.cgen.lines.len p.statement(true) - if start > 0 && p.cgen.lines[start - 1] != '' && p.cgen.fn_main != '' { - start-- - } + //if start > 0 && p.cgen.lines[start - 1] != '' && + //p.cgen.fn_main != '' { + //start-- + //} p.genln('') end := p.cgen.lines.len lines := p.cgen.lines[start..end] + //println('adding "' + lines.join('\n') + '"\n') //mut line := p.cgen.fn_main + lines.join('\n') //line = line.trim_space() - p.cgen.fn_main = p.cgen.fn_main + lines.join('\n') + p.cgen.fn_main += lines.join('\n') p.cgen.resetln('') for i := start; i < end; i++ { p.cgen.lines[i] = '' @@ -565,7 +568,7 @@ fn (p mut Parser) parse(pass Pass) { else { p.error('unexpected token `${p.strtok()}`') } - } + } } } } @@ -1618,14 +1621,18 @@ fn (p mut Parser) var_decl() { // declaration p.gen('$var_type $var_name = ${p.var_decl_name}.var_$i') } - p.register_var(Var { - name: var_name - typ: var_type - is_mut: var_is_mut - is_alloc: p.is_alloc || var_type.starts_with('array_') - line_nr: p.tokens[ var_token_idx ].line_nr - token_idx: var_token_idx - }) + // Function bodies are always parsed once in the second pass, but + // script programs are parsed in the first pass, need to handle that. + if p.pass == .main { + p.register_var(Var { + name: var_name + typ: var_type + is_mut: var_is_mut + is_alloc: p.is_alloc || var_type.starts_with('array_') + line_nr: p.tokens[ var_token_idx ].line_nr + token_idx: var_token_idx + }) + } //if p.fileis('str.v') { //if p.is_alloc { println('REG VAR IS ALLOC $name') } //}