cgen: fix enum print case
parent
3fd547f2e5
commit
d38acb5487
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -545,9 +545,8 @@ pub mut:
|
|||
}
|
||||
|
||||
pub struct Enum {
|
||||
pub mut:
|
||||
pub:
|
||||
vals []string
|
||||
default_exprs []ast.Expr
|
||||
}
|
||||
|
||||
pub struct Alias {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
orange
|
||||
yellow
|
||||
green
|
||||
green
|
||||
|
|
|
@ -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}')
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue