From 2f614ad79f5fbdd44bcfc25c0bde4d488684f946 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 1 Jul 2020 22:43:13 +0800 Subject: [PATCH] parser: optimize cross assign using expr.str() (#5604) --- vlib/v/ast/ast.v | 1 - vlib/v/gen/cgen.v | 11 ++++------- vlib/v/parser/assign.v | 18 ++++++++---------- vlib/v/parser/parser.v | 2 -- vlib/v/scanner/scanner.v | 4 ---- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index e9b1ddcfac..272ea22c75 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -410,7 +410,6 @@ pub: pub struct IndexExpr { pub: pos token.Position - expr string // a[0] m['a'] etc left Expr index Expr // [0], [start..end] etc pub mut: diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 5c7ff2f3e7..c6713e1b83 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1308,13 +1308,10 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) { ast.IndexExpr { mut has_var := false for lx in left { - if lx is ast.IndexExpr { - inx := lx as ast.IndexExpr - if val.expr == inx.expr { - g.write('_var_$inx.pos.pos') - has_var = true - break - } + if val_.str() == lx.str() { + g.write('_var_${lx.position().pos}') + has_var = true + break } } if !has_var { diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index 18a105506a..ccf5075ba6 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -44,12 +44,13 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) { } fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool { - match val { + val_ := val + match val_ { ast.Ident { for expr in exprs { if expr is ast.Ident { ident := expr as ast.Ident - if ident.name == val.name { + if ident.name == val_.name { return true } } @@ -57,17 +58,14 @@ fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool { } ast.IndexExpr { for expr in exprs { - if expr is ast.IndexExpr { - idx := expr as ast.IndexExpr - if idx.expr == val.expr { - return true - } + if expr.str() == val.str() { + return true } } } - ast.InfixExpr { return p.check_cross_variables(exprs, val.left) || p.check_cross_variables(exprs, val.right) } - ast.PrefixExpr { return p.check_cross_variables(exprs, val.right) } - ast.PostfixExpr { return p.check_cross_variables(exprs, val.expr) } + ast.InfixExpr { return p.check_cross_variables(exprs, val_.left) || p.check_cross_variables(exprs, val_.right) } + ast.PrefixExpr { return p.check_cross_variables(exprs, val_.right) } + ast.PostfixExpr { return p.check_cross_variables(exprs, val_.expr) } else {} } return false diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d6ed7ea511..9f96bae31e 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -985,12 +985,10 @@ fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr { } } // [expr] - end := p.tok.position() p.check(.rsbr) return ast.IndexExpr{ left: left index: expr - expr: p.scanner.expr_string(left.position(), end).replace(' ', '') pos: p.tok.position() } } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 0745bd86d0..59dc376c8f 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -1379,7 +1379,3 @@ pub fn (mut s Scanner) codegen(newtext string) { } } } - -pub fn (s Scanner) expr_string(start, end token.Position) string { - return s.text[start.pos..end.pos].trim_space() -}