-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_sequence.py
300 lines (272 loc) · 10.4 KB
/
parse_sequence.py
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
from collections import OrderedDict
import re
import math
import struct
import config
from utils import compute_crc
units = {'': 1, 'mm' : 1e-3, 'deg' : math.pi/180 }
def parse_literal(val):
#try int with unit
m = re.match('(-?[\d]+)([a-z]*)', val)
if m:
return int(m.group(1)) * units[m.group(2)]
#try vec2
m = re.match('\((-?[\w]+),(-?[\w]+)\)', val)
if m:
return (parse_literal(m.group(1)), parse_literal(m.group(2)))
#try vec3
m = re.match('\((-?[\w]+),(-?[\w]+),(-?[\w]+)\)', val)
if m:
return (parse_literal(m.group(1)), parse_literal(m.group(2)), parse_literal(m.group(3)))
return int(val)
sensor_shifts = {'microswitch':0}
opcodes = {
'mov',
'add',
'cmp',
'jne',
'je',
'jl',
'jle',
'jg',
'jge',
'call'
}
opcodes_jump = {
'jmp' : 200,
'jz': 201,
'jnz': 202,
'je': 201,
'jne': 202,
'jge': 203, # jump if greater or equal
}
opcodes = {
'nop': (0, None, None, None),
'mov1': (1, 'var', 'var', None), #move one int32 from arg1 into arg0
'mov2': (2, 'var', 'var', None), #move two int32 from arg1 into arg0
'mov3': (3, 'var', 'var', None), #move three int32 from arg1 into arg0
'movi': (4, 'var', 'imm', None), #move one 8 bit integer to variable
'addi': (8, 'var', 'imm', None),
'subi': (9, 'var', 'imm', None),
'add': (10, 'var', 'var', 'var'),
'sub': (11, 'var', 'var', 'var'),
'propulsion.motors_enable': (64, None, None, None),
'propulsion.enable': (65, None, None, None),
'propulsion.motors_disable': (66, None, None, None),
'propulsion.disable': (67, None, None, None),
'wait_movement_finished': (126, None, None, None),
'wait_arm_finished': (125, None, None, None),
'propulsion.set_pose': (127, 'var', None, None),
'propulsion.set_pose_virtual': (0, None, None, None),
'propulsion.point_to': (128, 'var','var', None),
'propulsion.move_to': (129, 'var', 'var', None),
'propulsion.rotate': (130, 'var', 'var', None),
'propulsion.translate': (131, 'var', 'var', None),
'propulsion.reposition': (132, 'var', 'var', None),
'propulsion.enter_manual': (133, None, None, None),
'propulsion.exit_manual': (134, None, None, None),
'propulsion.set_control_levels': (135, 'imm', 'imm', None),
'propulsion.set_target_pose': (136, 'var', 'var', None),
'propulsion.face_direction': (137, 'var', 'var', None),
'propulsion.set_adversary_detection_enable': (138, 'imm', None, None),
'propulsion.measure_normal': (139, 'var', None, None),
'propulsion.measure_point': (147, 'var', 'var', None),
'propulsion.get_pose': (152, 'var', None, None),
'pump.set_pwm': (140, 'var', None, None),
'arm.go_to_position': (141, 'arm_position', 'imm', 'imm'),
'set_servo': (142, 'servo_id', 'var', 'imm'),
'wait_servo_finished': (146, 'servo_id', None, None),
'arm.shutdown': (143, None, None, None,),
'dc_motor.set_pwm': (144, 'dc_motor_id', 'var', None),
'gpio.set': (145, 'gpio_id', 'imm', None),
'propulsion.trajectory': (120, 'var','imm', 'var'),
'ret': (30, None, None, None,),
'call': (31,'sequence', 0,0),
'delay': (32, 'var', None, None),
'yield': (33, None, None, None,),
'check_sensor' : (150, 'sensor_id', None, None),
'check_propulsion_state' : (151, 'imm', None, None),
'cmp': (160,'var','var',None),
'send_event' : (34, 'imm', 'var', 'imm')
}
#format: op,a1,a2;a3
#a1 = output address
#a2 = input_address
#a3 = 2nd input_address or index
#for 2 input operands
#has 2 forms: input or output indexed
# re.match('^-?[\d+]*$', '-1000')
# re.match('^-?[\d+]*\.[\d+]*$', '-1000.0')
class Variable:
def __init__(self, args):
args = args.split(' ')
nd = args[1].split('=')
self.type = args[0]
self.name = nd[0].strip()
self.default = parse_literal(nd[1]) if len(nd) == 2 else None
self.index = None
def encode(self):
if self.type == 'int':
return struct.pack('<i', self.default)
if self.type == 'float':
return struct.pack('<f', self.default)
if self.type == 'vec2':
return struct.pack('<ff', self.default[0], self.default[1])
if self.type == 'vec3':
return struct.pack('<fff', self.default[0], self.default[1], self.default[2])
class Arg:
def __init__(self, arg):
if re.match('^[a-z_][\w]*$', arg):
self.type = 'var'# var_idx, cst
self.name = arg
else:
self.type = 'cst'
self.value = parse_literal(arg)
def encode(self, typ, parser):
if typ is None:
return 0
if typ == 'var':
return parser.variable_index(self.name)
if typ == 'imm':
return int(self.value)
if typ == 'arm_position':
return parser.config.get_arm_position_index(self.name)
if typ == 'sequence':
return list(parser.sequences.keys()).index(self.name)
if typ == 'servo_id':
return parser.config.get_servo_index(self.name)
if typ == 'gpio_id':
return parser.config.get_gpio_index(self.name)
if typ == 'sensor_id':
return parser.config.get_sensor_index(self.name)
if typ == 'dc_motor_id':
return parser.config.get_dc_motor_index(self.name)
def __repr__(self):
return '<Arg {}>'.format(self.__dict__)
class Op:
def __init__(self, op, args, lineno):
self.op = op
self.args = [Arg(a) for a in args.split(',')] if args != '' else []
self.lineno = lineno
def encode(self, parser):
try:
if self.op in opcodes_jump:
offset = parser.current_sequence.labels[self.args[0].name] + parser.current_sequence_offset
return struct.pack('<BBBB', opcodes_jump[self.op], offset % 256, offset >> 8, 0)
args = [0,0,0]
opc = opcodes[self.op]
for i in range(len(self.args)):
args[i] = self.args[i].encode(opc[i+1], parser)
return struct.pack('<BBBB', opc[0], args[0], args[1], args[2])
except:
print('Compile error, line {}'.format(self.lineno))
print(args)
raise
def __repr__(self):
return '<Op {} {}>'.format(self.op, self.args)
class Sequence:
def __init__(self, name):
self.name = name
self.start_index = 0
self.variables = OrderedDict()
self.labels = {}
self.ops = []
class CompiledSequences:
pass
class SequenceParser:
def __init__(self):
self.constants = {}
self.variables = OrderedDict()
self.sequences = OrderedDict()
self.current_block = None
self.current_sequence = None
self.current_index = 0
def variable_index(self, name):
if name in self.current_sequence.variables:
return self.current_sequence.variables[name].index
return self.variables[name].index
def constant_index(self, name):
if name in self.current_sequence.constants:
return self.current_sequence.constants[name].index
return self.constants[name].index
def parse_file(self, path):
lineno = 0
for line in open(path):
lineno += 1
#strip whitespace
line = line.split('#')[0].strip()
#comment line and empty lines
if line == '':
continue
if ' ' in line:
op, args = line.split(' ', 1)
else:
op = line
args = ''
if op == 'include':
pass
if op == 'begin' and args.split(' ')[0] == 'sequence':
if self.current_block is None:
self.current_sequence = Sequence(args.split(' ')[1])
self.current_index = 0
self.sequences[self.current_sequence.name] = self.current_sequence
elif op == 'end' and args.split(' ')[0] == 'sequence':
self.current_sequence.ops.append(Op('ret','', lineno))
self.current_sequence = None
elif op == 'var':
var = Variable(args)
if self.current_sequence is None:
self.variables[var.name] = var
else:
self.current_sequence.variables[var.name] = var
elif op == 'label':
self.current_sequence.labels[args] = len(self.current_sequence.ops)
else:
try:
op = Op(op,args,lineno)
self.current_sequence.ops.append(op)
except:
print("Parse error at line {}".format(lineno))
print(line)
print(args)
raise
def compile(self):
#allocate global variables
vars_buffer = b''
for v in self.variables.values():
v.index = len(vars_buffer)//4
vars_buffer += v.encode()
#allocate sequence variables
for s in self.sequences.values():
for v in s.variables.values():
v.index = len(vars_buffer)//4
vars_buffer += v.encode()
print('Variable slots taken: {}'.format(len(vars_buffer)//4))
#encode sequences
seqs_table = []
seqs_buffer = b''
for s in self.sequences.values():
seqs_table.append(len(seqs_buffer) // 4)
self.current_sequence = s
self.current_sequence_offset = len(seqs_buffer) // 4
for op in s.ops:
seqs_buffer+=op.encode(self)
#encode buffer
#header
buff = struct.pack('BBBB', len(vars_buffer)//4, len(self.sequences), 0, 0)
#variables
buff += vars_buffer
for si in seqs_table:
buff += struct.pack('<H', si)
buff += seqs_buffer
retval = CompiledSequences()
#crc and size of buffer
buff = struct.pack('HH', len(buff), compute_crc(buff)) + buff
retval.binary = buff
retval.variables = self.variables
retval.sequences = self.sequences
retval.sequence_names = [s.name for s in self.sequences.values()]
return retval
#parser = SequenceParser()
#parser.parse_file('sequence.txt')
#print(len(parser.compile()))