parser: C enums + another enum simplification
parent
91bb969ed1
commit
855a736a2c
|
@ -201,11 +201,12 @@ fn (p mut Parser) chash() {
|
||||||
}
|
}
|
||||||
if hash.starts_with('include') {
|
if hash.starts_with('include') {
|
||||||
if p.first_pass() && !p.is_vh {
|
if p.first_pass() && !p.is_vh {
|
||||||
|
/*
|
||||||
if !p.pref.building_v && !p.fileis('vlib') {
|
if !p.pref.building_v && !p.fileis('vlib') {
|
||||||
p.warn('C #includes will soon be removed from the language' +
|
p.warn('C #includes will soon be removed from the language' +
|
||||||
'\ndefine the C structs and functions in V')
|
'\ndefine the C structs and functions in V')
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
if p.file_pcguard.len != 0 {
|
if p.file_pcguard.len != 0 {
|
||||||
//println('p: $p.file_platform $p.file_pcguard')
|
//println('p: $p.file_platform $p.file_pcguard')
|
||||||
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'
|
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'
|
||||||
|
|
|
@ -4,12 +4,16 @@
|
||||||
|
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
fn (p mut Parser) enum_decl(_enum_name string) {
|
fn (p mut Parser) enum_decl(no_name bool) {
|
||||||
mut enum_name := _enum_name
|
|
||||||
is_pub := p.tok == .key_pub
|
is_pub := p.tok == .key_pub
|
||||||
if is_pub {
|
if is_pub {
|
||||||
p.next()
|
p.next()
|
||||||
|
}
|
||||||
p.check(.key_enum)
|
p.check(.key_enum)
|
||||||
|
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()
|
enum_name = p.check_name()
|
||||||
}
|
}
|
||||||
// Specify full type name
|
// Specify full type name
|
||||||
|
@ -17,7 +21,7 @@ fn (p mut Parser) enum_decl(_enum_name string) {
|
||||||
enum_name = p.prepend_mod(enum_name)
|
enum_name = p.prepend_mod(enum_name)
|
||||||
}
|
}
|
||||||
// Skip empty enums
|
// Skip empty enums
|
||||||
if enum_name != 'int' && !p.first_pass() {
|
if !no_name && !p.first_pass() {
|
||||||
p.cgen.typedefs << 'typedef int $enum_name;'
|
p.cgen.typedefs << 'typedef int $enum_name;'
|
||||||
}
|
}
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
|
@ -55,7 +59,7 @@ fn (p mut Parser) enum_decl(_enum_name string) {
|
||||||
name: enum_name
|
name: enum_name
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
parent: 'int'
|
parent: 'int'
|
||||||
cat: TypeCategory.enum_
|
cat: .enum_ //.enum_ //if is_c { TypeCategory.c_struct } else {TypeCategory.enum_ }
|
||||||
enum_vals: fields.clone()
|
enum_vals: fields.clone()
|
||||||
is_public: is_pub
|
is_public: is_pub
|
||||||
})
|
})
|
||||||
|
|
|
@ -1219,7 +1219,7 @@ fn (p mut Parser) replace_type_params(f &Fn, ti TypeInst) []string {
|
||||||
fn (p mut Parser) register_vargs_stuct(typ string, len int) string {
|
fn (p mut Parser) register_vargs_stuct(typ string, len int) string {
|
||||||
vargs_struct := 'varg_$typ'
|
vargs_struct := 'varg_$typ'
|
||||||
varg_type := Type{
|
varg_type := Type{
|
||||||
cat: TypeCategory.struct_,
|
cat: .struct_,
|
||||||
name: vargs_struct,
|
name: vargs_struct,
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
}
|
}
|
||||||
|
@ -1299,7 +1299,7 @@ fn (p mut Parser) register_multi_return_stuct(types []string) string {
|
||||||
typ := '_V_MulRet_' + types.join('_V_').replace('*', '_PTR_')
|
typ := '_V_MulRet_' + types.join('_V_').replace('*', '_PTR_')
|
||||||
if p.table.known_type(typ) { return typ }
|
if p.table.known_type(typ) { return typ }
|
||||||
p.table.register_type2(Type{
|
p.table.register_type2(Type{
|
||||||
cat: TypeCategory.struct_,
|
cat: .struct_,
|
||||||
name: typ,
|
name: typ,
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
})
|
})
|
||||||
|
|
|
@ -227,7 +227,7 @@ fn (p & Parser) peek() TokenKind {
|
||||||
}
|
}
|
||||||
[inline] fn (p &Parser) peek_token() Token {
|
[inline] fn (p &Parser) peek_token() Token {
|
||||||
if p.token_idx >= p.tokens.len - 2 {
|
if p.token_idx >= p.tokens.len - 2 {
|
||||||
return Token{ tok:TokenKind.eof }
|
return Token{ tok:.eof }
|
||||||
}
|
}
|
||||||
return p.tokens[p.token_idx]
|
return p.tokens[p.token_idx]
|
||||||
}
|
}
|
||||||
|
@ -313,23 +313,19 @@ fn (p mut Parser) parse(pass Pass) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_enum {
|
.key_enum {
|
||||||
p.next()
|
next := p.peek()
|
||||||
if p.tok == .name {
|
if next == .name {
|
||||||
p.fgen('enum ')
|
p.fgen('enum ')
|
||||||
name := p.check_name()
|
|
||||||
p.fgen(' ')
|
p.fgen(' ')
|
||||||
p.enum_decl(name)
|
p.enum_decl(false)
|
||||||
}
|
}
|
||||||
else if p.pref.translated {
|
else if next == .lcbr && p.pref.translated {
|
||||||
// enum without a name, only allowed in code,
|
// enum without a name, only allowed in code,
|
||||||
// translated from C. it's a very bad practice
|
// translated from C. it's a very bad practice
|
||||||
// in C as well, but is used unfortunately
|
// in C as well, but is used unfortunately
|
||||||
// (for example, by DOOM). such fields are
|
// (for example, by DOOM). such fields are
|
||||||
// basically int consts
|
// basically int consts
|
||||||
p.enum_decl('int')
|
p.enum_decl(true)
|
||||||
}
|
|
||||||
else {
|
|
||||||
p.check(.name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_pub {
|
.key_pub {
|
||||||
|
@ -337,8 +333,10 @@ fn (p mut Parser) parse(pass Pass) {
|
||||||
match next {
|
match next {
|
||||||
.key_fn { p.fn_decl() }
|
.key_fn { p.fn_decl() }
|
||||||
.key_const { p.const_decl() }
|
.key_const { p.const_decl() }
|
||||||
.key_struct, .key_union, .key_interface { p.struct_decl() }
|
.key_struct,
|
||||||
.key_enum { p.enum_decl('') }
|
.key_union,
|
||||||
|
.key_interface { p.struct_decl() }
|
||||||
|
.key_enum { p.enum_decl(false) }
|
||||||
else {
|
else {
|
||||||
p.error('wrong pub keyword usage')
|
p.error('wrong pub keyword usage')
|
||||||
}
|
}
|
||||||
|
@ -641,7 +639,7 @@ fn (p mut Parser) type_decl() {
|
||||||
name: name
|
name: name
|
||||||
parent: parent.name
|
parent: parent.name
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
cat: TypeCategory.alias
|
cat: .alias
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ fn (p mut Parser) get_type2() Type {
|
||||||
name: f.typ_str()// 'fn (int, int) string'
|
name: f.typ_str()// 'fn (int, int) string'
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
func: f
|
func: f
|
||||||
cat: TypeCategory.func
|
cat: .func
|
||||||
}
|
}
|
||||||
p.table.register_type2(fn_typ)
|
p.table.register_type2(fn_typ)
|
||||||
return fn_typ
|
return fn_typ
|
||||||
|
|
|
@ -37,11 +37,12 @@ fn (p mut Parser) struct_decl() {
|
||||||
}
|
}
|
||||||
is_c := name == 'C' && p.tok == .dot
|
is_c := name == 'C' && p.tok == .dot
|
||||||
if is_c {
|
if is_c {
|
||||||
|
/*
|
||||||
if !p.pref.building_v && !p.fileis('vlib') {
|
if !p.pref.building_v && !p.fileis('vlib') {
|
||||||
p.warn('Virtual C structs will soon be removed from the language' +
|
p.warn('Virtual C structs will soon be removed from the language' +
|
||||||
'\ndefine the C structs and functions in V')
|
'\ndefine the C structs and functions in V')
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
name = p.check_name()
|
name = p.check_name()
|
||||||
cat = .c_struct
|
cat = .c_struct
|
||||||
|
@ -264,6 +265,7 @@ fn (p mut Parser) struct_init(typ string) string {
|
||||||
p.gen_struct_field_init(field)
|
p.gen_struct_field_init(field)
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
p.fspace()
|
p.fspace()
|
||||||
|
p.expected_type = f.typ
|
||||||
p.check_types(p.bool_expression(), f.typ)
|
p.check_types(p.bool_expression(), f.typ)
|
||||||
if p.tok == .comma {
|
if p.tok == .comma {
|
||||||
p.next()
|
p.next()
|
||||||
|
|
Loading…
Reference in New Issue