1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: C enums + another enum simplification

This commit is contained in:
Alexander Medvednikov 2019-11-06 04:43:13 +03:00
parent 91bb969ed1
commit 855a736a2c
6 changed files with 31 additions and 26 deletions

View File

@ -201,11 +201,12 @@ fn (p mut Parser) chash() {
}
if hash.starts_with('include') {
if p.first_pass() && !p.is_vh {
/*
if !p.pref.building_v && !p.fileis('vlib') {
p.warn('C #includes will soon be removed from the language' +
'\ndefine the C structs and functions in V')
}
*/
if p.file_pcguard.len != 0 {
//println('p: $p.file_platform $p.file_pcguard')
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'

View File

@ -4,20 +4,24 @@
module compiler
fn (p mut Parser) enum_decl(_enum_name string) {
mut enum_name := _enum_name
fn (p mut Parser) enum_decl(no_name bool) {
is_pub := p.tok == .key_pub
if is_pub {
p.next()
p.check(.key_enum)
enum_name = p.check_name()
}
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()
}
// Specify full type name
if !p.builtin_mod && p.mod != 'main' {
enum_name = p.prepend_mod(enum_name)
}
// Skip empty enums
if enum_name != 'int' && !p.first_pass() {
if !no_name && !p.first_pass() {
p.cgen.typedefs << 'typedef int $enum_name;'
}
p.check(.lcbr)
@ -55,7 +59,7 @@ fn (p mut Parser) enum_decl(_enum_name string) {
name: enum_name
mod: p.mod
parent: 'int'
cat: TypeCategory.enum_
cat: .enum_ //.enum_ //if is_c { TypeCategory.c_struct } else {TypeCategory.enum_ }
enum_vals: fields.clone()
is_public: is_pub
})

View File

@ -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 {
vargs_struct := 'varg_$typ'
varg_type := Type{
cat: TypeCategory.struct_,
cat: .struct_,
name: vargs_struct,
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_')
if p.table.known_type(typ) { return typ }
p.table.register_type2(Type{
cat: TypeCategory.struct_,
cat: .struct_,
name: typ,
mod: p.mod
})

View File

@ -227,7 +227,7 @@ fn (p & Parser) peek() TokenKind {
}
[inline] fn (p &Parser) peek_token() Token {
if p.token_idx >= p.tokens.len - 2 {
return Token{ tok:TokenKind.eof }
return Token{ tok:.eof }
}
return p.tokens[p.token_idx]
}
@ -313,32 +313,30 @@ fn (p mut Parser) parse(pass Pass) {
}
}
.key_enum {
p.next()
if p.tok == .name {
next := p.peek()
if next == .name {
p.fgen('enum ')
name := p.check_name()
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,
// translated from C. it's a very bad practice
// in C as well, but is used unfortunately
// (for example, by DOOM). such fields are
// basically int consts
p.enum_decl('int')
}
else {
p.check(.name)
p.enum_decl(true)
}
}
.key_pub {
next := p.peek()
match next {
.key_fn { p.fn_decl() }
.key_const { p.const_decl() }
.key_struct, .key_union, .key_interface { p.struct_decl() }
.key_enum { p.enum_decl('') }
.key_fn { p.fn_decl() }
.key_const { p.const_decl() }
.key_struct,
.key_union,
.key_interface { p.struct_decl() }
.key_enum { p.enum_decl(false) }
else {
p.error('wrong pub keyword usage')
}
@ -641,7 +639,7 @@ fn (p mut Parser) type_decl() {
name: name
parent: parent.name
mod: p.mod
cat: TypeCategory.alias
cat: .alias
})
}

View File

@ -31,7 +31,7 @@ fn (p mut Parser) get_type2() Type {
name: f.typ_str()// 'fn (int, int) string'
mod: p.mod
func: f
cat: TypeCategory.func
cat: .func
}
p.table.register_type2(fn_typ)
return fn_typ

View File

@ -37,11 +37,12 @@ fn (p mut Parser) struct_decl() {
}
is_c := name == 'C' && p.tok == .dot
if is_c {
/*
if !p.pref.building_v && !p.fileis('vlib') {
p.warn('Virtual C structs will soon be removed from the language' +
'\ndefine the C structs and functions in V')
}
*/
p.check(.dot)
name = p.check_name()
cat = .c_struct
@ -264,6 +265,7 @@ fn (p mut Parser) struct_init(typ string) string {
p.gen_struct_field_init(field)
p.check(.colon)
p.fspace()
p.expected_type = f.typ
p.check_types(p.bool_expression(), f.typ)
if p.tok == .comma {
p.next()