From 8ffbcdc55341d8559e43edcdfb9dcd4170a019f0 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 22 Oct 2020 19:48:06 +0300 Subject: [PATCH] cgen: extract g.new_ctemp_var/2, g.new_ctemp_var_then_gen/2 and g.gen_ctemp_var from gen_assert_stmt --- vlib/v/gen/cgen.v | 26 ++------------------------ vlib/v/gen/ctempvars.v | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 vlib/v/gen/ctempvars.v diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index b3a06dba0d..5249ca8f60 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1275,35 +1275,13 @@ fn (mut g Gen) gen_attrs(attrs []table.Attr) { fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) { mut a := original_assert_statement g.writeln('// assert') - mut tl_name := '' - mut tr_name := '' if a.expr is ast.InfixExpr { mut aie := a.expr as ast.InfixExpr if aie.left is ast.CallExpr { - tl_styp := g.typ(aie.left_type) - tl_name = g.new_tmp_var() - g.write('$tl_styp $tl_name = ') - g.expr(aie.left) - g.writeln(';') - aie.left = ast.Expr(ast.CTempVar{ - name: tl_name - typ: aie.left_type - is_ptr: aie.left_type.is_ptr() - orig: aie.left - }) + aie.left = g.new_ctemp_var_then_gen(aie.left, aie.left_type) } if aie.right is ast.CallExpr { - tr_styp := g.typ(aie.right_type) - tr_name = g.new_tmp_var() - g.write('$tr_styp $tr_name = ') - g.expr(aie.right) - g.writeln(';') - aie.right = ast.Expr(ast.CTempVar{ - name: tr_name - typ: aie.right_type - is_ptr: aie.right_type.is_ptr() - orig: aie.right - }) + aie.right = g.new_ctemp_var_then_gen(aie.right, aie.right_type) } } g.inside_ternary++ diff --git a/vlib/v/gen/ctempvars.v b/vlib/v/gen/ctempvars.v new file mode 100644 index 0000000000..b2dc39f655 --- /dev/null +++ b/vlib/v/gen/ctempvars.v @@ -0,0 +1,26 @@ +module gen + +import v.ast +import v.table + +fn (mut g Gen) new_ctemp_var(expr ast.Expr, expr_type table.Type) ast.CTempVar { + return ast.CTempVar{ + name: g.new_tmp_var() + typ: expr_type + is_ptr: expr_type.is_ptr() + orig: expr + } +} + +fn (mut g Gen) new_ctemp_var_then_gen(expr ast.Expr, expr_type table.Type) ast.CTempVar { + x := g.new_ctemp_var(expr, expr_type) + g.gen_ctemp_var(x) + return x +} + +fn (mut g Gen) gen_ctemp_var(tvar ast.CTempVar) { + styp := g.typ(tvar.typ) + g.write('$styp $tvar.name = ') + g.expr(tvar.orig) + g.writeln(';') +}