From 77c18f443551d90b72d65a1fb47338118c50f9f6 Mon Sep 17 00:00:00 2001 From: Wertzui123 <46199283+Wertzui123@users.noreply.github.com> Date: Wed, 6 Oct 2021 13:32:42 +0200 Subject: [PATCH] cgen: fix alphabetic order of ast statement generation (#12080) --- vlib/v/gen/c/cgen.v | 74 ++++++++++++++++++++++----------------------- vlib/v/gen/js/js.v | 57 +++++++++++++++++----------------- 2 files changed, 66 insertions(+), 65 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index e52e5f13a1..932d827aaf 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3622,6 +3622,9 @@ fn (mut g Gen) expr(node ast.Expr) { ast.Assoc { g.assoc(node) } + ast.AtExpr { + g.comp_at(node) + } ast.BoolLiteral { g.write(node.val.str()) } @@ -3711,19 +3714,13 @@ fn (mut g Gen) expr(node ast.Expr) { } } } - ast.DumpExpr { - g.dump_expr(node) - } - ast.AtExpr { - g.comp_at(node) - } + ast.Comment {} ast.ComptimeCall { g.comptime_call(node) } ast.ComptimeSelector { g.comptime_selector(node) } - ast.Comment {} ast.ConcatExpr { g.concat_expr(node) } @@ -3731,6 +3728,9 @@ fn (mut g Gen) expr(node ast.Expr) { // g.write('/*ctmp .orig: $node.orig.str() , ._typ: $node.typ, .is_ptr: $node.is_ptr */ ') g.write(node.name) } + ast.DumpExpr { + g.dump_expr(node) + } ast.EnumVal { // g.write('${it.mod}${it.enum_name}_$it.val') // g.enum_expr(node) @@ -3779,19 +3779,43 @@ fn (mut g Gen) expr(node ast.Expr) { g.write(node.val) // .int().str()) } } + ast.IsRefType { + typ := if node.typ == g.field_data_type { g.comp_for_field_value.typ } else { node.typ } + node_typ := g.unwrap_generic(typ) + sym := g.table.get_type_symbol(node_typ) + if sym.language == .v && sym.kind in [.placeholder, .any] { + g.error('unknown type `$sym.name`', node.pos) + } + is_ref_type := g.contains_ptr(node_typ) + g.write('/*IsRefType*/ $is_ref_type') + } + ast.Likely { + if node.is_likely { + g.write('_likely_') + } else { + g.write('_unlikely_') + } + g.write('(') + g.expr(node.expr) + g.write(')') + } ast.LockExpr { g.lock_expr(node) } - ast.MatchExpr { - g.match_expr(node) - } ast.MapInit { g.map_init(node) } + ast.MatchExpr { + g.match_expr(node) + } ast.NodeError {} ast.None { g.write('_const_none__') } + ast.OffsetOf { + styp := g.typ(node.struct_type) + g.write('/*OffsetOf*/ (u32)(__offsetof(${util.no_dots(styp)}, $node.field))') + } ast.OrExpr { // this should never appear here } @@ -3872,6 +3896,9 @@ fn (mut g Gen) expr(node ast.Expr) { ast.SelectExpr { g.select_expr(node) } + ast.SelectorExpr { + g.selector_expr(node) + } ast.SizeOf { typ := if node.typ == g.field_data_type { g.comp_for_field_value.typ } else { node.typ } node_typ := g.unwrap_generic(typ) @@ -3882,20 +3909,6 @@ fn (mut g Gen) expr(node ast.Expr) { styp := g.typ(node_typ) g.write('sizeof(${util.no_dots(styp)})') } - ast.IsRefType { - typ := if node.typ == g.field_data_type { g.comp_for_field_value.typ } else { node.typ } - node_typ := g.unwrap_generic(typ) - sym := g.table.get_type_symbol(node_typ) - if sym.language == .v && sym.kind in [.placeholder, .any] { - g.error('unknown type `$sym.name`', node.pos) - } - is_ref_type := g.contains_ptr(node_typ) - g.write('/*IsRefType*/ $is_ref_type') - } - ast.OffsetOf { - styp := g.typ(node.struct_type) - g.write('/*OffsetOf*/ (u32)(__offsetof(${util.no_dots(styp)}, $node.field))') - } ast.SqlExpr { g.sql_select_expr(node) } @@ -3913,9 +3926,6 @@ fn (mut g Gen) expr(node ast.Expr) { g.struct_init(node) } } - ast.SelectorExpr { - g.selector_expr(node) - } ast.TypeNode { // match sum Type // g.write('/* Type */') @@ -3929,16 +3939,6 @@ fn (mut g Gen) expr(node ast.Expr) { ast.TypeOf { g.typeof_expr(node) } - ast.Likely { - if node.is_likely { - g.write('_likely_') - } else { - g.write('_unlikely_') - } - g.write('(') - g.expr(node.expr) - g.write(')') - } ast.UnsafeExpr { g.expr(node.expr) } diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index b45ca76e8c..4af8632679 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -792,18 +792,13 @@ fn (mut g JsGen) stmt(node ast.Stmt) { } fn (mut g JsGen) expr(node ast.Expr) { + // NB: please keep the type names in the match here in alphabetical order: match node { - ast.NodeError {} ast.EmptyExpr {} - ast.CTempVar { - g.write('/* ast.CTempVar: node.name */') - } - ast.DumpExpr { - g.write('/* ast.DumpExpr: $node.expr */') - } ast.AnonFn { g.gen_fn_decl(node.decl) } + ast.ArrayDecompose {} ast.ArrayInit { g.gen_array_init_expr(node) } @@ -814,6 +809,9 @@ fn (mut g JsGen) expr(node ast.Expr) { ast.Assoc { // TODO } + ast.AtExpr { + g.write('"$node.val"') + } ast.BoolLiteral { g.write('new bool(') if node.val == true { @@ -826,12 +824,12 @@ fn (mut g JsGen) expr(node ast.Expr) { ast.CallExpr { g.gen_call_expr(node) } - ast.ChanInit { - // TODO - } ast.CastExpr { g.gen_type_cast_expr(node) } + ast.ChanInit { + // TODO + } ast.CharLiteral { if utf8_str_len(node.val) < node.val.len { g.write("new rune('$node.val'.charCodeAt())") @@ -840,9 +838,21 @@ fn (mut g JsGen) expr(node ast.Expr) { } } ast.Comment {} + ast.ComptimeCall { + // TODO + } + ast.ComptimeSelector { + // TODO + } ast.ConcatExpr { // TODO } + ast.CTempVar { + g.write('/* ast.CTempVar: node.name */') + } + ast.DumpExpr { + g.write('/* ast.DumpExpr: $node.expr */') + } ast.EnumVal { sym := g.table.get_type_symbol(node.typ) styp := g.js_name(sym.name) @@ -872,15 +882,21 @@ fn (mut g JsGen) expr(node ast.Expr) { ast.IntegerLiteral { g.gen_integer_literal_expr(node) } + ast.Likely { + g.write('(') + g.expr(node.expr) + g.write(')') + } ast.LockExpr { g.gen_lock_expr(node) } - ast.MapInit { - g.gen_map_init_expr(node) - } + ast.NodeError {} ast.None { g.write('none__') } + ast.MapInit { + g.gen_map_init_expr(node) + } ast.MatchExpr { g.match_expr(node) } @@ -982,28 +998,13 @@ fn (mut g JsGen) expr(node ast.Expr) { g.write('${g.js_name(sym.name)}') } - ast.Likely { - g.write('(') - g.expr(node.expr) - g.write(')') - } ast.TypeOf { g.gen_typeof_expr(node) // TODO: Should this print the V type or the JS type? } - ast.AtExpr { - g.write('"$node.val"') - } - ast.ComptimeCall { - // TODO - } - ast.ComptimeSelector { - // TODO - } ast.UnsafeExpr { g.expr(node.expr) } - ast.ArrayDecompose {} } }