1:- style_check(-singleton). 2return_(Data,[A]) -->
3 ("return",ws_,A).
4
5initialize_constant_(Data,[Name,Type,Value]) -->
6 ("const",ws_,Name,ws,"=",ws,Value).
7set_array_size_(Data,[Name,Size,Type]) -->
8 ("var",ws_,Name,ws,"=",ws,"Array",ws,".",ws,"apply",ws,"(",ws,"null",ws,",",ws,"Array",ws,"(",ws,Size,ws,")",ws,")",ws,".",ws,"map",ws,"(",ws,"function",ws,"(",ws,")",ws,"{",ws,"}",ws,")").
9
10
11set_var_(Data,[Name,Value]) -->
12 (Name,ws,"=",ws,Value).
13
14
15initialize_var_(Data,[Name,Expr,Type]) -->
16 ("var",ws_,Name,ws,"=",ws,Expr).
17index_in_array_(Data,[Container,Contained]) -->
18 (Container,ws,".",ws,"indexOf",ws,"(",ws,Contained,ws,")").
19
20concatenate_string_(Data,[A,B]) -->
21 (A,python_ws,"+",python_ws,B).
22
23array_length(Data, [A])-->
24 (A,ws,".",ws,"length").
25
26strlen_(Data,[A]) -->
27 (A,ws,".",ws,"length").
28
29access_array_(Data,[Array,Index]) -->
30 (Array,python_ws,"[",python_ws,Index,python_ws,"]").
31
34reverse_list_(Data,[List]) -->
35 (List,ws,".",ws,"map",ws,"(",ws,"function",ws,"(",ws,"arr",ws,")",ws,"{",ws,"return",ws_,"arr",ws,".",ws,"slice()",ws,";",ws,"}",ws,")").
36
37reverse_list_in_place_(Data,[List]) -->
38 (List,python_ws,".",python_ws,"reverse",python_ws,"(",python_ws,")").
39
40charAt_(Data,[AString,Index]) -->
41 (AString,ws,".",ws,"charAt",ws,"(",ws,Index,ws,")").
42join_(Data,[Array,Separator]) -->
43 (Array,ws,".",ws,"join",ws,"(",ws,Separator,ws,")").
44
46concatenate_arrays_(Data,[A1,A2]) -->
47 (A1,ws,".",ws,"concat",ws,"(",ws,A2,ws,")").
48split_(Data,[AString,Separator]) -->
49 (AString,python_ws,".",python_ws,"split",python_ws,"(",python_ws,Separator,python_ws,")").
50
51function_call_(Data,[Name,Args]) -->
52 (Name,python_ws,"(",python_ws,Args,python_ws,")").
53
54minus_minus_(Data,[Name]) -->
55 (Name,ws,"--").
56
57initialize_static_var_with_value_(Data,[Type,Name,Value]) -->
58 ("var",ws_,Name,ws,"=",ws,Value).
59
60
61instance_method_(Data,[Name,Type,Params,Body,Indent]) -->
62 (Name,ws,"(",ws,Params,ws,")",ws,"{",!,ws,Body,(Indent;ws),"}").
63
64static_method_(Data, [Name,Type,Params,Body,Indent]) -->
65 ("static",ws_,Name,ws,"(",ws,Params,ws,")",ws,"{",ws,Body,(Indent;ws),"}").
66
67
68constructor_(Data,[Name,Params,Body,Indent]) -->
69 ("constructor",ws,"(",ws,Params,ws,")",ws,"{",ws,Body,(Indent;ws),"}").
70
71plus_equals_(Data,[A,B]) -->
72 (A,python_ws,"+=",python_ws,B).
73
74array_plus_equals_(Data,[A,B]) -->
75 (A,ws,".",ws,"push",ws,".",ws,"apply",ws,"(",ws,A,ws,",",ws,B,ws,")").
76
77string_plus_equals_(Data,[A,B]) -->
78 (A,python_ws,"+=",python_ws,B).
79
80divide_equals_(Data,[A,B]) -->
81 (A,ws,"/=",ws,B).
82
83modulo_equals_(Data,[A,B]) -->
84 (A,ws,"%=",ws,B).
85
86minus_equals_(Data,[A,B]) -->
87 (A,python_ws,"-=",python_ws,B).
88
89assert_(Data,[A]) -->
90 ("assert",ws,"(",ws,A,ws,")").
91
93log_base_e_(Data,[A]) -->
94 ("Math",ws,".",ws,"log",ws,"(",ws,A,ws,")",!).
95
96
97println_(Data,[A,Type]) -->
98 ("console",ws,".",ws,"log",ws,"(",ws,A,ws,")").
99
100times_equals_(Data,[Name,Expr]) -->
101 (Name,ws,"*=",ws,Expr).
102
103append_to_string_(Data,[Name,Expr]) -->
104 (Name,python_ws,"+=",python_ws,Expr).
105
106append_to_array_(Data,[Name,Expr]) -->
107 (Name,ws,".",ws,"push",ws,"(",ws,Expr,ws,")").
108
109throw_(Data,[A]) -->
110 ("throw",ws_,A).
111
112
113initialize_empty_var_(Data,[Name,Type]) -->
114 ("var",ws_,Name).
115set_dict_(Data,[Name,Index,Value]) -->
116 (Name,ws,"[",ws,Index,ws,"]",ws,"=",ws,Value).
117
118initializer_list_(Data,[A,Type]) -->
119 ("[",python_ws,A,python_ws,"]").
121
122dict_(Data,[A,Type]) -->
123 ("{",python_ws,A,python_ws,"}").
124
125
126tan_(Data,[Var1]) -->
127
128 ("Math",ws,".",ws,"tan",ws,"(",ws,Var1,ws,")").
129
130false_(Data) -->
131 "false".
132
133true_(Data) -->
134 ("true").
135acos_(Data,[Var1]) -->
136 ("Math",ws,".",ws,"acos",ws,"(",ws,Var1,ws,")").
137
138asin_(Data,[Var1]) -->
139 ("Math",ws,".",ws,"asin",ws,"(",ws,Var1,ws,")").
140
141atan_(Data,[Var1]) -->
142 ("Math",ws,".",ws,"atan",ws,"(",ws,Var1,ws,")").
143
144
145sinh_(Data,[Var1]) -->
146 ("Math",ws,".",ws,"sinh",ws,"(",ws,Var1,ws,")").
147cosh_(Data,[Var1]) -->
148 ("Math",ws,".",ws,"cosh",ws,"(",ws,Var1,ws,")").
149
151abs_(Data,[Var1]) -->
152
153 ("Math",ws,".",ws,"abs",ws,"(",ws,Var1,ws,")").
154
155sin_(Data,[Var1]) -->
156 ("Math",ws,".",ws,"sin",ws,"(",ws,Var1,ws,")").
157cos_(Data,[Var1]) -->
158 ("Math",ws,".",ws,"cos",ws,"(",ws,Var1,ws,")").
159
161ceiling_(Data,[Params]) -->
162 ("Math",ws,".",ws,"ceil",ws,"(",ws,Params,ws,")").
163
165floor_(Data,[Params]) -->
166 ("Math",ws,".",ws,"floor",ws,"(",ws,Params,ws,")").
167
168
169anonymous_function_(Data,[Type,Params,B]) -->
170 (
171 "function",ws,"(",ws,Params,ws,")",ws,"{",ws,B,ws,"}";
172 173 "(",ws,Params,ws,")",ws,"=>",ws,"{",ws,B,ws,"}"
174 ).
175
176type_conversion_(Data,[string,[array,char],Arg]) -->
177 ("Array",ws,".",ws,"from",ws,"(",ws,Arg,ws,")").
178
179type_conversion_(Data,[int,string,Arg]) -->
180 ("String",ws,"(",ws,Arg,ws,")").
181
182type_conversion_(Data,[string,int,Arg]) -->
183 ("parseInt",ws,"(",ws,Arg,ws,")").
184type_conversion_(Data,[string,double,Arg]) -->
185 ("Number",ws,"(",ws,Arg,ws,")").
186type_conversion_(Data,[double,string,Arg]) -->
187 ("toString",ws,"(",ws,Arg,ws,")").
188
189static_method_call_(Data,[Class_name,Function_name,Args]) -->
190 (Class_name,".",Function_name,ws,"(",ws,Args,ws,")").
191instance_method_call_(Data,[Instance_name,Function_name,Args]) -->
192 (Instance_name,".",Function_name,ws,"(",ws,Args,ws,")").
193
194plus_plus_(Data,[Name]) -->
195 (Name,ws,"++").
196set_array_index_(Data,[Name,Index,Value]) -->
197 set_var_(Data,[access_array_(Data,[Name,Index]),Value]).
198
199mod_(Data,[A,B]) -->
200 (A,python_ws,"%",python_ws,B).
201
202arithmetic_(Data,[Exp1,Exp2,Symbol]) -->
203 (Exp1,ws,Symbol,ws,Exp2).
204new_regex_(Data,[A]) -->
205 ("new",ws,"RegExp",ws,"(",ws,A,ws,")").
206foreach_with_index_(Data,[Array,Var,Index,Type,Body,Indent]) -->
207 (Array,ws,".",ws,"forEach",ws,"(",ws,"function",ws,"(",ws,Var,ws,",",ws,Index,ws,")",ws,"{",ws,Body,(Indent;ws),"}",ws,")",ws,";").
208
209
210foreach_(Data,[Array,Var,Type,Body,Indent]) -->
211 (Array,ws,".",ws,"forEach",ws,"(",ws,"function",ws,"(",ws,Var,ws,")",ws,"{",!,ws,Body,(Indent;ws),"}",ws,")",ws,";").
212switch_(Data,[A,B,Indent]) -->
213 ("switch",ws,"(",!,ws,A,ws,")",ws,"{",!,ws,B,(Indent;ws),"}").
214
215if_without_else_(Data,[A,B,Indent]) -->
216 ("if",ws,"(",ws,A,ws,")",ws,"{",ws,B,(Indent;ws),"}").
217if(Data,[A,B,C,D,Indent]) -->
218 (if_without_else_(Data,[A,B,Indent]),(Indent;ws),C,(Indent;ws),D).
219do_while_(Data,[Condition,Body,Indent]) -->
220 ("do",ws,"{",!,ws,Body,(Indent;ws),"}",ws,"while",ws,"(",ws,Condition,ws,")",ws,";").
221
222
223while_(Data,[A,B,Indent]) -->
224 ("while",ws,"(",ws,A,ws,")",ws,"{",!,ws,B,(Indent;ws),"}").
225for_(Data,[Statement1,Condition,Statement2,Body,Indent]) -->
226 ("for",ws,"(",!,ws,Statement1,ws,";",ws,Condition,ws,";",ws,Statement2,ws,")",ws,"{",!,ws,Body,(Indent;ws),"}").
227
228semicolon_(Data,[A]) -->
229 (A,ws,";",!).
230
231
232class_extends_(Data,[C1,C2,B,Indent]) -->
233 ("class",ws_,C1,ws_,"extends",!,ws_,C2,ws,"{",!,ws,B,(Indent;ws),"}").
234
235class_(Data,[Name,Body,Indent]) -->
236 ("class",ws_,Name,ws,"{",!,ws,Body,(Indent;ws),"}").
237
238
239function_(Data,[Name,Type,Params,Body,Indent]) -->
240 ("function",ws_,Name,ws,"(",!,ws,Params,ws,")",ws,"{",!,ws,Body,(Indent;ws),"}").
241reserved_words(A) :-
242 forall(member(B,["end","sin","cos","tan","abs","type","writeln","indexOf","charAt","gets","sample","array","readline","array_rand","input","random","choice","randrange","list","print","print_int","print_string","String","string","int","sort","sorted","reverse","sha1","reversed","len","unique_everseen","True","Number","float","double","return","def","str","char","boolean","function","false","true","enumerate"]),dif(A,B)).
243
244var_name_(Data,Type,A) -->
245 symbol(A),!.
246
247else(Data,[Indent,A]) -->
248 ("else",ws,"{",!,ws,A,(Indent;ws),"}").
249
250
251parameter_separator(Data) -->
252 ",".
253
254
255parameter_(Data,[Type,Name]) -->
256 (Name).
257
258import_(Data,[A]) -->
259 ("import",ws_,"*",ws_,"as",ws_,A,ws_,"from",ws_,"'",ws,A,ws,"'",ws,";",!).
260
(Data,[A]) -->
262 ("//",A).
263
264first_case_(Data,[B,Compare_expr,Expr,Case_or_default]) -->
265 ("case",ws_,Expr,ws,":",!,ws,B,ws,"break",ws,";",!,ws,Case_or_default).
266
267case(Data,[A,B,Expr,Case_or_default,Indent]) -->
268 ("case",ws_,Expr,ws,":",!,ws,B,ws,"break",ws,";",!,ws,Case_or_default).
269
270
271default(Data,[A,Indent]) -->
272 ("default",ws,":",!,ws,A).
273
274elif(Data,[Indent,A,B]) -->
275 ("else",ws_,"if",ws,"(",!,ws,A,ws,")",!,ws,"{",!,ws,B,(Indent;ws),"}").
276default_parameter_(Data,[Type,Name,Value]) -->
277 (Name,python_ws,"=",python_ws,Value).
278
280random_int_in_range(Data,[Min,Max]) -->
281 ("(",ws,"Math",ws,".",ws,"floor",ws,"(",ws,"Math",ws,".",ws,"random",ws,"(",ws,")",ws,"*",ws,"(",ws,Max,ws,"-",ws,Min,ws,"+",ws,"1",ws,")",ws,"+",ws,Min,ws,")").
282
284random_from_list(Data,[Arr]) -->
285 (Arr,python_ws,"[Math.floor(Math.random()*",python_ws,Arr,python_ws,".length)]").
286
287random_number(Data) -->
288 "Math",ws,".",ws,"random",ws,"(",ws,")",!.
289
290type(Data,regex) -->
291 "RegExp".
292
293type(Data,[dict,Type_in_dict]) -->
294 "Object".
295type(Data,int) -->
296 "number".
297
298type(Data,string) -->
299 "string".
300
301
302type(Data, bool) -->
303 "boolean".
304
305type(Data,void) -->
306 "void".
307
308type(Data,double) -->
309 "number".
310
312type(Data,[array,Type]) -->
313 {ground(Type)}, 314 "Array".
315
316
317statement_separator(Data) -->
318 ws.
319initializer_list_separator(Data) -->
320 ",".
321key_value_separator(Data) -->
322 ",".
323
324
325top_level_statement_(Data,A) -->
326 A.
328regex_literal_(Data,[S]) -->
329 ("/",S,"/").
330
331include_in_each_file(Data) -->
332 "".
333
337reverse_string_(Data,[Str]) -->
338 ("esrever",ws,".",ws,"reverse",ws,"(",ws,Str,ws,")").
339
340key_value_(Data,[A,B]) -->
341 (A,ws,":",!,ws,B).
342
343array_slice_(Data,[A,B,C]) -->
344 (A,ws,".","slice","(",ws,B,ws,",",ws,C,ws,")").
345
346compare_(Data,Type,[A,B]) -->
347 (A,ws,"===",ws,B),{Type=int;Type=string;Type=bool}.
348
349less_than_(_,[A,B]) -->
350 (infix_operator("<",A,B)).
351
352less_than_or_equal_to_(_,[A,B]) -->
353 infix_operator("<=",A,B).
354
355
356greater_than_or_equal_to_(_,[A,B]) -->
357 A,ws,">=",ws,B.
358
359greater_than_(_,[A,B]) -->
360 infix_operator(">",A,B).
361
362string_not_equal_(_,[A,B]) -->
363 (infix_operator("!==",A,B)).
364
365int_not_equal_(_,[A,B]) -->
366 (infix_operator("!==",A,B)).
367
368pow_(_,[A,B]) -->
369("Math",ws,".",ws,"pow",ws,"(",!,ws,A,ws,",",ws,B,ws,")").
370
371sqrt(_,[X]) -->
372 ("Math",ws,".",ws,"sqrt",ws,"(",!,ws,X,ws,")").
373list_comprehension_(_,[Variable,Array,Result,Condition]) -->
374 ("[",ws,Result,ws_,"for",ws,"(",ws,Variable,ws_,"of",ws_,Array,ws,")",ws,"if",ws_,Condition,ws,"]").
375
376startswith_(_,[Str1,Str2]) -->
377 (Str1,ws,".",ws,"startsWith",ws,"(",!,ws,Str2,ws,")").
378endswith_(_,[Str1,Str2]) -->
379 (Str1,ws,".",ws,"endsWith",ws,"(",!,ws,Str2,ws,")").
382trim_(_,[Str]) -->
383 (Str,ws,".",ws,"trim",ws,"(",!,ws,")").
384
386lstrip_(_,[Str]) -->
387 (Str,ws,".replace(/^\s+/,'')").
388
390rstrip_(_,[Str]) -->
391 (Str,ws,".replace(/\s+$/,'')").
392
393lowercase_(_,[Str]) -->
394 (Str,ws,".",ws,"toLowerCase",ws,"(",!,ws,")").
395
396array_contains(_,[Container,Contained]) -->
397 ("(",ws,Container,ws,".",ws,"indexOf",ws,"(",ws,Contained,ws,")",ws,"!==",ws,"-1",ws,")";
398 Container,ws,".",ws,"includes",ws,"(",ws,Contained,ws,")").
399
401string_contains_(_,[Str1,Str2]) -->
402("(",ws,Str1,ws,".",ws,"indexOf",ws,"(",ws,Str2,ws,")",ws,(">";"!==";"!="),ws,"-1",ws,")").
403
404this_(_,[A]) -->
405 ("this",ws,".",!,ws,A).
406
407access_dict_(_,[Dict,Dict_,Index]) -->
408 ((Dict,ws,"[",ws,Index,ws,"]")).
409
410command_line_args_(_) -->
411 ("process",ws,".",ws,"argv",ws,".",ws,"slice",ws,"(",ws,"2",ws,")").
412
413call_constructor_(_,[Name,Args]) -->
414 ("new",ws_,Name,ws,"(",ws,Args,ws,")").
415
416regex_matches_string_(_,[Reg,Str]) -->
417 (Reg,ws,".",ws,"test",ws,"(",ws,Str,ws,")").
418
419
420dict_keys_(_,[A]) -->
421 ("Object",ws,".",ws,"keys",ws,"(",ws,A,ws,")").
422
423
425global_replace_in_string_(_,[Str,Sub,Replacement]) -->
426 (Str,ws,".",ws,"split",ws,"(",ws,Sub,ws,")",ws,".",ws,"join",ws,"(",ws,Replacement,ws,")").
427
429index_of_substring_(_,[String,Substring]) -->
430 (String,ws,".",ws,"indexOf",ws,"(",ws,Substring,ws,")").
431
432substring_(_,[A,B,C]) -->
433 (A,ws,".",ws,"substring",ws,"(",ws,B,ws,",",ws,C,ws,")").
434not_(_,[A]) -->
435 ("!",A).
436
437and_(_,[A,B]) -->
438 (A,ws,"&&",ws,B).
439
440sort_in_place_(_,[List]) -->
441 (List,python_ws,".",python_ws,"sort",python_ws,"(",python_ws,")").
442reverse_sort_in_place_(_,[List]) -->
443 (List,ws,".",ws,"sort",ws,"(",ws,")",ws,".",ws,"reverse",ws,"(",ws,")").
444
445uppercase_(_,[Str]) -->
446 (Str,ws,".",ws,"toUpperCase",ws,"(",ws,")").
447
448
450pi_(_) -->
451 ("Math",ws,".",ws,"PI").
452
453
454eager_and_(_,[Var1,Var2]) -->
455 (Var1,python_ws,"&",python_ws,Var2).
456eager_or_(_,[Var1,Var2]) -->
457 (Var1,python_ws,"|",python_ws,Var2).
458
459or_(_,[Var1,Var2]) -->
460 (Var1,ws,"||",!,ws,Var2).
461last_index_of_(_,[String,Substring]) -->
462 (String,ws,".",ws,"lastIndexOf",ws,"(",ws,Substring,ws,")").
463
464
465optional_indent(Data,Indent) -->
466 {Data = [Lang,_,_,Indent],
467 offside_rule_langs(Offside_rule_langs)},
468 {memberchk(Lang,Offside_rule_langs)}->
469 Indent;
470 (Indent;"").
471
473declare_vars_(_,[Vars,Type]) -->
474 ("var",ws_,Vars).
476initialize_vars_(_,[Vars,Type]) -->
477 ("var",ws_,Vars).
478
479
480try_catch_(Data,[Body1,Name,Body2,Indent]) -->
481("try",ws,"{",ws,Body1,ws,"}",ws,"catch",ws,"(",ws,Name,ws,")",ws,"{",ws,Body2,ws,"}").
484sort_(Data,[List]) -->
485 (List,ws,".",ws,"sort",ws,"(",ws,")")