mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check enums
This commit is contained in:
parent
05ed6c57b6
commit
28ee0f4ebe
@ -397,7 +397,10 @@ pub:
|
||||
|
||||
pub struct EnumVal {
|
||||
pub:
|
||||
name string
|
||||
enum_name string
|
||||
val string
|
||||
pos token.Position
|
||||
// name string
|
||||
}
|
||||
|
||||
pub struct EnumDecl {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ int main() {
|
||||
foo(3);
|
||||
int ak = 10;
|
||||
int mypi = pi;
|
||||
Color color = Color_red;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -37,8 +37,7 @@ fn main() {
|
||||
foo(3)
|
||||
ak := 10
|
||||
mypi := pi
|
||||
//color := Color.red
|
||||
//Color color = Color_red;
|
||||
color := Color.red
|
||||
}
|
||||
/*
|
||||
user := User{}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -367,7 +367,7 @@ pub mut:
|
||||
|
||||
pub struct Enum {
|
||||
pub mut:
|
||||
vals []Field
|
||||
vals []string
|
||||
}
|
||||
|
||||
pub struct Alias {
|
||||
|
Loading…
Reference in New Issue
Block a user