-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlStructures.nim
202 lines (158 loc) · 4.21 KB
/
controlStructures.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import macros
## This Module provides a number of Control Structures.
## Most of them are just Syntactic Sugar: some Loops,
## better syntax for defining Classes. But there is also
## an Python-like Context-Manager and hopefully some more
## in the future
##
template unless*(cond: bool, body: untyped) =
##if not(cond)
if not(cond):
body
template until*(cond: bool, body: untyped) =
##until loop with testing at the beginnings
while not(cond):
body
template do_while*(cond: bool, body: untyped) =
##while loop with testing at the end
while true:
body
if not (cond):
break
template do_until*(cond: bool, body: untyped) =
##until loop with testing at the end
while true:
body
if (cond):
break
template do_forever*(body) =
##repeat forever
while true:
body
template times*(head: int, body: untyped) =
##repeat n times
for i in 1..head:
body
template do_until_exception*(body) =
##Repeat until an exception is raised
while true:
try:
body
except:
break
template do_while_exception*(body) =
##Repeat while an exception is raised
while true:
try:
body
break
except:
discard
macro a_if*(head, body: untyped): untyped =
##An Arithmetic If similiar to the one in Fortran
template prepareAST(exp, pos, zero, neg: untyped) =
case exp:
of 1..high(int):
pos
of 0:
zero
of low(int)..(-1):
neg
else:
raise newException(ValueError, "Invalid Value")
echo $body[0][0]
if len(body) == 3 and $body[0][0] == "pos" and $body[1][0] == "zero" and $body[2][0] == "neg":
result = getAst(prepareAST(head, body[0][1], body[1][1], body[2][1]))
else:
error "syntaxError"
macro take*(head, body: untyped): untyped =
## Python-like Context Manager
## Executes 'enter()' on entrance and 'exit()' on exit
var
value, varName: NimNode
if head.kind == nnkInfix and $head[0] == "as":
value = head[1]
varName = head[2]
else:
error "Invalid node: " & head.lispRepr
#preparing AST
template withstmt(a, b:varargs[untyped], c:untyped): untyped =
block:
var
b = a
try:
enter(b)
except:
discard
try:
c
except:
raise
finally:
exit(b)
result = getAst(withstmt(value, varName, body))
proc enter*(a: any) =
##general proc for 'take'
discard
proc exit*(a: any) =
##general proc for 'take'
discard
macro class*(head, body: untyped): untyped =
##Better Syntax for Class-Definitions
##
## ..code-block:: nim
## class Test of RootObj:
## var
## name: string
## proc hello() =
## echo "Hello " & this.name
## method bye() =
## echo "Bye " & this.name
##
## gets transformed to:
## ..code-block:: nim
## type Test = ref of RootObj
## name: string
## proc hello(this: Test) =
## echo "Hello " & this.name
## method bye(this: Test) {.base.}=
## echo "Bye " & this.name
var
className, classType : NimNode
exported : bool
routineDefs : NimNode
varDefs : NimNode
varDefs = newNimNode(nnkRecList)
routineDefs = newStmtList()
if head.kind == nnkInfix and $head[0] == "of":
className = head[1]
classType = head[2]
exported = false
elif head.kind == nnkInfix and $head[0] == "*":
className = head[1]
classType = head[2][1]
exported = true
else:
error "Invalid Node"
for node in body:
if node.kind == nnkProcDef or node.kind == nnkMethodDef:
routineDefs.add(node)
elif node.kind == nnkVarSection:
for i in node:
if i.kind == nnkIdentDefs:
varDefs.add(i)
for node in routineDefs:
node[3].insert(1, newIdentDefs(ident("this"), className))
if node.kind == nnkMethodDef:
node[4] = newNimNode(nnkPragma)
node[4].add(ident("base"))
result = routineDefs
template typeDecl(a, b: untyped): untyped =
type a = ref object of b
template typeDeclPub(a, b: untyped): untyped =
type a* = ref object of b
if exported:
result.insert(0, getAst(typeDeclPub(className, classType)))
else:
result.insert(0, getAst(typeDecl(className, classType)))
result[0][0][2][0][2] = varDefs