aboutsummaryrefslogtreecommitdiff
path: root/paradox.false
blob: 8c705d17245192f4b1653963db4f1bd907b934af (plain)
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
{
	variables:

	c: current_char
	q: state: 0=CONTINUE, 1=DONE, 2=ERROR

	f: fn_counter
	s: str_counter

	i: current_int

	l: line_number
	p: line_position

	t: stack_size
	u: buffer_size

	x: compile_fn
	r: read_char
	e: error
}

0  f: { fn_counter <- 0 }
0  s: { str_counter <- 0 }
1_ i: { current_int <- -1 }

1  l: { line_number <- 1 }
0  p: { line_position <- 0 }

8388608 t: { stack_size <- 8MiB }
8192    u: { buffer_size <- 8KiB }

{ read_char() }
[
	^ { input <- getchar() }
	{ if input = newline }
	$10=$[
		l; 1+ l: { increment line_number }
		0 p:     { reset line_position }
	]?
	{ else }
	~[
		p; 1+ p: { increment line_position }
	]?
]r:

{ error(condition, message) }
[
	{ if condition}
	\ [
		10,"%fatal FALSE syntax error at " { first part of error message }
		l;.":"p;.": "                      { print line_number and line_position }
		$! 10,                             { call message }
		2q:                                { state <- ERROR }
	]?
	% { pop message }
]e:

