-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_program.py
More file actions
586 lines (548 loc) · 20.7 KB
/
prepare_program.py
File metadata and controls
586 lines (548 loc) · 20.7 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
from control_flow import prepare_control_flow
from exception import throw_exception
import re
import ast2
import operator_overload
import strtools
import line_manager
import execution
class BraceMatcher(object):
# TODO : This class keeps a count of open braces
# for each type: '(', '[', '{'.
# Consider using a stack so that mismatched
# braces such as '[(])' are not considered valid.
def __init__(self, is_repl):
self.is_repl = is_repl
self.reset()
def reset(self):
self.open_parens = 0
self.open_brackets = 0
self.open_braces = 0
self.has_error = False
def match_line(self, line):
for i, char in enumerate(line):
if char == '(':
self.open_parens += 1
elif char == '[':
self.open_brackets += 1
elif char == '{':
self.open_braces += 1
elif char == ')':
self.open_parens -= 1
if self.open_parens < 0:
self.found_unmatched('Parenthesis', i, line)
elif char == ']':
self.open_brackets -= 1
if self.open_brackets < 0:
self.found_unmatched('Bracket', i, line)
elif char == '}':
self.open_braces -= 1
if self.open_braces < 0:
self.found_unmatched('Brace', i, line)
elif char == ',' and (not self.is_repl) and self.is_complete():
return i # found a comma not in any braces
if self.is_repl:
return self
elif self.has_error:
return i
return -1 # no character index for unmatched brace
def found_unmatched(self, brace_name, column, line):
if self.is_repl:
throw_exception('UnmatchedClose' + brace_name, 'At column {0} on line: {1}'.format(column, line))
else:
self.has_error = True
def is_complete(self):
return self.open_parens == 0 and self.open_brackets == 0 and \
self.open_braces == 0
def is_quote(prgm, i):
if i == 0:
return prgm[0] == '"'
else:
return prgm[i] == '"' and prgm[i - 1] != '\\'
def remove_quoted_strings(line):
processed = ''
i = 0
in_double_quotes = False
while i < len(line):
if is_quote(line, i):
in_double_quotes = not in_double_quotes
elif not in_double_quotes:
processed += line[i]
i += 1
return processed
def extract_compound_operator_line(line, operators):
line_without_strings = remove_quoted_strings(line)
for operator in operators:
if operator in line_without_strings:
return operator
return None
def convert_compound_operator(line):
operator = extract_compound_operator_line(line, ast2.compound_operators)
if operator is not None:
# All compound operators take the form of '$=',
# where 'a $= b' expands to 'a = a $ b'
actual_operator = operator[0]
pieces = line.split(operator)
variable = (pieces[0]).strip()
right_hand_side = pieces[1]
if ' ' in variable:
throw_exception(
'DeclaredTypeCompoundOperator',
'Can only use compound operator {0} when variable already ' \
'exists (cannot declare type of variable in this case)'.format(operator)
)
else:
# Expand 'a $= b' into 'a=a$ (b)'
# For instance, 'x *= y + z' becomes 'x=x*( y + z)'
processed_line = '{0}={0}{1}({2})'.format(variable, actual_operator, right_hand_side)
return processed_line
else:
return line
def convert_single_quotes(prgm):
def escape_double_quotes(prgm):
processed = ''
for i in xrange(len(prgm)):
if is_quote(prgm, i):
processed += '\\"'
else:
processed += prgm[i]
return processed
processed = ''
length = len(prgm)
i = 0
in_double_quotes = False
while i < length:
char = prgm[i]
if (not in_double_quotes) and prgm[i : i+2] in ('//', '/*'):
if prgm[i + 1] == '/':
# Scan until end of line
while i < length and prgm[i] != '\n':
processed += prgm[i]
i += 1
char = '\n'
else:
# Scan until end of comment
while i < length and prgm[i : i+2] != '*/':
processed += prgm[i]
i += 1
char = '*'
elif is_quote(prgm, i):
in_double_quotes = not in_double_quotes
if char == "'" and not in_double_quotes:
j = strtools.find_matching_quote(prgm[i + 1:], "'")
if j == -1:
throw_exception('NoClosingSingleQuote', 'No closing single quote in: ' + prgm)
processed += '"' + escape_double_quotes(prgm[i+1 : i+j+1]) + '"'
i += j + 1
else:
processed += char
i += 1
return processed
def preprocess(prgm):
def remove_comments(prgm):
"""
Removes single-line comments and multi-line comments,
except those embedded in strings.
"""
i = 0
processed = ''
length = len(prgm)
in_quotes = False
while i < length:
char = prgm[i]
if is_quote(prgm, i):
in_quotes = not in_quotes
if (not in_quotes) and char == '/' and i + 1 < length:
next = prgm[i + 1]
if next == '/':
# Scan forward until end of line
while i < length and prgm[i] != '\n':
i += 1
i -= 1
elif next == '*':
# Scan forward until end of multi-line comment
while i < length and prgm[i : i+2] != '*/':
i += 1
i += 1
else:
processed += char
else:
processed += char
i += 1
return processed
def replace_special(prgm):
i = 0
processed = ''
length = len(prgm)
in_quotes = False
while i < length:
char = prgm[i]
if is_quote(prgm, i):
in_quotes = not in_quotes
if in_quotes:
processed += strtools.convert_special_char(char)
else:
processed += char
i += 1
return processed
prgm = replace_special(remove_comments(convert_single_quotes(prgm)))
# TODO : handle semicolons embedded in string literals
# and semicolons in the repl.
prgm = prgm.replace(';', '\n')
return prgm
def convert_increment_decrement_operators(line):
if line.startswith('++') or line.startswith('--'):
# Change prefix notation to postfix notation
line = line[2:] + line[:2]
if line.endswith('++'):
operation = '+'
elif line.endswith('--'):
operation = '-'
else:
operation = None
if operation is not None:
if '.' in line or '[' in line:
# This is a complex statement, which includes
# a dot operator or index assignment.
reference = line[:-2]
line = '{0}={0}{1}1'.format(reference, operation)
else:
# This is a simple statement, for an ordinary
# variable name.
var_name = line[:-2]
if operation == '+':
directive_name = 'inc'
else:
directive_name = 'dec'
line = ':{0} {1}'.format(directive_name, var_name)
return line
special_statement_starters = [
'print ', 'show ', 'return ',
'if ', 'when ', 'while ', 'for ', 'repeat ', 'switch ',
'else if ', 'case ', 'throw ', 'super '
]
# Any minus signs that follow one of these characters
# (ignoring any whitespace in the line)
# must be treated as an unary minus, not a binary minus.
significant_chars = '><=+-*/%~^:([{,|'
def replace_for_special_statements(line):
for starter in special_statement_starters:
if line.startswith(starter):
without_starter = line[len(starter):]
if without_starter.lstrip().startswith('-'):
return line.replace('-', '~', 1)
return line
def detect_and_replace_unary_minus(line):
line = replace_for_special_statements(line)
last_meaningful_char = None
processed_line = ''
for char in line:
# Is this an unary minus sign? If so, replace with '~'.
if char == '-' and last_meaningful_char is not None and \
last_meaningful_char in significant_chars:
char = '~'
if not char.isspace():
last_meaningful_char = char
processed_line += char
return processed_line
def is_op_overload_syntax(line):
return line.startswith('sub ') and '(' not in line
ordered_overloadable_ops = [op for op in ast2.ordered_ops if ' ' not in op and op not in ['.', '~']]
def construct_equivalent_function_defn(line):
if not is_op_overload_syntax(line):
return line
if line.endswith(' -this'):
pieces = line.split()
function_keyword = pieces[0]
if len(pieces) == 2:
return '{0} $negative()'.format(function_keyword)
else:
return_type = pieces[1]
return '{0} {1} $negative()'.format(function_keyword, return_type)
elif line.endswith(' not this'):
pieces = line.split()
function_keyword = pieces[0]
if len(pieces) == 3:
return '{0} $booleanNot()'.format(function_keyword)
else:
return_type = pieces[1]
return '{0} {1} $booleanNot()'.format(function_keyword, return_type)
else:
found_operator = None
for operator in ordered_overloadable_ops:
if operator in line:
# Only replace the first occurrence
line = line.replace(operator, ' ', 1)
found_operator = operator
break
if found_operator is not None:
pieces = line.split()
function_keyword = pieces[0]
is_reflected = pieces[-1] == 'this'
method_name = operator_overload.method_names[found_operator]
if is_reflected:
method_name = 'r' + method_name
arg_name = pieces[-2]
else:
arg_name = pieces[-1]
return_type = '' if len(pieces) == 3 else pieces[1] + ' '
return '{0} {1}${2}({3})'.format(
function_keyword,
return_type,
method_name,
arg_name
)
else:
throw_exception(
'InvalidFunctionDefinition',
"'{0}' is not a valid function definition".format(line)
)
def find_assignment_operator(single_line_function):
defn = single_line_function
# 5 is the length of the string 'func '
i = 5
length = len(defn)
while i + 1 < length:
# Look for a standalone '=' operator, which should not be a part of
# '==', '<=', '>=', or '!='
if defn[i] == '=' and defn[i - 1] not in '<>=!' and defn[i + 1] != '=':
return i
i += 1
throw_exception(
'InvalidFunctionSyntax',
'Single line function definition has no assignment operator:\n' + defn
)
def replace_op_overload_syntax(line_mgr):
i = 0
while i < len(line_mgr):
line = line_mgr[i]
if line.startswith('func '):
j = find_assignment_operator(line)
defn = line[5:j]
if '(' not in defn:
return_expr = line[j + 1:]
line_mgr[i : i+1] = [
'sub ' + defn.rstrip(),
'return ' + return_expr,
'end'
]
i += 2
i += 1
line_mgr.for_each_line(construct_equivalent_function_defn)
def find_op_keyword(line, op):
"""
Finds the last operator or keyword in the line,
returning the index at which it is located.
Ignores any content in string literals.
Return -1 if there is no such operator or keyword.
"""
if op not in line:
return -1
in_double_quotes = False
for i in reversed(xrange(len(line))):
if is_quote(line, i):
in_double_quotes = not in_double_quotes
elif (not in_double_quotes) and line[i : i+len(op)] == op:
return i
return -1
def char_range(first, last):
return ''.join(chr(n) for n in xrange(ord(first), ord(last) + 1))
ident_chars = '_$' + char_range('A', 'Z') + char_range('a', 'z') + char_range('0', '9')
def extract_lambda(line, brace_matcher):
arrow_index = find_op_keyword(line, '->')
if arrow_index == -1:
return None
# TODO : for pattern matching, allow nested braces such as [args]
# in an argument list
begin_index = arrow_index - 1
# Skip whitespace:
while begin_index > 0 and line[begin_index] in ' \t':
begin_index -= 1
args = None
if line[begin_index] == ')':
begin_index -= 1
while begin_index >= 0 and line[begin_index] != '(':
begin_index -= 1
args = line[begin_index : arrow_index].strip()
else:
while begin_index >= 1 and line[begin_index - 1] in ident_chars:
begin_index -= 1
args = '(' + line[begin_index : arrow_index].strip() + ')'
arrow_index += 2 # move past the '->' token
brace_matcher.reset()
# Find the end of the lambda
end_index = brace_matcher.match_line(line[arrow_index:])
if end_index == -1:
end_index = len(line) # lambda stops at end of line
else:
end_index += arrow_index # compensate for location of arrow
body = line[arrow_index : end_index].strip()
return args, body, begin_index, end_index
def get_nested_lambda(lambda_body):
match_obj = re.match(r'\(*(\$lambda[0-9]+)\)*', lambda_body)
if match_obj:
return match_obj.group(1)
return None
def scan_delim(line, i, accept, step):
length = len(line)
while i >= 0 and i < length:
ch = line[i]
if ch not in ' \t':
return ch in accept
i += step
return False
def substr_at(line, i, substr):
return line[i:].startswith(substr)
section_operators = [':', '^', '*', '/', '%', '+', '-', '==', '!=', '>=', '<=', '>', '<']
def lift_op_sections(line):
in_double_quotes = False
for i in xrange(len(line)):
if is_quote(line, i):
in_double_quotes = not in_double_quotes
if in_double_quotes:
continue
for op in section_operators:
if substr_at(line, i, op):
right_start = i + len(op)
if scan_delim(line, i - 1, ',([{', -1) and \
scan_delim(line, right_start, ',)]}', 1):
return line[:i] + '(a,b)->a{0}b'.format(op) + lift_op_sections(line[right_start:])
return line
def interleave(lst, item):
result = []
for elem in lst:
result.extend([elem, item])
if len(result) >= 2:
result.pop()
return result
def decompose_function_calls(expr):
tokens = ast2.AST(expr).parse()
transformed = []
for token in tokens:
match_obj = re.match(r'(\$?[A-Za-z_][A-Za-z_0-9]*)\((.*)\)', token)
if match_obj:
func_name = match_obj.group(1)
func_args = match_obj.group(2)
arg_list = execution.split_args(func_args)
transformed.extend([func_name, '('] + interleave(arg_list, ',') + [')'])
else:
transformed.append(token)
return [token.strip() for token in transformed]
def extract_list_comprehension(line):
has_state = False
from_index = find_op_keyword(line, ' from ')
if from_index == -1:
return None
begin_index = from_index - 1
# Skip until pipe character
while begin_index > 0 and line[begin_index] != '|':
begin_index -= 1
elem = line[begin_index + 1 : from_index].strip()
pipe_index = begin_index
while begin_index > 0 and line[begin_index] != '[':
begin_index -= 1
map_expr = line[begin_index + 1 : pipe_index].strip()
map_tokens = decompose_function_calls(map_expr)
if 'this' in map_tokens:
map_expr = ''.join(('$stateList' if token == 'this' else token) for token in map_tokens)
has_state = True
right_start = from_index + len(' from ')
end_index = right_start
while end_index < len(line) and line[end_index] != ']':
end_index += 1
source = line[right_start : end_index].strip()
return map_expr, elem, source, begin_index, end_index, has_state
def lift_list_comprehension(line):
result = extract_list_comprehension(line)
if result is None:
return line
map_expr, elem, source, begin_index, end_index, has_state = result
if has_state:
return lift_list_comprehension(line[:begin_index]) + \
'$mapWithState(({0}, $stateList) -> {1}, {2})'.format(elem, map_expr, source) + \
line[end_index + 1:]
else:
return lift_list_comprehension(line[:begin_index]) + \
'$map({0} -> {1}, {2})'.format(elem, map_expr, source) + \
line[end_index + 1:]
def split_statement(line):
for keyword in special_statement_starters:
if line.startswith(keyword):
return keyword[:-1], line[len(keyword):]
if line.startswith('func '):
# TODO : allow default arguments in function definition
split_index = line.find('=') + 1
return line[:split_index], line[split_index:]
op = extract_compound_operator_line(line, ast2.compound_operators)
if op is not None:
split_index = line.find(op) + 2
return line[:split_index], line[split_index:]
return '', line
def convert_ternary(line):
# Scan the string first before doing extra work:
if 'then' in line and 'else' in line:
front, rest = split_statement(line)
tokens = decompose_function_calls(rest)
try:
# TODO: this part needs to use BraceMatcher
# and the BraceMatcher must be able to operate on lists
delim_front = tokens.index('then')
delim_rest = tokens.index('else')
except ValueError:
# It's possible that the tokens don't
# form a real ternary, in which case
# it's ok to continue:
return line
cond = ''.join(tokens[:delim_front])
true_case = ''.join(tokens[delim_front + 1:delim_rest])
false_case = ''.join(tokens[delim_rest + 1:])
return front + ' $ternary({0}, {1}, {2})'.format(cond, true_case, false_case)
return line
def lift_lambdas(line_mgr, env):
line_mgr.for_each_line(lift_op_sections)
brace_matcher = BraceMatcher(False)
lambda_num = 0 if env is None else env.lambda_num
new_line_mgr = line_manager.LineManager([])
for i, line_data in line_mgr.enumerate_line_data():
line = line_data.line
lifted = []
while True:
line = lift_list_comprehension(convert_ternary(line))
lambda_content = extract_lambda(line, brace_matcher)
if lambda_content is None:
break
args, body, begin_index, end_index = lambda_content
nested_lambda = get_nested_lambda(body)
if nested_lambda is not None:
if len(lifted) == 0:
throw_exception(
'InternalError',
'Lambda {0} was never defined'.format(nested_lambda)
)
last_lambda_def = lifted.pop()
# Warning: when generating these code lines, do not add
# any indentation (leading whitespace) inside the string
# literals. The code has already been preprocessed
# to remove such whitespace.
lifted.extend([
'sub $lambda{0}{1}'.format(lambda_num, args),
last_lambda_def,
'return ' + nested_lambda,
'end'
])
else:
lifted.append('func $lambda{0}{1} = {2}'.format(lambda_num, args, body))
line = line[:begin_index] + '$lambda' + str(lambda_num) + line[end_index:]
lambda_num += 1
lifted.append(line)
for ln in lifted:
new_line_mgr.append_line_data(line_manager.LineData(ln, line_data.line_num, line_data.end_line_num))
return new_line_mgr, lambda_num
def prepare_program(line_mgr):
"""
Splits lines of a program and prepares control flow.
"""
line_mgr.for_each_line(convert_compound_operator)
line_mgr.for_each_line(convert_increment_decrement_operators)
line_mgr.for_each_line(detect_and_replace_unary_minus)
prepare_control_flow(line_mgr)