parser: optimize cross assign using expr.str() (#5604)

pull/5606/head
yuyi 2020-07-01 22:43:13 +08:00 committed by GitHub
parent 013a4fc0f6
commit 2f614ad79f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 24 deletions

View File

@ -410,7 +410,6 @@ pub:
pub struct IndexExpr { pub struct IndexExpr {
pub: pub:
pos token.Position pos token.Position
expr string // a[0] m['a'] etc
left Expr left Expr
index Expr // [0], [start..end] etc index Expr // [0], [start..end] etc
pub mut: pub mut:

View File

@ -1308,15 +1308,12 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
ast.IndexExpr { ast.IndexExpr {
mut has_var := false mut has_var := false
for lx in left { for lx in left {
if lx is ast.IndexExpr { if val_.str() == lx.str() {
inx := lx as ast.IndexExpr g.write('_var_${lx.position().pos}')
if val.expr == inx.expr {
g.write('_var_$inx.pos.pos')
has_var = true has_var = true
break break
} }
} }
}
if !has_var { if !has_var {
g.expr(val_) g.expr(val_)
} }

View File

@ -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 { fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool {
match val { val_ := val
match val_ {
ast.Ident { ast.Ident {
for expr in exprs { for expr in exprs {
if expr is ast.Ident { if expr is ast.Ident {
ident := expr as ast.Ident ident := expr as ast.Ident
if ident.name == val.name { if ident.name == val_.name {
return true return true
} }
} }
@ -57,17 +58,14 @@ fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool {
} }
ast.IndexExpr { ast.IndexExpr {
for expr in exprs { for expr in exprs {
if expr is ast.IndexExpr { if expr.str() == val.str() {
idx := expr as ast.IndexExpr
if idx.expr == val.expr {
return true return true
} }
} }
} }
} ast.InfixExpr { return p.check_cross_variables(exprs, val_.left) || p.check_cross_variables(exprs, val_.right) }
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.PrefixExpr { return p.check_cross_variables(exprs, val.right) } ast.PostfixExpr { return p.check_cross_variables(exprs, val_.expr) }
ast.PostfixExpr { return p.check_cross_variables(exprs, val.expr) }
else {} else {}
} }
return false return false

View File

@ -985,12 +985,10 @@ fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr {
} }
} }
// [expr] // [expr]
end := p.tok.position()
p.check(.rsbr) p.check(.rsbr)
return ast.IndexExpr{ return ast.IndexExpr{
left: left left: left
index: expr index: expr
expr: p.scanner.expr_string(left.position(), end).replace(' ', '')
pos: p.tok.position() pos: p.tok.position()
} }
} }

View File

@ -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()
}