-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenglishbyte.py
72 lines (61 loc) · 2.89 KB
/
englishbyte.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
class EnglishByte(object):
"""
Class for handling the different string formating operations
of the english translation.
"""
translation = {'load': "{0}", 'call': "result of call to {0}",
'store': "Store {0} as {1}",'return': "Return {0}",
'binary': "{1} {0} {2}", 'compare': "{1} {0} {2}"}
binary_operators = {'add': 'plus', 'subtract': 'minus', 'multiply': 'times',
'divide': 'divided by', 'power': 'raised to the power of',
'modulo': 'modulo', 'and': 'AND', 'or': 'OR', 'xor': 'XOR',
'==': "is equal to", '>': "is greater than",
'<': "is less than", '>=': "is greater or equal to",
'<=': "is less or equal to", '!=': "is not equal to",
'in': "is in"}
short_english_only = {'load', 'compare'}
def __init__(self, command, line_num, *args):
self.line_num = line_num
self.pre_formatted_string = self.translation[command]
self.prefix = ""
self.suffix = "."
special_init = getattr(self, '{}_init'.format(command), self.default_init)
special_init(args)
if command != 'call' and command not in self.short_english_only:
self.full = self._full_english()
def default_init(self, args):
self.short = self.pre_formatted_string.format(*args)
def return_init(self, args):
return_value = args[1]
self.short = self.pre_formatted_string.format(return_value)
def binary_init(self, args):
operation = self.binary_operators[args[0]]
operand1 = args[1]
operand2 = args[2]
self.short = self.pre_formatted_string.format(operation, operand1,\
operand2)
self.prefix = "Compute "
def compare_init(self, args):
comparison = self.binary_operators[args[0]]
operand1 = args[1]
operand2 = args[2]
self.short = self.pre_formatted_string.format(comparison, operand1,\
operand2)
def call_init(self, args):
# args[0] is the number of arguments (not used here)
func = args[1]
self.short = self.pre_formatted_string.format(func)
# dealing with the full english
args_of_func= args[2:]
if len(args_of_func) == 0:
suffix = 'no argument'
elif len(args_of_func) == 1:
suffix = '{0} as argument'.format(*args_of_func)
else:
suffix = '{0} as arguments'.format(', '.join(map(str, args_of_func)))
self.full = 'Call {0} with {1}.'.format(func, suffix)
self.full = '\t'.join([self.line_num, self.full])
def _full_english(self):
result = ''.join([self.prefix, self.short, self.suffix])
result = '\t'.join([self.line_num, result])
return result