1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser, cgen: temporary prefix ++ for translated code

This commit is contained in:
Alexander Medvednikov 2022-06-18 13:30:33 +03:00
parent 18dfaf6164
commit 10051e005a
5 changed files with 18 additions and 3 deletions

View File

@ -1358,6 +1358,7 @@ fn (t Tree) postfix_expr(node ast.PostfixExpr) &Node {
obj.add_terse('expr', t.expr(node.expr)) obj.add_terse('expr', t.expr(node.expr))
obj.add('auto_locked', t.string_node(node.auto_locked)) obj.add('auto_locked', t.string_node(node.auto_locked))
obj.add('pos', t.pos(node.pos)) obj.add('pos', t.pos(node.pos))
obj.add('is_c2v_prefix', t.bool_node(node.is_c2v_prefix))
return obj return obj
} }

View File

@ -862,8 +862,9 @@ pub mut:
// ++, -- // ++, --
pub struct PostfixExpr { pub struct PostfixExpr {
pub: pub:
op token.Kind op token.Kind
pos token.Pos pos token.Pos
is_c2v_prefix bool // for `--x` (`x--$`), only for translated code until c2v can handle it
pub mut: pub mut:
expr Expr expr Expr
auto_locked string auto_locked string

View File

@ -2387,6 +2387,9 @@ pub fn (mut f Fmt) postfix_expr(node ast.PostfixExpr) {
} else { } else {
f.write('$node.op') f.write('$node.op')
} }
if node.is_c2v_prefix {
f.write('$')
}
} }
pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) { pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {

View File

@ -3040,6 +3040,9 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.writeln('sync__RwMutex_lock(&$node.auto_locked->mtx);') g.writeln('sync__RwMutex_lock(&$node.auto_locked->mtx);')
} }
g.inside_map_postfix = true g.inside_map_postfix = true
if node.is_c2v_prefix {
g.write(node.op.str())
}
if node.expr.is_auto_deref_var() { if node.expr.is_auto_deref_var() {
g.write('(*') g.write('(*')
g.expr(node.expr) g.expr(node.expr)
@ -3048,7 +3051,9 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.expr(node.expr) g.expr(node.expr)
} }
g.inside_map_postfix = false g.inside_map_postfix = false
g.write(node.op.str()) if !node.is_c2v_prefix {
g.write(node.op.str())
}
if node.auto_locked != '' { if node.auto_locked != '' {
g.writeln(';') g.writeln(';')
g.write('sync__RwMutex_unlock(&$node.auto_locked->mtx)') g.write('sync__RwMutex_unlock(&$node.auto_locked->mtx)')

View File

@ -467,10 +467,15 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden
if mut node is ast.IndexExpr { if mut node is ast.IndexExpr {
node.recursive_mapset_is_setter(true) node.recursive_mapset_is_setter(true)
} }
is_c2v_prefix := p.peek_tok.kind == .dollar
node = ast.PostfixExpr{ node = ast.PostfixExpr{
op: p.tok.kind op: p.tok.kind
expr: node expr: node
pos: p.tok.pos() pos: p.tok.pos()
is_c2v_prefix: is_c2v_prefix
}
if is_c2v_prefix {
p.next()
} }
p.next() p.next()
// return node // TODO bring back, only allow ++/-- in exprs in translated code // return node // TODO bring back, only allow ++/-- in exprs in translated code