Skip to content

Commit

Permalink
implemented enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Jul 15, 2024
1 parent c19d425 commit 22c4521
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/nifc-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ FieldDecl ::= (fld SymbolDef FieldPragmas Type)
UnionDecl ::= (union Empty FieldDecl*)
ObjDecl ::= (object [Empty | Type] FieldDecl*)
EnumFieldDecl ::= (efld SymbolDef Expr)
EnumFieldDecl ::= (efld SymbolDef Number)
EnumDecl ::= (enum Type EnumFieldDecl+)
ProcType ::= (proctype Empty Params Type ProcTypePragmas)
Expand Down
35 changes: 32 additions & 3 deletions src/nifc/gentypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,34 @@ proc genObjectOrUnionBody(c: var GeneratedCode; types: TypeGraph; n: NodePos) =
c.add Semicolon
else: discard

proc genEnumDecl(c: var GeneratedCode; t: TypeGraph; n: NodePos) =
# (efld SymbolDef Expr)
# EnumDecl ::= (enum Type EnumFieldDecl+)
let baseType = n.firstSon
for ch in sonsFromX(t, n):
if t[ch].kind == EfldC:
let (a, b) = sons2(t, ch)
if t[a].kind == SymDef:
let enumFieldName = mangle(c.m.lits.strings[t[a].litId])
c.add "#define "
c.add enumFieldName
c.add Space
c.add ParLe
c.add ParLe
c.genType t, baseType
c.add ParRi
case t[b].kind
of IntLit: c.genIntLit t[b].litId
of UIntLit: c.genUIntLit t[b].litId
else:
error c.m, "expected `Number` but got: ", t, a
c.add ParRi
c.add NewLine
else:
error c.m, "expected `SymbolDef` but got: ", t, a
else:
error c.m, "expected `efld` but got: ", t, ch

proc generateTypes(c: var GeneratedCode; types: TypeGraph; o: TypeOrder) =
for (d, declKeyword) in o.forwardedDecls.s:
let decl = asTypeDecl(types, d)
Expand Down Expand Up @@ -253,17 +281,18 @@ proc generateTypes(c: var GeneratedCode; types: TypeGraph; o: TypeOrder) =
c.add Semicolon
c.add CurlyRi
c.add s
c.add Semicolon
of EnumC:
assert false, "too implement"
genEnumDecl c, types, decl.body
of ProctypeC:
c.add TypedefKeyword
genType c, types, decl.body, s
c.add Semicolon
else:
c.add declKeyword
c.add CurlyLe
# XXX generate attributes and pragmas here
c.genObjectOrUnionBody types, decl.body
c.add CurlyRi
c.add s
c.add Semicolon

c.add Semicolon
5 changes: 5 additions & 0 deletions tests/nifc/hello.nif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
(incl "<stdio.h>")
(type :MyObject.c . (object . ))
(type :MyProc . (proctype . (params) (void) . ))
(type :MyEnum . (enum (u 8 .)
(efld :ValueA 0)
(efld :ValueB 1)
(efld :ValueC 6)
))
(nodecl (proc :printf.c (params (param :f.1 . (ptr FILE.c))) (void) (pragmas (varargs)) .))
(proc :main.c . (i M 90) . (stmts
(call printf.c "hello %s" "abc")
Expand Down

0 comments on commit 22c4521

Please sign in to comment.