checker: check enums

pull/3840/head
Alexander Medvednikov 2020-02-25 15:02:34 +01:00
parent 05ed6c57b6
commit 28ee0f4ebe
7 changed files with 33 additions and 13 deletions

View File

@ -397,7 +397,10 @@ pub:
pub struct EnumVal { pub struct EnumVal {
pub: pub:
name string enum_name string
val string
pos token.Position
// name string
} }
pub struct EnumDecl { pub struct EnumDecl {

View File

@ -57,7 +57,7 @@ pub fn (c mut Checker) check_files(ast_files []ast.File) {
c.stmt(*it) c.stmt(*it)
} }
else {} else {}
} }
} }
} }
for file in ast_files { for file in ast_files {
@ -316,7 +316,6 @@ fn (c mut Checker) stmt(node ast.Stmt) {
mut field := it.fields[i] mut field := it.fields[i]
typ := c.expr(expr) typ := c.expr(expr)
mut xconst := c.table.consts[field.name] mut xconst := c.table.consts[field.name]
// if xconst.typ == 0 { // if xconst.typ == 0 {
xconst.typ = typ xconst.typ = typ
c.table.consts[field.name] = xconst c.table.consts[field.name] = xconst
@ -366,6 +365,17 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
ast.AssignExpr { ast.AssignExpr {
c.check_assign_expr(it) 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 { ast.FloatLiteral {
return table.f64_type return table.f64_type
} }

View File

@ -226,6 +226,9 @@ fn (g mut Gen) expr(node ast.Expr) {
ast.BoolLiteral { ast.BoolLiteral {
g.write(it.val.str()) g.write(it.val.str())
} }
ast.EnumVal {
g.write('${it.enum_name}_$it.val')
}
ast.IntegerLiteral { ast.IntegerLiteral {
g.write(it.val.str()) g.write(it.val.str())
} }

View File

@ -31,6 +31,7 @@ int main() {
foo(3); foo(3);
int ak = 10; int ak = 10;
int mypi = pi; int mypi = pi;
Color color = Color_red;
return 0; return 0;
} }

View File

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

View File

@ -577,12 +577,16 @@ 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.tok.lit[0].is_capital() {
else if p.peek_tok.kind == .dot { enum_name := p.check_name()
p.warn('enum val $name') p.check(.dot)
val := p.check_name()
// println('enum val $enum_name . $val')
return ast.EnumVal{
enum_name: enum_name
val: val
}
} }
*/
else { else {
mut ident := ast.Ident{} mut ident := ast.Ident{}
ident = p.parse_ident(is_c) ident = p.parse_ident(is_c)
@ -907,10 +911,10 @@ fn (p &Parser) is_addative() bool {
// `pref.BuildMode.default_mode` // `pref.BuildMode.default_mode`
fn (p mut Parser) enum_val() (ast.Expr,table.Type) { fn (p mut Parser) enum_val() (ast.Expr,table.Type) {
p.check(.dot) p.check(.dot)
name := p.check_name() val := p.check_name()
mut node := ast.Expr{} mut node := ast.Expr{}
node = ast.EnumVal{ node = ast.EnumVal{
name: name val: val
} }
return node,table.int_type return node,table.int_type
} }

View File

@ -367,7 +367,7 @@ pub mut:
pub struct Enum { pub struct Enum {
pub mut: pub mut:
vals []Field vals []string
} }
pub struct Alias { pub struct Alias {