parser, cgen: temporary prefix ++ for translated code

master
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('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
}

View File

@ -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

View File

@ -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) {

View File

@ -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)')

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 {
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