1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v2: enum fixes

This commit is contained in:
Alexander Medvednikov 2020-02-25 13:30:43 +01:00
parent 7e930c2a75
commit f6c2b3a54b
5 changed files with 27 additions and 7 deletions

View File

@ -69,11 +69,11 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
} }
ast.EnumDecl { ast.EnumDecl {
g.writeln('enum $it.name {') g.writeln('typedef enum {')
for i, val in it.vals { for i, val in it.vals {
g.writeln('\t${it.name}_$val, // $i') g.writeln('\t${it.name}_$val, // $i')
} }
g.writeln('}') g.writeln('} $it.name;')
} }
ast.Import {} ast.Import {}
ast.FnDecl { ast.FnDecl {

View File

@ -15,11 +15,11 @@ typedef struct {
int age; int age;
} User; } User;
enum Color { typedef enum {
Color_red, // 0 Color_red, // 0
Color_green, // 1 Color_green, // 1
Color_blue, // 2 Color_blue, // 2
} } Color;
int main() { int main() {
int a = 10; int a = 10;

View File

@ -37,6 +37,8 @@ fn main() {
foo(3) foo(3)
ak := 10 ak := 10
mypi := pi mypi := pi
//color := Color.red
//Color color = Color_red;
} }
/* /*
user := User{} user := User{}

View File

@ -577,6 +577,12 @@ pub fn (p mut Parser) name_expr() ast.Expr {
// || p.table.known_type(p.tok.lit)) { // || p.table.known_type(p.tok.lit)) {
return p.struct_init() return p.struct_init()
} }
/*
else if p.peek_tok.kind == .dot {
p.warn('enum val $name')
}
*/
else { else {
mut ident := ast.Ident{} mut ident := ast.Ident{}
ident = p.parse_ident(is_c) ident = p.parse_ident(is_c)
@ -1649,6 +1655,13 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
} }
} }
p.check(.rcbr) p.check(.rcbr)
p.table.register_type_symbol(table.TypeSymbol{
kind: .enum_
name: name
info: table.Enum{
vals: vals
}
})
return ast.EnumDecl{ return ast.EnumDecl{
name: name name: name
is_pub: is_pub is_pub: is_pub

View File

@ -7,8 +7,8 @@ import (
strings strings
) )
pub type TypeInfo = Array | ArrayFixed | Map | Struct | pub type TypeInfo = Array | ArrayFixed | Map | Struct |
MultiReturn | Alias MultiReturn | Alias | Enum
pub struct TypeSymbol { pub struct TypeSymbol {
pub: pub:
@ -86,6 +86,7 @@ pub enum Kind {
multi_return multi_return
sum_type sum_type
alias alias
enum_
} }
[inline] [inline]
@ -364,6 +365,11 @@ pub mut:
fields []Field fields []Field
} }
pub struct Enum {
pub mut:
vals []Field
}
pub struct Alias { pub struct Alias {
pub: pub:
foo string foo string
@ -411,7 +417,6 @@ pub fn (table &Table) type_to_str(t Type) string {
res += ')' res += ')'
return res return res
} }
mut res := sym.name.replace('array_', '[]') mut res := sym.name.replace('array_', '[]')
// mod.submod.submod2.Type => submod2.Type // mod.submod.submod2.Type => submod2.Type
if res.contains('.') { if res.contains('.') {