Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix enum with const value declaration #97

Merged
merged 5 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/c2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -894,32 +894,34 @@ fn (mut c C2V) enum_decl(mut node Node) {
for i, mut child in node.inner {
name := filter_name(child.name.to_lower())
vals << name
mut has_anon_generated := false
// empty enum means it's just a list of #define'ed consts
if enum_name == '' {
if !name.starts_with('_') && name !in c.consts {
c.consts << name
c.genln('\t${name} = ${i}')
c.gen('\t${name}')
has_anon_generated = true
}
} else {
c.gen('\t' + name)
// handle custom enum vals, e.g. `MF_SHOOTABLE = 4`
if child.inner.len > 0 {
mut const_expr := child.try_get_next_child() or {
}
// handle custom enum vals, e.g. `MF_SHOOTABLE = 4`
if child.inner.len > 0 {
mut const_expr := child.try_get_next_child() or {
println(err)
bad_node
}
if const_expr.kind == .constant_expr {
c.gen(' = ')
c.skip_parens = true
c.expr(const_expr.try_get_next_child() or {
println(err)
bad_node
}
if const_expr.kind == .constant_expr {
c.gen(' = ')
c.skip_parens = true
c.expr(const_expr.try_get_next_child() or {
println(err)
bad_node
})
c.skip_parens = false
}
} else {
c.genln('')
})
c.skip_parens = false
}
} else if has_anon_generated {
c.genln(' = ${i}')
}
}
if enum_name != '' {
Expand Down
27 changes: 27 additions & 0 deletions tests/11.enum_default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
enum myEnum {
A,
B,
C
};

typedef enum {
D,
E,
F
} myAnotherEnum;

typedef enum myStrangeEnum {
G,
H,
I
} myStrangeEnum;

enum { J = 1 };

int main() {
enum myEnum myEnumVar = A;
myAnotherEnum myEnumVar2 = D;
myStrangeEnum myEnumVar3 = G;
int myIntVar = J;
return 0;
}
32 changes: 32 additions & 0 deletions tests/11.enum_default.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[translated]
module main

enum MyEnum {
a
b
c
}

enum MyAnotherEnum {
d
e
f
}

enum MyStrangeEnum {
g
h
i
}

const ( // empty enum
j = 1
)

fn main() {
myenumvar := MyEnum.a
myenumvar2 := MyAnotherEnum.d
myenumvar3 := MyStrangeEnum.g
myintvar := j
return
}