-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunsnake_compiler.js
665 lines (588 loc) · 22.3 KB
/
sunsnake_compiler.js
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
print = console.log
_class_names = ['Entity', 'Button', 'Text', 'HealthBar', 'RainbowSlider', 'InputField', 'TextField', 'StateHandler', 'Scene', 'Array2D', 'Video']
_language = 'sunsnake'
Array.prototype.at = function(i) {
if (i < 0) {
i = this.length + i
}
return this[i]
}
try {''.replaceAll('', '')} // make sure replaceAll is supported, else implement it.
catch {String.prototype.replaceAll = function replaceAll(search, replace) { return this.split(search).join(replace); }}
function compile(script) {
try {t = performance.now()} catch {}
// start parsing
script = script.replaceAll(',\n', ',')
script = script.replaceAll('(\n', '(')
script = script.replaceAll('{\n', '{')
script = script.replaceAll('[\n', '[')
script = script.replaceAll('=\n', '=')
script = script.replaceAll('+\n', '+')
script = script.replaceAll(' == ', ' === ')
script = script.replaceAll('.index(', '.indexOf(')
script = script.replaceAll(' self.', ' this.')
script = script.replaceAll('(self.', '(this.')
// script = script.replaceAll('(self.', '(this.')
var all_lines = script.split('\n')
var lines = []
lines.push('\n')
strings = []
string_index = 0
const regexp = '\'(.*?)\''
const regexp_backtick = '\`(.*?)\`'
extra_replacements = []
is_in_multiline_string = false
prev_created_variable = null // for storing the recently defined variable starting with $, so it can be references with '_.'
for (var i=0; i<all_lines.length; i++) {
if (!all_lines[i].trim()) {
// print('skip line')
continue
}
line = all_lines[i].trimStart()
if (line.startsWith('//')) {
continue
}
if (line.startsWith('#')) {
continue
}
if (all_lines[i].includes(': #')) {
all_lines[i] = all_lines[i].split(': #')[0] + ':'
}
if (line == `'language=python'`) {
_language = 'python'
}
else if (line == `'language=sunsnake'`) {
_language = 'sunsnake'
}
if (_language == 'python') {
continue
}
// if (is_in_multiline_string && !line.trimEnd().endsWith("'''")) {
// lines.push(`"${all_lines[i]}" +`)
// continue
// }
// if (line.trimEnd().endsWith("'''")) {
// is_in_multiline_string = !is_in_multiline_string
// if (is_in_multiline_string) {
// all_lines[i] = line.slice(0,-3) + '"" +'
// }
// else {
// all_lines[i] = line.slice(0,-3) + '""'
// }
// }
// remove text so it doesn't get parsed as code.
quotes = [...all_lines[i].matchAll(regexp)];
quotes.push(...all_lines[i].matchAll(regexp_backtick))
for (var j=0; j<quotes.length; j++) {
if (quotes[j][1].startsWith('#')) {
continue
}
if (quotes[j][1].length > 0) {
strings.push(quotes[j][1])
// print('TEXT_CONTENT_$', string_index, quotes[j][1])
all_lines[i] = all_lines[i].replace(`'${quotes[j][1]}'`, `[TEXT_CONTENT_${string_index}]`)
string_index += 1
}
}
// after keyword for easier invoke()
if (line.startsWith('after ') && all_lines[i].trimEnd().endsWith(':')) {
start_indent = get_indent(all_lines[i])
all_lines[i] = all_lines[i].replaceAll('after ', 'after(')
all_lines[i] = all_lines[i].slice(0,-1) + ', function()'
for (var j=i+1; j<all_lines.length; j++) {
if (get_indent(all_lines[j]) <= start_indent) {
break
}
}
}
if (all_lines[i].startsWith('class ')) {
class_name = all_lines[i].slice(6).split(' ')[0]
print('found class:', class_name)
_class_names.push(class_name)
}
lines.push(all_lines[i])
}
for (var i=0; i<lines.length; i++) {
if (lines[i].endsWith('\\')) {
continue
}
if (lines[i].trimStart().startsWith('sunsnake.define(')) {
content = lines[i].slice(16, -1)
const [key, ...rest] = content.split(',')
const value = rest.join(',')
content = [key, value]
extra_replacements.push(content)
lines[i] = ''
continue
}
if (lines[i].trimStart().startsWith('import ')) {
module_name = lines[i].slice(7)
lines[i] = `(async () => await __import__('${module_name}'))();`
continue
}
for (e of extra_replacements) {
lines[i] = lines[i].replaceAll(e[0], e[1])
}
lines[i] = lines[i].replaceAll(' and ', ' && ')
lines[i] = lines[i].replaceAll(' or ', ' || ')
lines[i] = lines[i].replaceAll(' not ', ' ! ')
lines[i] = lines[i].replaceAll('(not ', '(! ')
lines[i] = lines[i].replaceAll('def ', 'function ')
lines[i] = lines[i].replaceAll('def():', 'function()')
lines[i] = lines[i].replaceAll('.append(', '.push(')
lines[i] = lines[i].replaceAll('.add(', '.push(')
lines[i] = lines[i].replaceAll('.sum()', '.reduce((a, b) => a + b, 0)')
lines[i] = lines[i].replaceAll('.endswith(', '.endsWith(')
lines[i] = lines[i].replaceAll('[-1]', '.at(-1)')
lines[i] = lines[i].replaceAll(' # ', ' //') // comments
// list comprehention
if (lines[i].includes('[') && lines[i].includes(']') && lines[i].includes(' for ') && lines[i].includes(' in ') && !lines[i].endsWith(':') && !lines[i].includes('[[')) {
// remove part before list comprehension
if (lines[i].includes(' = [')) {
code_before_list_comprehension = lines[i].split(' = [')[0] + ' = '
list_comprehension = lines[i].split(' = [')[1]
}
else {
code_before_list_comprehension = lines[i].split('[')[0]
list_comprehension = lines[i].split('[')[1]
}
code_after_list_comprehension = list_comprehension.substring(list_comprehension.lastIndexOf(']')+1)
list_comprehension = list_comprehension.substring(0, list_comprehension.lastIndexOf(']'))
// print('code_after_list_comprehension:', code_after_list_comprehension)
// print('list_comprehension:', list_comprehension)
first_var = list_comprehension.split(' for ')[0]
element_name = list_comprehension.split(' for ')[1].split(' in ')[0]
target_list = list_comprehension.split(' in ')[1]
if (target_list.includes(' if ')) {
target_list = target_list.split(' if ')[0]
}
condition = ''
if (list_comprehension.includes(' if ')) {
condition = list_comprehension.split(target_list)[1].split(' if ')[1] // get the part after the target list and remove the if and the last ]
}
map_code = ''
if (first_var != element_name) {
map_code = `.map(${element_name} => ${first_var})`
}
filter_code = ''
if (condition != '') {
filter_code = `.myCustomFilter(${element_name} => {if (${condition}) return ${element_name}})`
}
lines[i] = `${code_before_list_comprehension}${target_list}${map_code}${filter_code}${code_after_list_comprehension}`
}
if (lines[i].endsWith(':')) {
lines[i] = lines[i].slice(0,-1)
}
// ifs
if (lines[i].trimStart().startsWith('if ')) {
lines[i] = lines[i].replace('if ', 'if (')
lines[i] = lines[i] + ')'
}
// elifs
else if (lines[i].trimStart().startsWith('elif ')) {
lines[i] = lines[i].replace('elif ', 'else if (')
lines[i] = lines[i] + ')'
}
// dict iteration
// print('ITERATE DICT', lines[i].trimStart().startsWith('for key, value in '), lines[i].includes('.items()'))
else if (lines[i].trimStart().startsWith('for key, value in ') && lines[i].includes('.items()')) {
// print('ITERATE DICT')
var dict_name = lines[i].split('for key, value in ')[1].split('.items()')[0]
lines[i] = lines[i].replace('for key, value in ', 'for (let [key, value] of ')
lines[i] = lines[i].replace(`${dict_name}.items()`, `Object.entries(${dict_name}))`)
}
// for loops
else if (lines[i].trimStart().startsWith('for ') && lines[i].includes(' in ')) {
start = lines[i].split('for ')[0] // keep indentation
elements = lines[i].split('for ')[1].split(' in ')[0]
array = lines[i].split(' in ')[1]
// normal for loop
if (!elements.includes(', ')) {
lines[i] = lines[i].replace('for ', 'for (let ')
lines[i] = lines[i].replace(' in ', ' of ')
lines[i] = lines[i] + ')'
}
// auto enumerate, match 'for ?, ? in ?'
else {
elements = `[${elements}]`
if (!array.startsWith('enumerate(')) {
array = `enumerate(${array})`
}
lines[i] = `${start}for (let ${elements} of ${array})`
}
}
// is in list
if (lines[i].includes(' in ') && !lines[i].includes('for ')) {
word_before_in = lines[i].split(' in ')[0].split(' ').pop()
if (word_before_in.startsWith('(')) {
word_before_in = word_before_in.slice(1) // remove first and last
}
// print('word before:', word_before_in)
word_after_in = lines[i].split(' in ')[1].split(' ')[0]
if (word_after_in.endsWith(')') && !word_after_in.endsWith('()')) {
word_after_in = word_after_in.slice(0,-1)
}
// print('word after:', word_after_in)
lines[i] = lines[i].replace(`${word_before_in} in ${word_after_in}`, `${word_after_in}.includes(${word_before_in})`)
}
// multiline string wrappend in triple backticks```
if (!is_in_multiline_string && lines[i].trimEnd().endsWith('```') && lines[i].trim() != '```') { // start
is_in_multiline_string = true
lines[i] = lines[i].replace('```', '(')
}
else if (is_in_multiline_string && !lines[i].includes('```')) {
lines[i] = ' '.repeat(get_indent(lines[i])) + '`' + lines[i].trimStart() + '\n` +'
}
else if (is_in_multiline_string && lines[i].includes('```')) { // end
is_in_multiline_string = false
lines[i] = lines[i].replace('```', '``)')
}
if (lines[i].startsWith('$')) {
prev_created_variable = lines[i].substring(1).split('=')[0].trimEnd()
lines[i] = lines[i].split('$')[1]
}
else if (lines[i].includes('_.') && prev_created_variable) {
// indent = get_indent(lines[i])
// start = ' '.repeat(indent)
// end = lines[i].trimStart().substring(1)
// end = end.replaceAll('_.', prev_created_variable)
// print('---:', lines[i].trimStart().substring(1))
// lines[i] = start + prev_created_variable + end
lines[i] = lines[i].replaceAll('_.', prev_created_variable+'.')
lines[i] = lines[i].replaceAll('_,', prev_created_variable+',')
}
// convert dict(...) to {...}
for (var class_name of ['dict', ]) {
if (lines[i].includes(`${class_name}({`)) {
continue
}
if (lines[i].startsWith(`${class_name}(`) || lines[i].includes(` ${class_name}(`)) {
lines[i] = convert_arguments(lines[i], class_name)
}
}
// add 'new' in front of class initializations
for (var class_name of _class_names) {
is_first_word = lines[i].startsWith(`${class_name}(`) ? '' : ' ' // don't add space if line starts with 'Entity(', do add otherwise, to ensure we match the whole name
if (lines[i].includes(`${is_first_word}${class_name}(`)) {
lines[i] = lines[i].replace(`${is_first_word}${class_name}(`, `${is_first_word}new ${class_name}(`)
lines[i] = convert_arguments(lines[i], class_name)
}
if (lines[i].startsWith(` new `)) {
lines[i] = lines[i].trimStart()
}
}
// units
for (var n=0; n<10; n++) {
lines[i] = lines[i].replaceAll(`${n}ms`, `${n}*.001`)
lines[i] = lines[i].replaceAll(`${n}s`, `${n}`)
lines[i] = lines[i].replaceAll(`${n}m`, `${n}*60`)
lines[i] = lines[i].replaceAll(`${n}h`, `${n}*60*60`)
lines[i] = lines[i].replaceAll(` in ${n}:`, ` in range(${n}):`)
lines[i] = lines[i].replaceAll(`${n}%`, `${n}*.01`)
}
}
// add brackets based on indentation
current_indent = 0
after_statement_indents = []
for (var i=1; i<lines.length; i++) {
prev_line = lines[i-1].trimEnd()
if (prev_line.endsWith('=') || prev_line.endsWith('+') || prev_line.endsWith('(')) {
continue
}
prev_line_indent = get_indent(lines[i-1])
current_line_indent = get_indent(lines[i])
if (current_line_indent > prev_line_indent) {
lines[i-1] = prev_line + ' {'
current_indent = current_line_indent
}
if (current_line_indent < prev_line_indent) {
for (var j of range(current_indent - current_line_indent)) {
lines[i-1] += '\n' + ' '.repeat(current_indent-j-1) + '}'
if (after_statement_indents.at(-1) === current_indent-j-1) {
lines[i-1] += ')'
after_statement_indents.pop()
}
}
current_indent = current_line_indent
}
if (lines[i].trimStart().startsWith('after(')) {
after_statement_indents.push(current_indent)
}
}
new_line = ''
for (var j of range(current_indent)) {
new_line += '' + ' '.repeat(current_indent-1) + '}'
}
lines.push(new_line)
var compiled_code = lines.join('\n')
// compiled_code = compiled_code.replaceAll('+\n', '+')
// compiled_code = compiled_code.replaceAll('(\n', '(')
// add text back in
for (var i=0; i<strings.length; i++) {
compiled_code = compiled_code.replace(`[TEXT_CONTENT_${i}]`, `'${strings[i]}'`)
}
print('COMPILED CODE:', compiled_code)
try {print('compiled in', performance.now() - t, 'ms')} catch {}
return compiled_code
}
function get_indent(str) {
if (!str || !str.trim()) {
return 0
}
return (str.length - str.trimStart().length) / 4
}
function get_inside_brackets(str, open_bracket, closing_bracket) {
text_inside_bracket = ''
counter = 1
for (const char of str) {
if (char == open_bracket)
counter += 1
if (char == closing_bracket)
counter -= 1
if (counter == 0)
return text_inside_bracket
text_inside_bracket += char
}
}
function convert_arguments(line, class_name) {
part_after = line.split(class_name+'(')[1]
arguments = get_inside_brackets(part_after, '(', ')')
new_arguments = arguments
has_inline_function = false
if (arguments.includes(`function()`)) {
has_inline_function = true
func_content = arguments.split('function()')[1]
lastIndex = arguments.lastIndexOf(')')
func_content = func_content.substr(0, lastIndex) + func_content.substr(lastIndex)
function_definition = 'function()' + func_content
new_arguments = arguments.replace(function_definition, `[INLINE_FUNC_PLACEHOLDER]`)
}
if (!new_arguments.includes('=')) {
return line
}
keys = new_arguments.split(',').map(e => e.split('=')[0])
if (!keys.includes('name') && class_name == 'Entity') {
if (line.includes(`= new ${class_name}`)) {
variable_name = line.split(`= new ${class_name}`)[0].trimStart()
if (variable_name.startsWith('let ')) {
variable_name = variable_name.slice(4)
}
new_arguments = `name='${variable_name}', ${new_arguments}`
}
}
js_style_arguments = '{' + new_arguments.replaceAll('=', ':') + '}'
if (has_inline_function) {
js_style_arguments = js_style_arguments.replace('[INLINE_FUNC_PLACEHOLDER]', 'function(){' + func_content + '}')
}
return line.replace(arguments, js_style_arguments)
}
function len(arr) {
return arr.length
}
function sum(arr) {
return arr.reduce((a, b) => a + b, 0)
}
String.prototype.count=function(c) {
var result = 0, i = 0;
for(i;i<this.length;i++)if(this[i]==c)result++;
return result;
}
print = console.log
False = false
True = true
None = null
float = parseFloat
min = Math.min
max = Math.max
abs = Math.abs
floor = Math.floor
ceil = Math.ceil
math = Math
sqrt = Math.sqrt
sin = Math.sin
pow = Math.pow
math.pi = Math.PI
function round(value, digits=0) {
return Number(Math.round(value+'e'+digits)+'e-'+digits);
}
function enumerate(list) {
if (typeof list.prop2 === 'function') {
return list.entries()
}
else if (typeof list === 'object') {
return Object.entries(list)
}
}
function str(value) {
return value.map(function(i){return String.fromCharCode(i)}).join("")
}
function int(value) {
if (value === true) {return 1}
if (value === false) {return 0}
return parseInt(value)
}
function Array_2d(w, h, default_value=0) {
print('deprecated: Array_2d. Use Array2D')
var tiles = new Array(w)
for (var i = 0; i < tiles.length; i++) {
tiles[i] = new Array(h);
tiles[i].fill(default_value)
}
return tiles
}
class Array2D {
constructor(width, height, default_value=0) {
this.width = width;
this.height = height;
this.data = new Array(width);
for (let i = 0; i < width; i++) {
this.data[i] = new Array(height).fill(default_value);
}
return new Proxy(this, {
get: (target, prop) => {
if (typeof prop === "symbol" || isNaN(prop)) {
return target[prop]; // Handle non-numeric properties
}
return target.data[prop]; // Access rows directly
}
});
}
get(x, y) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
return this.data[x][y];
}
return null; // Return null if out of bounds
}
set(x, y, value) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
this.data[x][y] = value;
}
}
flatten() {
return this.data.reduce((flat, row) => flat.concat(row), []);
}
}
function Array_3d(w, h, d) {
var arr = new Array(w);
for (var i = 0; i < w; i++) {
arr[i] = new Array(h);
for (var j = 0; j < h; j++) {
arr[i][j] = new Array(d).fill(0);
}
}
return arr;
}
// function range(n) {return Array(n).keys()}
function range(start, stop, step) {
if (typeof stop == 'undefined') {
// one param defined
stop = start;
start = 0;
}
if (typeof step == 'undefined') {
step = 1;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return [];
}
var result = [];
for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i);
}
return result;
};
Array.prototype.myCustomFilter = function (fn) {
const filtered = []
for (let i=0; i<this.length; i++) {
if (fn(this[i]) != undefined) {
filtered.push(this[i]);
}
}
return filtered;
}
Array.prototype.remove = function (element) {
var index = this.indexOf(element)
if (index >= 0) {
this.splice(index, 1)
}
}
Array.prototype.remove_at = function (index) {
this.splice(index, 1)
}
Array.prototype.skip = function (n) {
return this.splice(n)
}
Array.prototype.take = function (n) {
return this.splice(0, n)
}
Array.prototype.take_last = function (n) {
return this.splice(-n)
}
Object.prototype.keys = function() {
return Object.keys(this)
}
Object.prototype.values = function() {
return Object.values(this)
}
Object.prototype.includes = function(key) { // to allow for if name in dict, instad of having to do if name in dict.keys()
return Object.keys(this).includes(key);
};
function tuple(arr) {
return arr
}
function sorted(arr) {
return arr.slice().sort((a, b) => a - b) // Sort a copy of the array
}
function dict(values={}) {
return values
}
function hasattr(class_instance, name) {
return class_instance.hasOwnProperty(name)
}
__name__ = null // for python compability
__autocompile__ = true
if (typeof document !== "undefined") {
var scripts = document.getElementsByTagName("script")
}
else {
var scripts = []
}
for (var script of scripts) {
if (script.type == 'text/sunsnake') {
print('compile:', script)
if (script.textContent) { // inline script content
compiled_code = compile(script.textContent)
eval(compiled_code)
}
else if (script.src) {
// Fetch the content from the src attribute if it exists
fetch(script.src)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
compiled_code = compile(data);
eval(compiled_code);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
}
}
async function __import__(url) {
console.log('importing:', url);
if (!url.includes('.')) {
url = url + '.sunsnake';
}
const response = await fetch(url).then();
if (!response.ok) {
throw new Error(`Failed to fetch module: ${response.statusText}`);
}
const data = await response.text();
const compiled_code = compile(data);
eval(compiled_code);
}