-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.nim
54 lines (49 loc) · 1.55 KB
/
state.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from cppclass import CppClass
from macros import error, newIdentNode, newStrLitNode, newTree, `$`, kind
from macros import nnkExprColonExpr, nnkStrLit
type
SourceType* {.pure.} = enum
none, dynlib, header
WrapSource = object
is_string: bool
case kind: SourceType
of SourceType.none: discard
of SourceType.dynlib, SourceType.header:
file*: string
State* = object
namespace*: string
class*: CppClass
source: WrapSource
proc newState*(source: NimNode = newIdentNode(""),
source_type: SourceType = SourceType.none): State =
if source_type == SourceType.none or
$source == "": State()
else:
let source_string = $source
State(source: WrapSource(
is_string: source.kind == nnkStrLit,
kind: source_type,
file: source_string))
proc append*(self: State, namespace: string = nil,
class: CppClass = nil): State =
result = self
if class != nil:
if result.class == nil: result.class = class
else: error(
"Class can not be appended becouse it already exists in State!")
if namespace != nil:
result.namespace =
if result.namespace != nil:
result.namespace & "::" & namespace
else: namespace
proc source_declaration*(self: State): NimNode =
case self.source.kind
of SourceType.none: newIdentNode("nodecl")
of SourceType.dynlib, SourceType.header:
let file =
if self.source.is_string:
newStrLitNode(self.source.file)
else: newIdentNode(self.source.file)
newTree(nnkExprColonExpr,
newIdentNode($self.source.kind),
file)