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
|
import copy
import json
import math
import random
class Tonkadur:
def generate_instance_of (self, typedef):
if (typedef['category'] == "bool"):
return False
elif (typedef['category'] == "float"):
return 0.0
elif (typedef['category'] == "int"):
return 0
elif (typedef['category'] == "rich_text"):
result = dict()
result['content'] = []
result['effect'] = None
return result
elif (typedef['category'] == "string"):
return ""
elif (typedef['category'] == "list"):
return dict()
elif (typedef['category'] == "pointer"):
return []
elif (typedef['category'] == "structure"):
return copy.deepcopy(self.types[typedef['name']])
def __init__ (self, json_file):
self.memory = dict()
self.types = dict()
self.sequences = dict()
self.code = []
self.program_counter = 0
self.allocated_data = 0
self.available_choices = []
with open(json_file, 'r') as f:
json_content = json.load(f)
#### INITIALIZE TYPES ##############################################
for typedef in json_content['structure_types']:
new_type = dict()
for field in typedef['fields']:
new_type[field['name']] = self.generate_instance_of(field['type'])
self.types[typedef['name']] = new_type
#### INITIALIZE VARIABLES ##########################################
for vardef in json_content['variables']:
self.memory[vardef['name']] = self.generate_instance_of(vardef['type'])
#### INITIALIZE SEQUENCES ##########################################
for seqdef in json_content['sequences']:
self.sequences[seqdef['name']] = seqdef['line']
#### INITIALIZE CODE ###############################################
self.code = json_content['code']
def compute (self, computation):
computation_category = computation['category']
if (computation_category == "add_rich_text_effect"):
effect = dict()
effect['name'] = computation['effect']
effect['parameters'] = []
for c in computation['parameters']:
effect['parameters'].append(self.compute(c))
result = dict()
result['content'] = []
result['effect'] = effect
for c in computation['content']:
result['content'].append(self.compute(c))
return result
elif (computation_category == "cast"):
origin_type = computation['from']['category']
target_type = computation['to']['category']
content = self.compute(computation['content'])
if (target_type == "string"):
return str(content)
elif (target_type == "float"):
return float(content)
elif (target_type == "bool"):
if (origin_type == "string"):
return (content == "true")
elif (origin_type == "int"):
return (content != 0)
elif (target_type == "int"):
if (origin_type == "float"):
return math.floor(content)
else:
return int(content)
elif (computation_category == "constant"):
target_type = computation['type']['category']
content = computation['value']
if (target_type == "string"):
return content
elif (target_type == "float"):
return float(content)
elif (target_type == "bool"):
return (content == "true")
elif (target_type == "int"):
return int(content)
else:
print("Unknown Constant type '" + str(target_type) + "'")
raise "error"
elif (computation_category == "if_else"):
cond = self.compute(computation['condition'])
if (cond):
return self.compute(computation['if_true'])
else:
return self.compute(computation['if_false'])
elif (computation_category == "new"):
address = ".alloc." + str(self.allocated_data)
self.allocated_data += 1
self.memory[address] = self.generate_instance_of(computation['target'])
#print("Allocated " + str(address) + " = " + str(self.memory[address]))
return [address]
elif (computation_category == "operation"):
operator = computation['operator']
x = self.compute(computation['x'])
y = self.compute(computation['y']) if ('y' in computation) else None
if (operator == "divide"):
if (isinstance(x, int)):
return x // y
else:
return x / y
elif (operator == "minus"):
return x - y
elif (operator == "modulo"):
return x % y
elif (operator == "plus"):
return x + y
elif (operator == "power"):
return x ** y
elif (operator == "rand"):
return random.randint(x, y)
elif (operator == "times"):
return x * y
elif (operator == "and"):
return x and y
elif (operator == "not"):
return not x
elif (operator == "less_than"):
return x < y
elif (operator == "equals"):
return x == y
else:
print("unknown operator " + operator)
elif (computation_category == "address"):
result = self.compute(computation['address'])
if (isinstance(result, list)):
return result
else:
return [result]
elif (computation_category == "relative_address"):
base = self.compute(computation['base']).copy()
base.append(self.compute(computation['extra']))
return base
elif (computation_category == "rich_text"):
result = dict()
result['effect'] = None
result['content'] = []
for c in computation['content']:
result['content'].append(self.compute(c))
return result
elif (computation_category == "newline"):
result = dict()
result['effect'] = None
result['content'] = ['\n']
return result
elif (computation_category == "size"):
target = self.memory
access = self.compute(computation['reference'])
for addr in access:
target = target[addr]
return len(target)
elif (computation_category == "value_of"):
target = self.memory
access = self.compute(computation['reference'])
for addr in access:
#print("Reading " + str(addr) + " of " + str(target))
# print("addr = " + str(addr))
target = target[addr]
# if (isinstance(target, list)):
# print("That's a list.")
return target
def resolve_choice_to (self, line):
self.available_choices = []
self.program_counter = line
def run (self):
while True:
#print("\nmemory: " + str(self.memory))
#print("\nline: " + str(self.program_counter))
instruction = self.code[self.program_counter]
instruction_category = instruction['category']
#print("instruction:" + str(instruction))
if (instruction_category == "add_choice"):
self.available_choices.append(
[
self.compute(instruction['label']),
self.compute(instruction['address'])
]
)
self.program_counter += 1
elif (instruction_category == "assert"):
condition = self.compute(instruction['condition'])
if (not condition):
result = dict()
result["category"] = "assert"
result["line"] = self.program_counter
result["message"] = self.compute(instruction['message'])
self.program_counter += 1
return result
self.program_counter += 1
elif (instruction_category == "display"):
result = dict()
result["category"] = "display"
result["content"] = self.compute(instruction['content'])
self.program_counter += 1
return result
elif (instruction_category == "end"):
result = dict()
result["category"] = "end"
return result
elif (instruction_category == "event_call"):
result = dict()
result["category"] = "event"
result["name"] = instruction["event"]
params = []
for param in instruction['parameters']:
params.append(self.compute(param))
result["parameters"] = params
self.program_counter += 1
return result
elif (instruction_category == "remove"):
pre_val = self.memory
current_val = pre_val
last_access = ""
for access in self.compute(instruction["reference"]):
pre_val = current_val
last_access = access
current_val = current_val[access]
#print("Removing " + str(last_access) + " of " + str(pre_val))
del pre_val[last_access]
self.program_counter += 1
elif (instruction_category == "resolve_choices"):
result = dict()
result["category"] = "resolve_choices"
result["choices"] = self.available_choices
return result
elif (instruction_category == "set_pc"):
self.program_counter = self.compute(instruction["value"])
elif (instruction_category == "set_value"):
pre_val = self.memory
current_val = pre_val
last_access = ""
#print("Reference:" + str(instruction["reference"]))
access_full = self.compute(instruction["reference"])
#print("Writing: " + str(access_full))
for access in access_full:
pre_val = current_val
last_access = access
#print("Writing " + str(access) + " of " + str(current_val))
if (access in current_val):
current_val = current_val[access]
result = self.compute(instruction["value"])
if (isinstance(result, list) or isinstance(result, dict)):
result = copy.deepcopy(result)
pre_val[last_access] = result
self.program_counter += 1
|