Skip to content

Commit ff46b4f

Browse files
committed
Add enum output to golang
1 parent 9a12817 commit ff46b4f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎golang/codegen/purego_to_test.odin‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ MAX_ARRAY_CAP = 4096 #Untyped
4646
my_size_type = #UInt64
4747
Window = #Struct name #String width #UInt32 height #UInt32
4848
Array = #Struct len my_size_type cap my_size_type els #UInt32 #Attr Ptr 1 #AttrEnd
49+
Fruit = #Enum #UInt32 Banana 1 Apple 2 Orange 3 Kiwi 4 Apricot 5 Peach 6
4950
5051
[extern]
5152
FILE = "stdinc/stdio.h" #SInt32
@@ -79,6 +80,7 @@ my_size_type = #UInt64
7980
Window = #Struct name #String width #UInt32 height #UInt32
8081
WindowWin32 = #Struct name #String width #UInt32 height #UInt32 win32Id #UIntX
8182
Array = #Struct len my_size_type cap my_size_type els #UInt32 #Attr Ptr 1 #AttrEnd
83+
Fruit = #Enum #UInt32 Banana 1 Apple 2 Orange 3 Kiwi 4 Apricot 5 Peach 6
8284
8385
[extern]
8486
FILE = "stdinc/stdio.h" #SInt32

‎golang/codegen/to.odin‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,37 @@ generate_constants :: proc(rs: runic.Runestone, rn: runic.To) -> string {
255255
strings.write_rune(&buf, '\n')
256256
}
257257

258+
for entry in rs.types.data {
259+
type_name, typ := entry.key, entry.value
260+
261+
emum, emum_ok := typ.spec.(runic.Enum)
262+
if !emum_ok do continue
263+
264+
strings.write_rune(&buf, '\n')
265+
266+
for enum_entry in emum.entries {
267+
strings.write_rune(&buf, '\t')
268+
269+
// TODO: properly handle casing
270+
strings.write_string(&buf, type_name)
271+
strings.write_string(&buf, enum_entry.name)
272+
273+
strings.write_rune(&buf, ' ')
274+
strings.write_string(&buf, type_name)
275+
276+
strings.write_string(&buf, " = ")
277+
278+
switch v in enum_entry.value {
279+
case i64:
280+
strings.write_i64(&buf, v)
281+
case string:
282+
strings.write_string(&buf, v)
283+
}
284+
285+
strings.write_rune(&buf, '\n')
286+
}
287+
}
288+
258289
strings.write_string(&buf, ")")
259290

260291
return strings.to_string(buf)

0 commit comments

Comments
 (0)