From b3143bb5597c87c0a6fc3a52d55242f1ad7fa08a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 11 Nov 2019 05:22:34 +0300 Subject: [PATCH] parser: remove start_tmp() entirely --- vlib/compiler/cgen.v | 33 ++++++++++++++++++--------------- vlib/compiler/match.v | 5 +---- vlib/compiler/parser.v | 7 ------- vlib/compiler/query.v | 9 +++------ 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index 80df1ef54f..e9afb1d09f 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -105,25 +105,28 @@ fn (g mut CGen) save() { g.out.close() } -fn (g mut CGen) start_tmp() { - if g.is_tmp { - g.prev_tmps << g.tmp_line + +// returns expression's type, and entire expression's string representation) +fn (p mut Parser) tmp_expr() (string, string) { + // former start_tmp() + if p.cgen.is_tmp { + p.cgen.prev_tmps << p.cgen.tmp_line } // kg.tmp_lines_pos++ - g.tmp_line = '' - g.is_tmp = true -} - -fn (g mut CGen) end_tmp() string { - res := g.tmp_line - if g.prev_tmps.len > 0 { - g.tmp_line = g.prev_tmps.last() - g.prev_tmps = g.prev_tmps[0..g.prev_tmps.len-1] + p.cgen.tmp_line = '' + p.cgen.is_tmp = true + // + typ := p.bool_expression() + + res := p.cgen.tmp_line + if p.cgen.prev_tmps.len > 0 { + p.cgen.tmp_line = p.cgen.prev_tmps.last() + p.cgen.prev_tmps = p.cgen.prev_tmps[0..p.cgen.prev_tmps.len-1] } else { - g.tmp_line = '' - g.is_tmp = false + p.cgen.tmp_line = '' + p.cgen.is_tmp = false } - return res + return typ, res } fn (g &CGen) add_placeholder() int { diff --git a/vlib/compiler/match.v b/vlib/compiler/match.v index 9cb1fc8d7c..d3848bbefa 100644 --- a/vlib/compiler/match.v +++ b/vlib/compiler/match.v @@ -12,13 +12,10 @@ import ( fn (p mut Parser) match_statement(is_expr bool) string { p.check(.key_match) p.fspace() - p.cgen.start_tmp() - typ := p.bool_expression() + typ, expr := p.tmp_expr() if typ.starts_with('array_') { p.error('arrays cannot be compared') } - expr := p.cgen.end_tmp() - // is it safe to use p.cgen.insert_before ??? tmp_var := p.get_tmp() p.cgen.insert_before('$typ $tmp_var = $expr;') diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 7abb36a8a5..831d09de17 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -2450,13 +2450,6 @@ fn (p mut Parser) get_tmp_counter() int { return p.tmp_cnt } -// returns expression's type, and entire expression's string representation) -fn (p mut Parser) tmp_expr() (string, string) { - p.cgen.start_tmp() - typ := p.bool_expression() - val := p.cgen.end_tmp() - return typ, val -} fn (p mut Parser) if_st(is_expr bool, elif_depth int) string { if is_expr { diff --git a/vlib/compiler/query.v b/vlib/compiler/query.v index 19f1a89f2d..ba6ae39d6b 100644 --- a/vlib/compiler/query.v +++ b/vlib/compiler/query.v @@ -90,21 +90,18 @@ fn (p mut Parser) select_query(fn_ph int) string { // `where` statement if p.tok == .name && p.lit == 'where' { p.next() - p.cgen.start_tmp() + _, expr := p.tmp_expr() p.is_sql = true - p.bool_expression() p.is_sql = false - q += ' where ' + p.cgen.end_tmp() + q += ' where ' + expr } // limit? mut query_one := false if p.tok == .name && p.lit == 'limit' { p.next() - p.cgen.start_tmp() + _, limit := p.tmp_expr() p.is_sql = true - p.bool_expression() p.is_sql = false - limit := p.cgen.end_tmp() q += ' limit ' + limit // `limit 1` means we are getting `?User`, not `[]User` if limit.trim_space() == '1' {