2019-10-20 19:59:53 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module compiler
|
|
|
|
|
2019-11-06 04:43:13 +03:00
|
|
|
fn (p mut Parser) enum_decl(no_name bool) {
|
2019-10-24 13:19:27 +03:00
|
|
|
is_pub := p.tok == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
2019-12-05 14:09:33 +03:00
|
|
|
}
|
2019-11-06 04:43:13 +03:00
|
|
|
p.check(.key_enum)
|
2019-11-10 03:08:53 +03:00
|
|
|
p.fspace()
|
2019-11-06 04:43:13 +03:00
|
|
|
mut enum_name := p.check_name()
|
|
|
|
is_c := enum_name == 'C' && p.tok == .dot
|
|
|
|
if is_c {
|
|
|
|
p.check(.dot)
|
|
|
|
enum_name = p.check_name()
|
|
|
|
}
|
2019-10-20 19:59:53 +03:00
|
|
|
// Specify full type name
|
|
|
|
if !p.builtin_mod && p.mod != 'main' {
|
|
|
|
enum_name = p.prepend_mod(enum_name)
|
|
|
|
}
|
|
|
|
// Skip empty enums
|
2019-11-06 04:43:13 +03:00
|
|
|
if !no_name && !p.first_pass() {
|
2019-10-20 19:59:53 +03:00
|
|
|
p.cgen.typedefs << 'typedef int $enum_name;'
|
|
|
|
}
|
2019-11-10 03:08:53 +03:00
|
|
|
p.fspace()
|
2019-10-20 19:59:53 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
mut val := 0
|
|
|
|
mut fields := []string
|
|
|
|
for p.tok == .name {
|
|
|
|
field := p.check_name()
|
2019-10-24 14:25:03 +03:00
|
|
|
if contains_capital(field) {
|
|
|
|
p.warn('enum values cannot contain uppercase letters, use snake_case instead')
|
|
|
|
}
|
2019-10-20 19:59:53 +03:00
|
|
|
fields << field
|
2019-11-18 13:10:31 +03:00
|
|
|
p.fgen_nl()
|
2019-10-20 19:59:53 +03:00
|
|
|
name := '${mod_gen_name(p.mod)}__${enum_name}_$field'
|
2019-10-21 14:00:41 +03:00
|
|
|
if p.tok == .assign {
|
|
|
|
mut enum_assign_tidx := p.cur_tok_index()
|
|
|
|
if p.peek() == .number {
|
|
|
|
p.next()
|
|
|
|
val = p.lit.int()
|
|
|
|
p.next()
|
|
|
|
}else{
|
|
|
|
p.next()
|
|
|
|
enum_assign_tidx = p.cur_tok_index()
|
|
|
|
p.error_with_token_index('only numbers are allowed in enum initializations', enum_assign_tidx)
|
2019-12-05 14:09:33 +03:00
|
|
|
}
|
2019-10-21 14:00:41 +03:00
|
|
|
}
|
2019-10-20 19:59:53 +03:00
|
|
|
if p.pass == .main {
|
|
|
|
p.cgen.consts << '#define $name $val'
|
|
|
|
}
|
|
|
|
if p.tok == .comma {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
val++
|
|
|
|
}
|
2019-12-05 14:09:33 +03:00
|
|
|
p.table.register_type(Type {
|
2019-10-20 19:59:53 +03:00
|
|
|
name: enum_name
|
|
|
|
mod: p.mod
|
|
|
|
parent: 'int'
|
2019-11-06 06:26:04 +03:00
|
|
|
cat: .enum_
|
2019-10-20 19:59:53 +03:00
|
|
|
enum_vals: fields.clone()
|
2019-10-24 13:19:27 +03:00
|
|
|
is_public: is_pub
|
2019-10-20 19:59:53 +03:00
|
|
|
})
|
|
|
|
p.check(.rcbr)
|
|
|
|
p.fgenln('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_enum_member_access() {
|
|
|
|
T := p.find_type(p.expected_type)
|
|
|
|
if T.cat == .enum_ {
|
|
|
|
p.check(.dot)
|
|
|
|
val := p.check_name()
|
|
|
|
// Make sure this enum value exists
|
|
|
|
if !T.has_enum_val(val) {
|
|
|
|
p.error('enum `$T.name` does not have value `$val`')
|
|
|
|
}
|
|
|
|
p.gen(mod_gen_name(T.mod) + '__' + p.expected_type + '_' + val)
|
|
|
|
} else {
|
|
|
|
p.error('`$T.name` is not an enum')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|