aspic.ipynb
11.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Syntax\n",
"\n",
"The syntax is very simple and generic:\n",
"\n",
"```\n",
" # this is a comment\n",
" model ::= stmt+\n",
" stmt ::= call | block\n",
" call ::= name args \"\\n\"\n",
" args ::= \"(\" arglist? \")\"\n",
" arglist ::= arg (\",\" arg)*\n",
" arg ::= atom (\"=\" atom)?\n",
" block ::= deco? name args? \":\" \"\\n\" INDENT stmt+ DEDENT\n",
" deco ::= \"@\" name args? \"\\n\"\n",
" atom ::= name | number | code | string\n",
" name ::= /[a-z][a-z0-9_]*/i\n",
" number ::= /[+-]?[0-9]+/\n",
" code ::= /{.*}/ | /[.*]/ # simplified here (nesting is correctly handled)\n",
" string ::= /\".*\"/ | /'.*'/ | /\"\"\".*\"\"\"/a | /'''.*'''/a # just Python strings\n",
"```\n",
"\n",
"Each `block` must be one of:\n",
" * `if` / `then` / `?else` (which means that `else` is optional)\n",
" * `choose` / `+either` (which means that `either` may be repeated)\n",
" * `parallel` / `+besides`\n",
" * `race` / `+against`\n",
" * `retry`\n",
" * `negate`\n",
" * `task`\n",
"\n",
"The correct sequencing of blocks is not enforced in the syntax but when the parse tree is built. Moreover, each block may actually have parameters nd a decorator (`@...` like in Python). It is not checked at parse time wether arguments and decorators are correctly used, instead this is let to the compiler.\n",
"\n",
"## Implementation\n",
"\n",
"Import parser and build a parse tree from souce code. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from zinc.io.aspic import parse\n",
"src = \"\"\"\n",
"retry :\n",
" if :\n",
" pos()\n",
" comp(x=10, y=42, duration=100)\n",
" race :\n",
" traj()\n",
" against:\n",
" negate :\n",
" obs()\n",
" then :\n",
" pose()\n",
" keep()\n",
" else :\n",
" negate :\n",
" avoid()\n",
"\"\"\"\n",
"tree = parse(src)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The parse tree can be displayed in compact (`repr`) or full (`dump`) form."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"model :\n",
" + body[0] = retry :\n",
" | + body[0] = if :\n",
" | | + body[0] = call :\n",
" | | | + name = 'pos'\n",
" | | + body[1] = call :\n",
" | | | + name = 'comp'\n",
" | | | + kargs['x'] = const :\n",
" | | | | + value = 10\n",
" | | | | + type = 'int'\n",
" | | | + kargs['y'] = const :\n",
" | | | | + value = 42\n",
" | | | | + type = 'int'\n",
" | | | + kargs['duration'] = const :\n",
" | | | | + value = 100\n",
" | | | | + type = 'int'\n",
" | | + body[2] = race :\n",
" | | | + body[0] = call :\n",
" | | | | + name = 'traj'\n",
" | | | + against[0] = against :\n",
" | | | | + body[0] = negate :\n",
" | | | | | + body[0] = call :\n",
" | | | | | | + name = 'obs'\n",
" | | + else = else :\n",
" | | | + body[0] = negate :\n",
" | | | | + body[0] = call :\n",
" | | | | | + name = 'avoid'\n",
" | | + then = then :\n",
" | | | + body[0] = call :\n",
" | | | | + name = 'pose'\n",
" | | | + body[1] = call :\n",
" | | | | + name = 'keep'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"model :\n",
" + body[0] = retry :\n",
" | + body[0] = if :\n",
" | | + body[0] = call :\n",
" | | | + name = 'pos'\n",
" | | | + largs = []\n",
" | | | + kargs = {}\n",
" | | + body[1] = call :\n",
" | | | + name = 'comp'\n",
" | | | + largs = []\n",
" | | | + kargs['x'] = const :\n",
" | | | | + value = 10\n",
" | | | | + type = 'int'\n",
" | | | + kargs['y'] = const :\n",
" | | | | + value = 42\n",
" | | | | + type = 'int'\n",
" | | | + kargs['duration'] = const :\n",
" | | | | + value = 100\n",
" | | | | + type = 'int'\n",
" | | + body[2] = race :\n",
" | | | + body[0] = call :\n",
" | | | | + name = 'traj'\n",
" | | | | + largs = []\n",
" | | | | + kargs = {}\n",
" | | | + args = None\n",
" | | | + deco = None\n",
" | | | + against[0] = against :\n",
" | | | | + body[0] = negate :\n",
" | | | | | + body[0] = call :\n",
" | | | | | | + name = 'obs'\n",
" | | | | | | + largs = []\n",
" | | | | | | + kargs = {}\n",
" | | | | | + args = None\n",
" | | | | | + deco = None\n",
" | | | | + args = None\n",
" | | | | + deco = None\n",
" | | | | + against = []\n",
" | | + args = None\n",
" | | + deco = None\n",
" | | + else = else :\n",
" | | | + body[0] = negate :\n",
" | | | | + body[0] = call :\n",
" | | | | | + name = 'avoid'\n",
" | | | | | + largs = []\n",
" | | | | | + kargs = {}\n",
" | | | | + args = None\n",
" | | | | + deco = None\n",
" | | | + args = None\n",
" | | | + deco = None\n",
" | | + then = then :\n",
" | | | + body[0] = call :\n",
" | | | | + name = 'pose'\n",
" | | | | + largs = []\n",
" | | | | + kargs = {}\n",
" | | | + body[1] = call :\n",
" | | | | + name = 'keep'\n",
" | | | | + largs = []\n",
" | | | | + kargs = {}\n",
" | | | + args = None\n",
" | | | + deco = None\n",
" | | | + else = None\n",
" | + args = None\n",
" | + deco = None\n"
]
}
],
"source": [
"tree.dump()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `dump` above allows to see all the attributes of each node, even the empty/not used ones. For instance, each block has attributes `deco` and `args`, even if they have not been used in the parsed source.\n",
"\n",
"The parse tree is a nest of `node` instances, each one has the following attributes and methods:\n",
" * `tag` the kind of the node (or the block name if this node is for a block)\n",
" * `_fields` the names of its attributes\n",
" * `__setitem__(self, key, val)` to add a new attribute and record it to `_fields`\n",
" * `__getitem__(self, key)` to retrieve an attribute in a way more convenient than using `getattr`\n",
" * `__contains__(self, key)` to check whether it has an attribute\n",
" * `__iter__(self)` to iterate over the attributes in `_field` provided as name/value pairs"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(False, True, True)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"spam\" in tree, \"body\" in tree, tree[\"body\"] is tree.body"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compilation\n",
"\n",
"A very basic compiler is provided as `zinc.io.meta.Compiler`, it implements a node tranformer pattern similar to Python's standard `ast.NodeTransformer`:\n",
" * method `visit` allows to process a node\n",
" * it calls a method `visit_tag` if it exists, where `tag` is the node's tag\n",
" * otherwise it calls method `generic_visit` that basically turns the node into a `dict`, recursing over its attributes\n",
" \n",
"To implement a compiler, one may subclass `Compiler` adding appropriate `visit_*` methods for each kind of node."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from zinc.io.meta import Compiler\n",
"comp = Compiler()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'model': {'body': [{'retry': {'body': [{'if': {'body': [{'call': {'name': 'pos',\n",
" 'largs': [],\n",
" 'kargs': {}}},\n",
" {'call': {'name': 'comp',\n",
" 'largs': [],\n",
" 'kargs': {'x': {'const': {'value': 10, 'type': 'int'}},\n",
" 'y': {'const': {'value': 42, 'type': 'int'}},\n",
" 'duration': {'const': {'value': 100, 'type': 'int'}}}}},\n",
" {'race': {'body': [{'call': {'name': 'traj',\n",
" 'largs': [],\n",
" 'kargs': {}}}],\n",
" 'args': None,\n",
" 'deco': None,\n",
" 'against': [{'against': {'body': [{'negate': {'body': [{'call': {'name': 'obs',\n",
" 'largs': [],\n",
" 'kargs': {}}}],\n",
" 'args': None,\n",
" 'deco': None}}],\n",
" 'args': None,\n",
" 'deco': None,\n",
" 'against': []}}]}}],\n",
" 'args': None,\n",
" 'deco': None,\n",
" 'else': {'else': {'body': [{'negate': {'body': [{'call': {'name': 'avoid',\n",
" 'largs': [],\n",
" 'kargs': {}}}],\n",
" 'args': None,\n",
" 'deco': None}}],\n",
" 'args': None,\n",
" 'deco': None}},\n",
" 'then': {'then': {'body': [{'call': {'name': 'pose',\n",
" 'largs': [],\n",
" 'kargs': {}}},\n",
" {'call': {'name': 'keep', 'largs': [], 'kargs': {}}}],\n",
" 'args': None,\n",
" 'deco': None,\n",
" 'else': None}}}}],\n",
" 'args': None,\n",
" 'deco': None}}]}}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comp.visit(tree)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}