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