diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9211dc8d1d..13197fedd8 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -397,7 +397,10 @@ pub: pub struct EnumVal { pub: - name string + enum_name string + val string + pos token.Position + // name string } pub struct EnumDecl { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ae8a4cf95a..5cb3378e1e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -57,7 +57,7 @@ pub fn (c mut Checker) check_files(ast_files []ast.File) { c.stmt(*it) } else {} - } + } } } for file in ast_files { @@ -316,7 +316,6 @@ fn (c mut Checker) stmt(node ast.Stmt) { mut field := it.fields[i] typ := c.expr(expr) mut xconst := c.table.consts[field.name] - // if xconst.typ == 0 { xconst.typ = typ c.table.consts[field.name] = xconst @@ -366,6 +365,17 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type { ast.AssignExpr { c.check_assign_expr(it) } + ast.EnumVal { + typ_idx := c.table.find_type_idx(it.enum_name) // or { + typ := c.table.find_type(it.enum_name) or { + panic(err) + } + info := typ.info as table.Enum + if !(it.val in info.vals) { + c.error('enum `$it.enum_name` does not have a value `$it.val`', it.pos) + } + return typ_idx + } ast.FloatLiteral { return table.f64_type } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e94b3394cf..7409d6a326 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -226,6 +226,9 @@ fn (g mut Gen) expr(node ast.Expr) { ast.BoolLiteral { g.write(it.val.str()) } + ast.EnumVal { + g.write('${it.enum_name}_$it.val') + } ast.IntegerLiteral { g.write(it.val.str()) } diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 372241d9a0..4bc0324299 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -31,6 +31,7 @@ int main() { foo(3); int ak = 10; int mypi = pi; + Color color = Color_red; return 0; } diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index a7b3b59787..83b7adb1c5 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -37,8 +37,7 @@ fn main() { foo(3) ak := 10 mypi := pi - //color := Color.red - //Color color = Color_red; + color := Color.red } /* user := User{} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 09e1208b5c..73cc62212b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -577,12 +577,16 @@ pub fn (p mut Parser) name_expr() ast.Expr { // || p.table.known_type(p.tok.lit)) { return p.struct_init() } - /* - else if p.peek_tok.kind == .dot { - p.warn('enum val $name') + else if p.peek_tok.kind == .dot && p.tok.lit[0].is_capital() { + enum_name := p.check_name() + p.check(.dot) + val := p.check_name() + // println('enum val $enum_name . $val') + return ast.EnumVal{ + enum_name: enum_name + val: val + } } - */ - else { mut ident := ast.Ident{} ident = p.parse_ident(is_c) @@ -907,10 +911,10 @@ fn (p &Parser) is_addative() bool { // `pref.BuildMode.default_mode` fn (p mut Parser) enum_val() (ast.Expr,table.Type) { p.check(.dot) - name := p.check_name() + val := p.check_name() mut node := ast.Expr{} node = ast.EnumVal{ - name: name + val: val } return node,table.int_type } diff --git a/vlib/v/table/atype_symbols.v b/vlib/v/table/atype_symbols.v index 6e1f1cebad..7074d3b2ad 100644 --- a/vlib/v/table/atype_symbols.v +++ b/vlib/v/table/atype_symbols.v @@ -367,7 +367,7 @@ pub mut: pub struct Enum { pub mut: - vals []Field + vals []string } pub struct Alias {