v2: enum fixes

pull/3840/head
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 {
g.writeln('enum $it.name {')
g.writeln('typedef enum {')
for i, val in it.vals {
g.writeln('\t${it.name}_$val, // $i')
}
g.writeln('}')
g.writeln('} $it.name;')
}
ast.Import {}
ast.FnDecl {

View File

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

View File

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

View File

@ -577,6 +577,12 @@ 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 {
mut ident := ast.Ident{}
ident = p.parse_ident(is_c)
@ -1649,6 +1655,13 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
}
}
p.check(.rcbr)
p.table.register_type_symbol(table.TypeSymbol{
kind: .enum_
name: name
info: table.Enum{
vals: vals
}
})
return ast.EnumDecl{
name: name
is_pub: is_pub

View File

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