parser: change cur_line, tmp_line only in main pass

pull/1236/head
d2verb 2019-07-19 03:21:23 +09:00 committed by Alexander Medvednikov
parent 38c58f9f1b
commit 67c2932f34
3 changed files with 29 additions and 17 deletions

View File

@ -50,7 +50,7 @@ fn new_cgen(out_name_c string) *CGen {
} }
fn (g mut CGen) genln(s string) { fn (g mut CGen) genln(s string) {
if g.nogen || g.run == .decl { if g.nogen || g.run != .main {
return return
} }
if g.is_tmp { if g.is_tmp {
@ -66,7 +66,7 @@ fn (g mut CGen) genln(s string) {
} }
fn (g mut CGen) gen(s string) { fn (g mut CGen) gen(s string) {
if g.nogen || g.run == .decl { if g.nogen || g.run != .main {
return return
} }
if g.is_tmp { if g.is_tmp {
@ -77,6 +77,18 @@ fn (g mut CGen) gen(s string) {
} }
} }
fn (g mut CGen) resetln(s string) {
if g.nogen || g.run != .main {
return
}
if g.is_tmp {
g.tmp_line = s
}
else {
g.cur_line = s
}
}
fn (g mut CGen) save() { fn (g mut CGen) save() {
s := g.lines.join('\n') s := g.lines.join('\n')
g.out.writeln(s) g.out.writeln(s)
@ -109,7 +121,7 @@ fn (g mut CGen) add_placeholder() int {
} }
fn (g mut CGen) set_placeholder(pos int, val string) { fn (g mut CGen) set_placeholder(pos int, val string) {
if g.nogen { if g.nogen || g.run != .main {
return return
} }
// g.lines.set(pos, val) // g.lines.set(pos, val)
@ -135,7 +147,7 @@ fn (g mut CGen) add_placeholder2() int {
} }
fn (g mut CGen) set_placeholder2(pos int, val string) { fn (g mut CGen) set_placeholder2(pos int, val string) {
if g.nogen { if g.nogen || g.run != .main {
return return
} }
if g.is_tmp { if g.is_tmp {

View File

@ -696,7 +696,7 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn {
T := p.table.find_type(typ) T := p.table.find_type(typ)
fmt := p.typ_to_fmt(typ, 0) fmt := p.typ_to_fmt(typ, 0)
if fmt != '' { if fmt != '' {
p.cgen.cur_line = p.cgen.cur_line.replace('println (', '/*opt*/printf ("' + fmt + '\\n", ') p.cgen.resetln(p.cgen.cur_line.replace('println (', '/*opt*/printf ("' + fmt + '\\n", '))
continue continue
} }
if typ.ends_with('*') { if typ.ends_with('*') {
@ -714,7 +714,7 @@ fn (p mut Parser) fn_call_args(f *Fn) *Fn {
if name == '}' { if name == '}' {
p.error(error_msg) p.error(error_msg)
} }
p.cgen.cur_line = p.cgen.cur_line.left(index) p.cgen.resetln(p.cgen.cur_line.left(index))
p.create_type_string(T, name) p.create_type_string(T, name)
p.cgen.cur_line.replace(typ, '') p.cgen.cur_line.replace(typ, '')
p.next() p.next()

View File

@ -277,7 +277,7 @@ fn (p mut Parser) parse() {
//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 = p.cgen.fn_main + lines.join('\n')
p.cgen.cur_line = '' p.cgen.resetln('')
for i := start; i < end; i++ { for i := start; i < end; i++ {
p.cgen.lines[i] = '' p.cgen.lines[i] = ''
} }
@ -381,7 +381,7 @@ fn (p mut Parser) const_decl() {
// output a #define so that we don't pollute the binary with unnecessary global vars // output a #define so that we don't pollute the binary with unnecessary global vars
if is_compile_time_const(p.cgen.cur_line) { if is_compile_time_const(p.cgen.cur_line) {
p.cgen.consts << '#define $name $p.cgen.cur_line' p.cgen.consts << '#define $name $p.cgen.cur_line'
p.cgen.cur_line = '' p.cgen.resetln('')
p.fgenln('') p.fgenln('')
continue continue
} }
@ -393,7 +393,7 @@ fn (p mut Parser) const_decl() {
p.cgen.consts << p.table.cgen_name_type_pair(name, typ) + ';' p.cgen.consts << p.table.cgen_name_type_pair(name, typ) + ';'
p.cgen.consts_init << '$name = $p.cgen.cur_line;' p.cgen.consts_init << '$name = $p.cgen.cur_line;'
} }
p.cgen.cur_line = '' p.cgen.resetln('')
} }
p.fgenln('') p.fgenln('')
} }
@ -1144,7 +1144,7 @@ fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
expr := p.cgen.cur_line.right(pos) expr := p.cgen.cur_line.right(pos)
left := p.cgen.cur_line.left(pos) left := p.cgen.cur_line.left(pos)
//p.cgen.cur_line = left + 'opt_ok($expr)' //p.cgen.cur_line = left + 'opt_ok($expr)'
p.cgen.cur_line = left + 'opt_ok($expr, sizeof($expr_type))' p.cgen.resetln(left + 'opt_ok($expr, sizeof($expr_type))')
} }
else if !p.builtin_pkg && !p.check_types_no_throw(expr_type, p.assigned_type) { else if !p.builtin_pkg && !p.check_types_no_throw(expr_type, p.assigned_type) {
p.scanner.line_nr-- p.scanner.line_nr--
@ -1813,7 +1813,7 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
// Cant have &7, so need a tmp // Cant have &7, so need a tmp
tmp := p.get_tmp() tmp := p.get_tmp()
tmp_val := p.cgen.cur_line.right(assign_pos) tmp_val := p.cgen.cur_line.right(assign_pos)
p.cgen.cur_line = p.cgen.cur_line.left(assign_pos) p.cgen.resetln(p.cgen.cur_line.left(assign_pos))
// val := p.cgen.end_tmp() // val := p.cgen.end_tmp()
if is_map { if is_map {
p.cgen.set_placeholder(fn_ph, 'map__set(&') p.cgen.set_placeholder(fn_ph, 'map__set(&')
@ -1841,7 +1841,7 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
// "m, 0" gets killed since we need to start from scratch. It's messy. // "m, 0" gets killed since we need to start from scratch. It's messy.
// "m, 0" is an index expression, save it before deleting and insert later in map_get() // "m, 0" is an index expression, save it before deleting and insert later in map_get()
index_expr := p.cgen.cur_line.right(fn_ph) index_expr := p.cgen.cur_line.right(fn_ph)
p.cgen.cur_line = p.cgen.cur_line.left(fn_ph) p.cgen.resetln(p.cgen.cur_line.left(fn_ph))
// Can't pass integer literal, because map_get() requires a void* // Can't pass integer literal, because map_get() requires a void*
tmp := p.get_tmp() tmp := p.get_tmp()
tmp_ok := p.get_tmp() tmp_ok := p.get_tmp()
@ -2342,7 +2342,7 @@ fn (p mut Parser) string_expr() {
cur_line := p.cgen.cur_line.trim_space() cur_line := p.cgen.cur_line.trim_space()
if cur_line.contains('println (') && p.tok != .plus && if cur_line.contains('println (') && p.tok != .plus &&
!cur_line.contains('string_add') && !cur_line.contains('eprintln') { !cur_line.contains('string_add') && !cur_line.contains('eprintln') {
p.cgen.cur_line = cur_line.replace('println (', 'printf(') p.cgen.resetln(cur_line.replace('println (', 'printf('))
p.gen('$format\\n$args') p.gen('$format\\n$args')
return return
} }
@ -2402,7 +2402,7 @@ fn (p mut Parser) array_init() string {
p.check(.rsbr) p.check(.rsbr)
name := p.check_name() name := p.check_name()
if p.table.known_type(name) { if p.table.known_type(name) {
p.cgen.cur_line = '' p.cgen.resetln('')
p.gen('{}') p.gen('{}')
return '[$lit]$name' return '[$lit]$name'
} }
@ -2428,7 +2428,7 @@ fn (p mut Parser) array_init() string {
p.check_space(.semicolon) p.check_space(.semicolon)
val := p.cgen.cur_line.right(pos) val := p.cgen.cur_line.right(pos)
// p.cgen.cur_line = '' // p.cgen.cur_line = ''
p.cgen.cur_line = p.cgen.cur_line.left(pos) p.cgen.resetln(p.cgen.cur_line.left(pos))
// Special case for zero // Special case for zero
if false && val.trim_space() == '0' { if false && val.trim_space() == '0' {
p.gen('array_repeat( & V_ZERO, ') p.gen('array_repeat( & V_ZERO, ')
@ -3197,12 +3197,12 @@ fn (p mut Parser) return_st() {
tmp := p.get_tmp() tmp := p.get_tmp()
ret := p.cgen.cur_line.right(ph) ret := p.cgen.cur_line.right(ph)
p.cgen.cur_line = '$expr_type $tmp = ($expr_type)($ret);' p.cgen.resetln('$expr_type $tmp = ($expr_type)($ret);')
p.gen('return opt_ok(&$tmp, sizeof($expr_type))') p.gen('return opt_ok(&$tmp, sizeof($expr_type))')
} }
else { else {
ret := p.cgen.cur_line.right(ph) ret := p.cgen.cur_line.right(ph)
p.cgen.cur_line = 'return $ret' p.cgen.resetln('return $ret')
} }
p.check_types(expr_type, p.cur_fn.typ) p.check_types(expr_type, p.cur_fn.typ)
} }