cgen: fix enum print case

pull/4286/head^2
Daniel Däschle 2020-04-08 18:55:10 +02:00 committed by GitHub
parent 3fd547f2e5
commit d38acb5487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 9 additions and 29 deletions

1
.gitignore vendored
View File

@ -86,3 +86,4 @@ exe
vlib/v/tests/inout/hello.v vlib/v/tests/inout/hello.v
vlib/v/tests/inout/hello_devs.v vlib/v/tests/inout/hello_devs.v
vlib/v/tests/inout/os.v vlib/v/tests/inout/os.v
vlib/v/tests/inout/enum_print.v

View File

@ -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) { fn (g mut Gen) gen_str_for_enum(info table.Enum, styp string) {
s := styp.replace('.', '__') s := styp.replace('.', '__')
g.definitions.write('string ${s}_str($styp a) {\n\tswitch(a) {\n') g.definitions.write('string ${s}_str($styp a) {\n\tswitch(a) {\n')
for i, expr in info.default_exprs { for i, val in info.vals {
val := info.vals[i] g.definitions.write('\t\tcase ${s}_$val: return tos3("$val");\n')
int_expr := expr as ast.IntegerLiteral
g.definitions.write('\t\tcase $int_expr.val: return tos3("$val");\n')
} }
g.definitions.write('\t\tdefault: return tos3("unknown enum value"); } }\n') g.definitions.write('\t\tdefault: return tos3("unknown enum value"); } }\n')
} }

View File

@ -1879,7 +1879,6 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
name: name name: name
info: table.Enum{ info: table.Enum{
vals: vals vals: vals
default_exprs: default_exprs
} }
}) })
return ast.EnumDecl{ return ast.EnumDecl{

View File

@ -545,9 +545,8 @@ pub mut:
} }
pub struct Enum { pub struct Enum {
pub mut: pub:
vals []string vals []string
default_exprs []ast.Expr
} }
pub struct Alias { pub struct Alias {

View File

@ -1,3 +1,4 @@
orange
yellow yellow
green green
green green

View File

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

View File

@ -1,9 +1,10 @@
module main module main
enum Color { enum Color {
orange
green = 5 green = 5
red = 2 red = 2
yellow = 1 yellow = 3
} }
struct A{ struct A{
@ -13,6 +14,8 @@ struct A{
fn main() { fn main() {
col := Color.green col := Color.green
a := A{color: col} a := A{color: col}
orange := Color.orange
println(orange)
println(Color.yellow) println(Color.yellow)
println(col) println(col)
println(a.color) println(a.color)