{ compile_fn() }
[
	f;      { push fn_counter }
	$ 1+ f: { increment fn_counter }

	"fun_" $. ":" 10, { emit label }

	{
		string stack layout, from top to bottom:

		repeat:
			id (if -1, we've reached the end)
			length
			last .. first char
	}
	1_ { string stack end }

	0  q: { state <- CONTINUE }
	1_ c: { current_char <- EOF }

	{ while state == CONTINUE }
	[q;0=][
		{ if current_char = EOF }
		c;1_=[
			r;! c: { current_char <- read_char() }
		]?

		{ if '0'-1 < c < '9'+1 }
		c;$ '0 1- > \ '9 1+ \ > & $[
			i;1_=[0 i:]?       { if current_int = -1: current_int <- 0 }
			i; 10* c; '0- + i: { current_int = current_int * 10 + current_char - '0' }
			1_ c:              { consume current_char }
		]?

		{ elseif current_int != -1 }
		i;1_=~ $[%~1_\]?[
			{ if current_char = S }
			c;'S= $[
				i; t: { stack_size <- current_int }
				1_ c: { consume current_char }
			]?
			{ elseif current_char = U }
			c;'U= $[%~1_\]?[
				i; u: { buffer_size <- current_int }
				1_ c: { consume current_char }
			]?
			{ else }
			~[
				{ emit push int }
				"sub r12, 8" 10,
				"mov qword[r12], " i;. 10,
			]?

			1_ i: { clear current_int }
		]?

		{ elseif a-1 < c < z+1 }
		c;$ 'a 1- > \ 'z 1+ \ > & $[%~1_\]?[
			{ emit push var ref }
			"sub r12, 8" 10,
			"mov qword[r12], var_" c;, 10,

			1_ c: { consume current_char }
		]?

		{ elseif c = ' }
		c;''= $[%~1_\]?[
			r;! { read character literal }
			$1_=["unterminated char literal"]e;! { check for EOF }
			{ if state != ERROR }
			q;2=~ [
				{ emit push char }
				"sub r12, 8" 10,
				"mov qword[r12], " . 10,

				1_ c: { consume current_char }
			]?
		]?

		{ elseif c = " }
		c;'"= $[%~1_\]?[
			% { drop true to manipulate stack below }
			0 { length <- 0 }

			{ while read_char() != " and state != ERROR }
			[
				r;!           { push read_char() }
				$1_=["unterminated string literal"]e;! { check for EOF }
				$'"=~ q;2=~ & { condition }
			][
				\  { swap length to top }
				1+ { increment length }
			]#
			% { drop " or EOF }

			{ if state != ERROR }
			q;2=~ [
				s;      { id <- str_counter }
				$ 1+ s: { increment str_counter }

				{ emit print string }
				"mov rsi, str_" 0 ø . 10,
				"mov rcx, "     1 ø . 10,
				"call write" 10,

				1_ c: { consume current_char }
			]?

			1_ { push true }
		]?

		{ elseif c = ` }
		c;'`= $[%~1_\]?[
			{ while read_char() != ` and state != ERROR }
			[
				r;!           { push read_char() }
				$1_=["unterminated inline assembly"]e;! { check for EOF }
				$'`=~ q;2=~ & { condition }
			][,]#
			% { drop ` or EOF }

			1_ c: { consume current_char }
		]?

		{ elseif c = (opening bracket) }
		c;91= $[%~1_\]?[
			f;                 { backup fn_id }
			"jmp end_" f;. 10, { skip generated code }
			x;!                { call compile_fn }

			{ if state = ERROR }
			q;2=[
				{ stack is corrupted now }
				1_ { push true to skip remaining elseif branches }
			]?

			q;2=~ c;93=~ & ["unterminated lambda"]e;! { ensure current_char was (closing bracket) }

			{ if state != ERROR }
			q;2=~ [
				{ emit push fn ref }
				{ fn_id is top }
				"sub r12, 8" 10,
				"mov qword[r12], fun_" . 10,

				0  q: { state <- CONTINUE }
				1_ c: { clear current_char }
			]?
		]?

		{ elseif c is whitespace }
		c;9= c;10= | c;32= | $[%~1_\]?[
			1_ c: { consume current_char }
		]?

		{ elseif c = EOF or c = (closing bracket) }
		c;1_= c;93= | $[%~1_\]?[
			1 q: { state <- DONE }
		]?

		{ elseif c is UTF-8 }
		c;195= $[%~1_\]?[
			r;! c: { c <- read_char() }

			c;184=         $[ 'O c:	]? { if c = ø then c <- O }
			c;159= $[%~1_\]?[ 'B c:	]? { if c = ß then c <- B }
			~["unknown UTF-8 character"]e;! { else error }
		]?

		{ elseif c = ø then c <- O }
		c;248= $[%~1_\]?[ 'O c: ]?

		{ elseif c = ß then c <- B }
		c;223= $[%~1_\]?[ 'B c: ]?

		{ elseif c = (opening brace) }
		c;123= $[%~1_\]?[
			[r;!125=~][]# { while read_char() != (closing brace) }
			1_ c:         { clear current_char }
		]?

		{ elseif c = $ }
		c;'$= $[%~1_\]?[
"mov rax, [r12]
sub r12, 8
mov [r12], rax
"
		1_ c:]?

		{ elseif c = % }
		c;'%= $[%~1_\]?[
"add r12, 8
"
		1_ c:]?

		{ elseif c = \ }
		c;'\= $[%~1_\]?[
"mov rax, [r12]
mov rbx, [r12+8]
mov [r12], rbx
mov [r12+8], rax
"
		1_ c:]?

		{ elseif c = @ }
		c;'@= $[%~1_\]?[
"mov rax, [r12]
mov rbx, [r12+8]
mov rcx, [r12+16]
mov [r12], rcx
mov [r12+8], rax
mov [r12+16], rbx
"
		1_ c:]?

		{ elseif c = O }
		c;'O= $[%~1_\]?[
"mov rax, [r12]
lea rax, [r12+8*rax]
mov rax, [rax+8]
mov [r12], rax
"
		1_ c:]?

		{ elseif c = + }
		c;'+= $[%~1_\]?[
"mov eax, [r12]
add r12, 8
add [r12], eax
"
		1_ c:]?

		{ elseif c = - }
		c;'-= $[%~1_\]?[
"mov eax, [r12]
add r12, 8
sub [r12], eax
"
		1_ c:]?

		{ elseif c = * }
		c;'*= $[%~1_\]?[
"mov eax, [r12+8]
imul dword[r12]
add r12, 8
mov [r12], eax
"
		1_ c:]?

		{ elseif c = / }
		c;'/= $[%~1_\]?[
"xor edx, edx
mov eax, [r12+8]
idiv dword[r12]
add r12, 8
mov [r12], eax
"
		1_ c:]?

		{ elseif c = _ }
		c;'_= $[%~1_\]?[
"neg dword[r12]
"
		1_ c:]?

		{ elseif c = & }
		c;'&= $[%~1_\]?[
"mov eax, [r12]
and eax, [r12+8]
add r12, 8
mov [r12], eax
"
		1_ c:]?

		{ elseif c = | }
		c;'|= $[%~1_\]?[
"mov eax, [r12]
or eax, [r12+8]
add r12, 8
mov [r12], eax
"
		1_ c:]?

		{ elseif c = ~ }
		c;'~= $[%~1_\]?[
"not dword[r12]
"
		1_ c:]?

		{ elseif c = > }
		c;'>= $[%~1_\]?[
"mov ebx, 0
mov ecx, -1
add r12, 8
mov eax, [r12]
cmp eax, [r12-8]
cmovg ebx, ecx
mov [r12], ebx
"
		1_ c:]?

		{ elseif c = '=' }
		c;'== $[%~1_\]?[
"mov ebx, 0
mov ecx, -1
add r12, 8
mov eax, [r12]
cmp eax, [r12-8]
cmove ebx, ecx
mov [r12], ebx
"
		1_ c:]?

		{ elseif c = ! }
		c;'!= $[%~1_\]?[
"add r12, 8
call [r12-8]
"
		1_ c:]?

		{ elseif c = ? }
		c;'?= $[%~1_\]?[
"call conditional
"
		1_ c:]?

		{ elseif c = # }
		c;'#= $[%~1_\]?[
"call loop
"
		1_ c:]?

		{ elseif c = : }
		c;':= $[%~1_\]?[
"add r12, 16
mov rax, [r12-16]
mov rbx, [r12-8]
mov [rax], rbx
"
		1_ c:]?

		{ elseif c = ; }
		c;';= $[%~1_\]?[
"mov rax, [r12]
mov rax, [rax]
mov [r12], rax
"
		1_ c:]?

		{ elseif c = , }
		c;',= $[%~1_\]?[
"mov rsi, r12
mov rcx, 1
call write
add r12, 8
"
		1_ c:]?

		{ elseif c = ^ }
		c;'^= $[%~1_\]?[
"call read
sub r12, 8
mov [r12], eax
"
		1_ c:]?

		{ elseif c = . }
		c;'.= $[%~1_\]?[
"call print_num
"
		1_ c:]?

		{ elseif c = B }
		c;'B= $[%~1_\]?[
"call flush
"
		1_ c:]?

		{ else error }
		~["unknown character: "c;,]e;!
	]#

	{ if state != ERROR }
	q;2=~[
		"ret" 10, { emit return to caller }

		"section .data" 10,

		{ string stack is top }
		{ while top != -1 }
		[$1_=~][
			{ id is top }
			"str_" . ": db " { emit label }

			{ length is top }
			$ { copy length }

			{ print string }
			{ while length > 0}
			[$ 0 >][
				$ 1+ ø .     { print nth item }
				1-           { decrement length }
				$ 0 > [","]? { if length > 0 emit , }
			]#
			% { drop 0 }

			{ remove string }
			{ while length > 0 }
			[$ 0 >][
				\  { swap last char with length }
				%  { drop last char }
				1- { decrement length }
			]#
			% { drop 0 }

			10, { newline }
		]#
		% { drop string stack end }

		"section .text" 10,

		{ function counter is top now }
		"end_" . ":" 10, { end label }
	]?
] x:

"section .text" 10,

x;! { call compile_fn }

{ if state != ERROR }
q;2=~[

{ emit constants }
"%define STKSIZ " t;. 10,
"%define BUFSIZ " u;. 10,

{ emit builtin functions }
"section .data
readbuf_len: dq 0
readbuf_cursor: dq 0
writebuf_len: dq 0
section .bss
readbuf: resb BUFSIZ
writebuf: resb BUFSIZ
section .text
read:
mov rax, [readbuf_cursor]
mov rbx, [readbuf_len]
cmp rax, rbx
jb .has
cmp rbx, BUFSIZ
jb .fill
xor rbx, rbx
mov [readbuf_len], rbx
mov [readbuf_cursor], rbx
.fill:
mov rax, 0
mov rdi, 0
lea rsi, [rbx+readbuf]
mov rdx, BUFSIZ
sub rdx, rbx
syscall
add [readbuf_len], rax
cmp rax, 0
jne .has
mov eax, -1
ret
.has:
mov rax, [readbuf_cursor]
movzx eax, byte[readbuf+rax]
inc qword[readbuf_cursor]
ret
write:
mov rdi, [writebuf_len]
mov rax, BUFSIZ
sub rax, rdi
add rdi, writebuf
mov rdx, rcx
sub rdx, rax
jna .simple
mov rcx, rax
rep movsb
push rsi
push rdx
mov qword[writebuf_len], BUFSIZ
call flush
pop rdx
pop rsi
cmp rdx, BUFSIZ
ja .direct
mov rcx, rdx
mov rdi, writebuf
.simple:
add [writebuf_len], rcx
rep movsb
ret
.direct:
mov rax, 1
mov rdi, 1
syscall
ret
flush:
mov rdx, [writebuf_len]
cmp rdx, 0
je .return
mov rax, 1
mov rdi, 1
mov rsi, writebuf
syscall
mov qword[writebuf_len], 0
.return:
ret
conditional:
add r12, 16
mov eax, [r12-8]
cmp eax, 0
je .return
call [r12-16]
.return:
ret
loop:
add r12, 16
sub rsp, 16
mov rax, [r12-8]
mov [rsp], rax
mov rax, [r12-16]
mov [rsp+8], rax
.loop:
call [rsp]
add r12, 8
mov eax, [r12-8]
cmp eax, 0
je .return
call [rsp+8]
jmp .loop
.return:
add rsp, 16
ret
print_num:
mov rcx, rsp
sub rsp, 16
mov eax, dword[r12]
add r12, 8
mov ebx, eax
neg ebx
cmp eax, 0
cmovl eax, ebx
mov edi, 10
.loop:
dec rcx
xor edx, edx
idiv edi
add dl, '0'
mov byte[rcx], dl
cmp eax, 0
jne .loop
cmp ebx, 0
jle .print
dec rcx
mov byte[rcx], '-'
.print:
mov rsi, rcx
lea rcx, [rsp+16]
sub rcx, rsi
call write
add rsp, 16
ret
global _start
_start:
lea r12, [data_stack+STKSIZ]
lea rsp, [call_stack+STKSIZ]
call fun_0
call flush
mov rax, 60
mov rdi, 0
syscall
section .bss
data_stack: resq STKSIZ
call_stack: resq STKSIZ
"

{ emit variables }

"section .data" 10,

'a { iter <- a}
{ while iter != z+1}
[$ 'z1+ =~][
	"var_" $, ": dq 0" 10, { emit allocation }
	1+                     { increment iter }
]#
% { drop iter }

]?

ß