fix programs without fn main

pull/3127/head
Alexander Medvednikov 2019-12-17 05:04:50 +03:00
parent 84f535d242
commit 37add98043
1 changed files with 21 additions and 14 deletions

View File

@ -55,6 +55,7 @@ mut:
calling_c bool calling_c bool
cur_fn Fn cur_fn Fn
local_vars []Var // local function variables local_vars []Var // local function variables
global_vars []Var // only for "script" programs without "fn main"
var_idx int var_idx int
returns bool returns bool
vroot string vroot string
@ -546,17 +547,19 @@ fn (p mut Parser) parse(pass Pass) {
p.clear_vars() p.clear_vars()
} }
} }
mut start := p.cgen.lines.len start := p.cgen.lines.len
p.statement(true) p.statement(true)
if start > 0 && p.cgen.lines[start - 1] != '' && p.cgen.fn_main != '' { //if start > 0 && p.cgen.lines[start - 1] != '' &&
start-- //p.cgen.fn_main != '' {
} //start--
//}
p.genln('') p.genln('')
end := p.cgen.lines.len end := p.cgen.lines.len
lines := p.cgen.lines[start..end] lines := p.cgen.lines[start..end]
//println('adding "' + lines.join('\n') + '"\n')
//mut line := p.cgen.fn_main + lines.join('\n') //mut line := p.cgen.fn_main + lines.join('\n')
//line = line.trim_space() //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('') p.cgen.resetln('')
for i := start; i < end; i++ { for i := start; i < end; i++ {
p.cgen.lines[i] = '' p.cgen.lines[i] = ''
@ -1618,6 +1621,9 @@ fn (p mut Parser) var_decl() {
// declaration // declaration
p.gen('$var_type $var_name = ${p.var_decl_name}.var_$i') p.gen('$var_type $var_name = ${p.var_decl_name}.var_$i')
} }
// 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 { p.register_var(Var {
name: var_name name: var_name
typ: var_type typ: var_type
@ -1626,6 +1632,7 @@ fn (p mut Parser) var_decl() {
line_nr: p.tokens[ var_token_idx ].line_nr line_nr: p.tokens[ var_token_idx ].line_nr
token_idx: var_token_idx token_idx: var_token_idx
}) })
}
//if p.fileis('str.v') { //if p.fileis('str.v') {
//if p.is_alloc { println('REG VAR IS ALLOC $name') } //if p.is_alloc { println('REG VAR IS ALLOC $name') }
//} //}