diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index ffc4231f67..88e01a2e39 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -1358,6 +1358,7 @@ fn (t Tree) postfix_expr(node ast.PostfixExpr) &Node { obj.add_terse('expr', t.expr(node.expr)) obj.add('auto_locked', t.string_node(node.auto_locked)) obj.add('pos', t.pos(node.pos)) + obj.add('is_c2v_prefix', t.bool_node(node.is_c2v_prefix)) return obj } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 658d3cc599..33a4340a55 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -862,8 +862,9 @@ pub mut: // ++, -- pub struct PostfixExpr { pub: - op token.Kind - pos token.Pos + op token.Kind + pos token.Pos + is_c2v_prefix bool // for `--x` (`x--$`), only for translated code until c2v can handle it pub mut: expr Expr auto_locked string diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index fc91352e42..58935b7cf1 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2387,6 +2387,9 @@ pub fn (mut f Fmt) postfix_expr(node ast.PostfixExpr) { } else { f.write('$node.op') } + if node.is_c2v_prefix { + f.write('$') + } } pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 31c9bc013b..c7e16f5a5b 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3040,6 +3040,9 @@ fn (mut g Gen) expr(node_ ast.Expr) { g.writeln('sync__RwMutex_lock(&$node.auto_locked->mtx);') } g.inside_map_postfix = true + if node.is_c2v_prefix { + g.write(node.op.str()) + } if node.expr.is_auto_deref_var() { g.write('(*') g.expr(node.expr) @@ -3048,7 +3051,9 @@ fn (mut g Gen) expr(node_ ast.Expr) { g.expr(node.expr) } g.inside_map_postfix = false - g.write(node.op.str()) + if !node.is_c2v_prefix { + g.write(node.op.str()) + } if node.auto_locked != '' { g.writeln(';') g.write('sync__RwMutex_unlock(&$node.auto_locked->mtx)') diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 3fddc0440b..63ce4e7dbc 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -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 { node.recursive_mapset_is_setter(true) } + is_c2v_prefix := p.peek_tok.kind == .dollar node = ast.PostfixExpr{ op: p.tok.kind expr: node pos: p.tok.pos() + is_c2v_prefix: is_c2v_prefix + } + if is_c2v_prefix { + p.next() } p.next() // return node // TODO bring back, only allow ++/-- in exprs in translated code