From d38acb5487b184d99ab6d55e488a2bc902e7e449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 8 Apr 2020 18:55:10 +0200 Subject: [PATCH] cgen: fix enum print case --- .gitignore | 1 + vlib/v/gen/cgen.v | 6 ++---- vlib/v/parser/parser.v | 1 - vlib/v/table/atypes.v | 3 +-- vlib/v/tests/inout/enum_print.out | 1 + vlib/v/tests/inout/enum_print.v | 21 --------------------- vlib/v/tests/inout/enum_print.vv | 5 ++++- 7 files changed, 9 insertions(+), 29 deletions(-) delete mode 100644 vlib/v/tests/inout/enum_print.v diff --git a/.gitignore b/.gitignore index 99c972d172..7f8501b91c 100644 --- a/.gitignore +++ b/.gitignore @@ -86,3 +86,4 @@ exe vlib/v/tests/inout/hello.v vlib/v/tests/inout/hello_devs.v vlib/v/tests/inout/os.v +vlib/v/tests/inout/enum_print.v diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 4f11e163c1..94ddcdcb47 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -3047,10 +3047,8 @@ fn (g mut Gen) gen_str_for_type(sym table.TypeSymbol, styp string) { fn (g mut Gen) gen_str_for_enum(info table.Enum, styp string) { s := styp.replace('.', '__') g.definitions.write('string ${s}_str($styp a) {\n\tswitch(a) {\n') - for i, expr in info.default_exprs { - val := info.vals[i] - int_expr := expr as ast.IntegerLiteral - g.definitions.write('\t\tcase $int_expr.val: return tos3("$val");\n') + for i, val in info.vals { + g.definitions.write('\t\tcase ${s}_$val: return tos3("$val");\n') } g.definitions.write('\t\tdefault: return tos3("unknown enum value"); } }\n') } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 32d38ac850..54b1bcf04a 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1879,7 +1879,6 @@ fn (p mut Parser) enum_decl() ast.EnumDecl { name: name info: table.Enum{ vals: vals - default_exprs: default_exprs } }) return ast.EnumDecl{ diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 13ee5a7a60..2e877b00ce 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -545,9 +545,8 @@ pub mut: } pub struct Enum { -pub mut: +pub: vals []string - default_exprs []ast.Expr } pub struct Alias { diff --git a/vlib/v/tests/inout/enum_print.out b/vlib/v/tests/inout/enum_print.out index 16b698f351..362ee21355 100644 --- a/vlib/v/tests/inout/enum_print.out +++ b/vlib/v/tests/inout/enum_print.out @@ -1,3 +1,4 @@ +orange yellow green green diff --git a/vlib/v/tests/inout/enum_print.v b/vlib/v/tests/inout/enum_print.v deleted file mode 100644 index 4f38d6bc48..0000000000 --- a/vlib/v/tests/inout/enum_print.v +++ /dev/null @@ -1,21 +0,0 @@ -module main - -enum Color { - green = 5 - red = 2 - yellow = 1 -} - -struct A{ - color Color -} - -fn main() { - col := Color.green - a := A{color: col} - println(Color.yellow) - println(col) - println(a.color) - println('interp: ${col}') - println('interp: ${a.color}') -} diff --git a/vlib/v/tests/inout/enum_print.vv b/vlib/v/tests/inout/enum_print.vv index 4f38d6bc48..77bb301350 100644 --- a/vlib/v/tests/inout/enum_print.vv +++ b/vlib/v/tests/inout/enum_print.vv @@ -1,9 +1,10 @@ module main enum Color { + orange green = 5 red = 2 - yellow = 1 + yellow = 3 } struct A{ @@ -13,6 +14,8 @@ struct A{ fn main() { col := Color.green a := A{color: col} + orange := Color.orange + println(orange) println(Color.yellow) println(col) println(a.color)