forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedy_translation.py
351 lines (237 loc) · 10.4 KB
/
hedy_translation.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
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
import textwrap
from os.path import dirname
from lark import Transformer, Tree
import hedy
import yaml
from os import path
TRANSPILER_LOOKUP = {}
def keywords_to_dict(to_lang="nl"):
""""Return a dictionary of keywords from language of choice. Key is english value is lang of choice"""
base = path.abspath(path.dirname(__file__))
keywords_path = 'coursedata/keywords/'
yaml_filesname_with_path = path.join(base, keywords_path, to_lang + '.yaml')
with open(yaml_filesname_with_path, 'r') as stream:
command_combinations = yaml.safe_load(stream)
return command_combinations
def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1):
""""Return code with keywords translated to language of choice in level of choice"""
parser = hedy.get_parser(level, from_lang)
punctuation_symbols = ['!', '?', '.']
keyword_dict = keywords_to_dict(to_lang)
input_string = hedy.process_input_string(input_string, level)
program_root = parser.parse(input_string + '\n').children[0]
hedy.ExtractAST().transform(program_root)
translator = TRANSPILER_LOOKUP[level]
abstract_syntaxtree = translator(keyword_dict, punctuation_symbols).transform(program_root)
return abstract_syntaxtree
def hedy_translator(level):
def decorating(c):
TRANSPILER_LOOKUP[level] = c
c.level = level
return c
return decorating
def indent(s):
newIndent = ""
for line in s:
lines = line.split('\n')
newIndent += ''.join(['\n ' + l for l in lines])
return newIndent
@hedy_translator(level=1)
class ConvertToLang1(Transformer):
def __init__(self, keywords, punctuation_symbols):
self.keywords = keywords
self.punctuation_symbols = punctuation_symbols
__class__.level = 1
def command(self, args):
return args[0]
def program(self, args):
return '\n'.join([str(c) for c in args])
def text(self, args):
return ''.join([str(c) for c in args])
def invalid_space(self, args):
return " " + ''.join([str(c) for c in args])
def print(self, args):
return self.keywords["print"] + " " + "".join([str(c) for c in args])
def echo(self, args):
all_args = self.keywords["echo"]
if args:
all_args += " "
return all_args + "".join([str(c) for c in args])
def ask(self, args):
return self.keywords["ask"] + " " + "".join([str(c) for c in args])
def turn(self, args):
return self.keywords["turn"] + " " + "".join([str(c) for c in args])
def forward(self, args):
return self.keywords["forward"] + " " + "".join([str(c) for c in args])
def random(self, args):
return self.keywords["random"] + "".join([str(c) for c in args])
def invalid(self, args):
return ' '.join([str(c) for c in args])
def __default__(self, data, children, meta):
return Tree(data, children, meta)
@hedy_translator(level=2)
class ConvertToLang2(ConvertToLang1):
def assign(self, args):
return args[0] + " " + self.keywords["is"] + " " + ''.join([str(c) for c in args[1:]])
def print(self, args):
argument_string = ""
i = 0
for argument in args:
# escape quotes if kids accidentally use them at level 2
argument = hedy.process_characters_needing_escape(argument)
# final argument and punctuation arguments do not have to be separated with a space, other do
if i == len(args) - 1 or args[i + 1] in self.punctuation_symbols:
space = ''
else:
space = " "
argument_string += argument + space
i = i + 1
return self.keywords["print"] + " " + argument_string
def punctuation(self, args):
return ''.join([str(c) for c in args])
def var(self, args):
var = args[0]
all_parameters = ["'" + hedy.process_characters_needing_escape(a) + "'" for a in args[1:]]
return var + ''.join(all_parameters)
def ask(self, args):
var = args[0]
all_parameters = [hedy.process_characters_needing_escape(a) for a in args]
return all_parameters[0] + " " + self.keywords["is"] + " " + self.keywords["ask"] + " " + ''.join(
all_parameters[1:])
def ask_dep_2(self, args):
return self.keywords["ask"] + " " + ''.join([str(c) for c in args])
def echo_dep_2(self, args):
return self.keywords["echo"] + " " + ''.join([str(c) for c in args])
@hedy_translator(level=3)
class ConvertToLang3(ConvertToLang2):
def ask(self, args):
var = args[0]
remaining_args = args[1:]
return var + " " + self.keywords["is"] + " " + self.keywords["ask"] + " " + ''.join(remaining_args)
def var_access(self, args):
return ''.join([str(c) for c in args])
def assign_list(self, args):
return args[0] + " " + self.keywords["is"] + " " + ', '.join([str(c) for c in args[1:]])
def list_access(self, args):
return args[0] + " " + self.keywords["at"] + " " + ''.join([str(c) for c in args[1:]])
@hedy_translator(level=4)
class ConvertToLang4(ConvertToLang3):
def print(self, args):
i = 0
# self.check_args_type_allowed(args, 'print', self.level)
argument_string = ""
for argument in args:
if i == len(args) or args[i] in self.punctuation_symbols:
space = ''
else:
space = " "
argument_string += space + argument
i += 1
return self.keywords["print"] + argument_string
def print_nq(self, args):
return ConvertToLang2.print(self, args)
@hedy_translator(level=5)
class ConvertToLang5(ConvertToLang4):
def ifs(self, args):
return self.keywords["if"] + " " + ''.join([str(c) for c in args])
def ifelse(self, args):
return self.keywords["if"] + " " + args[0] + args[1] + " " + self.keywords["else"] + " " + args[2]
def condition(self, args):
return ' and '.join(args)
def equality_check(self, args):
return args[0] + " " + self.keywords["is"] + " " + " ".join([str(c) for c in args[1:]]) + " "
def in_list_check(self, args):
return args[0] + " " + self.keywords["in"] + " " + ''.join([str(c) for c in args[1:]]) + " "
@hedy_translator(level=6)
class ConvertToLang6(ConvertToLang5):
def addition(self, args):
return args[0] + " + " + args[1]
def substraction(self, args):
return args[0] + " - " + args[1]
def multiplication(self, args):
return args[0] + " * " + args[1]
def division(self, args):
return args[0] + " / " + args[1]
@hedy_translator(level=7)
class ConvertToLang7(ConvertToLang6):
def repeat(self, args):
return self.keywords["repeat"] + " " + args[0] + " " + self.keywords["times"] + " " + args[1]
@hedy_translator(level=8)
class ConvertToLang8(ConvertToLang7):
def command(self, args):
return '\n'.join([str(c) for c in args])
def repeat(self, args):
return self.keywords["repeat"] + " " + args[0] + " " + self.keywords["times"] + indent(args[1:])
def ifs(self, args):
return self.keywords["if"] + " " + args[0] + indent(args[1:])
def elses(self, args):
return self.keywords["else"] + indent(args[0:])
def equality_check(self, args):
return args[0] + " " + self.keywords["is"] + " " + " ".join([str(c) for c in args[1:]])
def end_block(self, args):
return args
@hedy_translator(level=9)
@hedy_translator(level=10)
class ConvertToLang9_10(ConvertToLang8):
def repeat_list(self, args):
return self.keywords["for"] + " " + args[0] + " " + self.keywords["in"] + " " + args[1] + indent(args[2:])
@hedy_translator(level=11)
class ConvertToLang11(ConvertToLang9_10):
def for_loop(self, args):
return self.keywords["for"] + " " + args[0] + " " + self.keywords["in"] + " " + \
self.keywords["range"] + " " + args[1] + " " + self.keywords["to"] + " " + args[2] + indent(args[3:])
@hedy_translator(level=12)
class ConvertToLang12(ConvertToLang11):
def text_in_quotes(self, args):
return ''.join(["'" + str(c) + "'" for c in args])
@hedy_translator(level=13)
class ConvertToLang13(ConvertToLang12):
def andcondition(self, args):
returnString = args[0]
for arg in args[1:]:
returnString += " " + self.keywords["and"] + " " + arg
return returnString
def orcondition(self, args):
returnString = args[0]
for arg in args[1:]:
returnString += " " + self.keywords["or"] + " " + arg
return returnString
def in_list_check(self, args):
return args[0] + " " + self.keywords["in"] + " " + ''.join([str(c) for c in args[1:]])
@hedy_translator(level=14)
class ConvertToLang14(ConvertToLang13):
def bigger(self, args):
return args[0] + " > " + args[1]
def smaller(self, args):
return args[0] + " < " + args[1]
def bigger_equal(self, args):
return args[0] + " >= " + args[1]
def smaller_equal(self, args):
return args[0] + " <= " + args[1]
def not_equal(self,args):
return args[0] + " != " + args[1]
@hedy_translator(level=15)
class ConvertToLang15(ConvertToLang14):
def while_loop(self, args):
return self.keywords["while"] + " " + args[0] + indent(args[1:])
@hedy_translator(level=16)
class ConvertToLang16(ConvertToLang15):
def assign_list(self, args):
return args[0] + " " + self.keywords["is"] + " [" + ', '.join([str(c) for c in args[1:]]) + "]"
def list_access(self, args):
return args[0] + "[" + ''.join([str(c) for c in args[1:]]) + "]"
@hedy_translator(level=17)
class ConvertToLang17(ConvertToLang16):
def for_loop(self, args):
return self.keywords["for"] + " " + args[0] + " " + self.keywords["in"] + " " + \
self.keywords["range"] + " " + args[1] + " " + self.keywords["to"] + " " + args[2] + ":" + indent(args[3:])
def while_loop(self, args):
return self.keywords["while"] + " " + args[0] + ":" + indent(args[1:])
def repeat_list(self, args):
return self.keywords["for"] + " " + args[0] + " " + self.keywords["in"] + " " + args[1] + ":" + indent(args[2:])
def ifs(self, args):
return self.keywords["if"] + " " + args[0] + ":" + indent(args[1:])
def elses(self, args):
return self.keywords["else"] + ":" + indent(args[0:])
def elifs(self, args):
return self.keywords["elif"] + " " + args[0] + ":" + indent(args[1:])