about summary refs log tree commit diff
path: root/pkg/lang/compiler/compiler_test.go
blob: 725620c603b61901735c5071054d6e71fdeb8b26 (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
package compiler_test

import (
	"jinx/pkg/lang/compiler"
	"jinx/pkg/lang/parser"
	"jinx/pkg/lang/scanner"
	"jinx/pkg/lang/vm/text"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestSimpleAddExpr(t *testing.T) {
	src := `
	1 + 2
	`

	expected := `
	push_int 1
	push_int 2
	add
	drop 1
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestOperationOrder(t *testing.T) {
	grouped := `
	(((1 + 2) - 3) + 4)
	`

	free := `
	1 + 2 - 3 + 4
	`

	expected := `
	push_int 1
	push_int 2
	add
	push_int 3
	sub
	push_int 4
	add
	drop 1
	halt
	`

	mustCompileTo(t, grouped, expected)
	mustCompileTo(t, free, expected)
}

func TestNestedExpr(t *testing.T) {
	// Yeah, we don't compile identifiers yet, so here we call and index directly on literals.
	src := `
	1 + 2 - 3 - (123("a", "b", "c") + 456[321])
	`

	expected := `
	push_int 1
	push_int 2
	add
	
	push_int 3
	sub

	push_int 123
	push_string "a"
	push_string "b"
	push_string "c"
	call 3

	push_int 456
	push_int 321
	index

	add
	sub

	drop 1

	halt
	`

	mustCompileTo(t, src, expected)
}

func TestVars(t *testing.T) {
	src := `
	var x = 10
	var y = 25
	x = x + 7

	var res = x + y
	`

	expected := `
	push_int 10

	push_int 25

	get_local 0
	push_int 7
	add
	set_local 0

	get_local 0
	get_local 1
	add

	halt
	`

	mustCompileTo(t, src, expected)
}

func TestArrayLit(t *testing.T) {
	src := `var x = [1, 2, 3]`

	expected := `
	push_array

	get_local 0
	get_member "push"
	push_int 1
	call 1

	get_local 0
	get_member "push"
	push_int 2
	call 1

	get_local 0
	get_member "push"
	push_int 3
	call 1
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestIf(t *testing.T) {
	src := `
	if 1 <= 5 {
		"hello " + "world"
	}
	`

	expected := `
	push_int 1
	push_int 5
	lte

	jf @end

	push_string "hello "
	push_string "world"
	add

	drop 1

	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestIfElifElse(t *testing.T) {
	src := `
		if false {
			1
		} elif true {
			2
		} else {
			3
		}
	`

	expected := `
	push_false
	jf @elif

	push_int 1
	drop 1
	jmp @end

	@elif:
	push_true
	jf @else
	push_int 2
	drop 1
	jmp @end

	@else:
	push_int 3
	drop 1

	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestIfNoElse(t *testing.T) {
	src := `
		if false {
			1
		} elif true {
			2
		}
	`

	expected := `
	push_false
	jf @elif

	push_int 1
	drop 1
	jmp @end

	@elif:
	push_true
	jf @end
	push_int 2
	drop 1

	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestNestedIfs(t *testing.T) {
	src := `
	if true {
		if false {
			1
		} else {
			2
		}
	}
	`

	expected := `
	push_true
	jf @end

	push_false
	jf @else

	push_int 1
	drop 1
	jmp @end

	@else:
	push_int 2
	drop 1
	
	@end:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestForCond(t *testing.T) {
	src := `
	for true {
		for false {
			1
		}
	}
	`

	expected := `
	@start:
	push_true
	jf @end

	@inner_start:
	push_false
	jf @inner_end

	push_int 1
	drop 1
	jmp @inner_start

	@inner_end:
	jmp @start

	@end
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestForCondBreakContinue(t *testing.T) {
	src := `
	var sum = 0
	var x = 0
	for {
		if x % 2 == 0 {
			continue
		}

		sum = sum + x

		if x == 100 {
			break
		}
	}
	`

	expected := `
	push_int 0
	push_int 0

	@continue:
	get_local 1
	push_int 2
	mod
	push_int 0
	eq
	jf @sum
	jmp @continue

	@sum:
	get_local 0
	get_local 1
	add
	set_local 0

	get_local 1
	push_int 100
	eq
	jf @repeat
	jmp @break

	@repeat:
	jmp @continue

	@break:
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestForIn(t *testing.T) {
	src := `
	for x in [1, 2, 3, "oops"] {
		if x == "oops" {
			break
		}

		"say"(x)
	}
	`

	expected := `
	push_array

	get_local 0
	get_member "push"
	push_int 1
	call 1

	get_local 0
	get_member "push"
	push_int 2
	call 1

	get_local 0
	get_member "push"
	push_int 3
	call 1

	get_local 0
	get_member "push"
	push_string "oops"
	call 1

	push_int 0
	push_null

	@check:
	get_local 1
	get_local 0
	get_member "length"
	call 0
	lt

	jf @end

	get_local 0
	get_local 1
	index
	set_local 2

	get_local 1
	push_int 1
	add
	set_local 1

	get_local 2
	push_string "oops"
	eq
	jf @say

	jmp @end

	@say:
	push_string "say"
	get_local 2
	call 1
	drop 1

	jmp @check
	@end:
	drop 3
	halt
	`

	mustCompileTo(t, src, expected)
}

func TestSimpleFunction(t *testing.T) {
	src := `
	fn the_meaning_of_life() {
		return 42
	}

	var result = the_meaning_of_life()
	`

	expected := `
	push_function @the_meaning_of_life

	get_local 0
	call 0
	halt

	@the_meaning_of_life:
	push_int 42
	ret
	`

	mustCompileTo(t, src, expected)
}

func TestFunctionArgs(t *testing.T) {
	// TODO: Are arguments in the correct order?
	src := `
	fn add(a, b) {
		return a + b
	}
	
	add(4, 5)
	`

	expected := `
	push_function @add
	set_arg_count 2

	get_local 0
	push_int 4
	push_int 5
	call 2
	drop 1
	halt

	@add:
	get_local 1
	get_local 0
	add
	ret
	`

	mustCompileTo(t, src, expected)
}

func TestClosureEnv(t *testing.T) {
	src := `
	fn create() {
		var x = 0
		fn closure() {
			x = x + 1
			return x
		}

		return closure
	}
	`

	expected := `
	push_function @create
	halt

	@closure:
	get_env 0
	push_int 1
	add
	set_env 0
	get_env 0
	ret

	@create:
	push_int 0
	push_function @closure
	add_to_env 0

	get_local 1
	ret
	`

	mustCompileTo(t, src, expected)
}

func TestType(t *testing.T) {
	src := `
	type Cat {
		(name, age) {
			this.name = name
			this.age = age
		}

		fn meow(this) {
			return this.name + " says Meow!"
		}
	}

	var kitty = Cat("Kitty", 3)
	kitty.meow()
	`

	expected := `
	push_type "Cat"

	get_local 0
	get_member "$add_method"

	push_string "$init"
	push_function @Cat:$init
	set_arg_count 2

	call 2
	drop 1

	get_local 0
	get_member "$add_method"

	push_string "meow"
	push_function @Cat:meow

	call 2
	drop 1

	get_local 0
	push_string "Kitty"
	push_int 3
	call 2

	get_local 1
	get_member "meow"
	call 0
	drop 1

	halt

	@Cat:$init:
		push_object
		get_env 0
		anchor_type

		get_local 2
		get_local 1
		set_member "name"

		get_local 2
		get_local 0
		set_member "age"

		get_local 2
		ret
	@Cat:meow:
		get_env 0
		get_member "name"

		push_string " says Meow!"

		add
		ret
	`

	mustCompileTo(t, src, expected)
}

func TestGlobals(t *testing.T) {
	src := `
	use mul from hello by mel
	use pi from math

	var result = mul(pi, 2)
	`

	expected := `
	get_global "mel:hello:mul"
	get_global ":math:pi"
	push_int 2
	call 2
	halt
	`

	mustCompileTo(t, src, expected)
}

func mustCompileTo(t *testing.T, src, expected string) {
	scanner := scanner.New(strings.NewReader(src))
	tokens, err := scanner.Scan()
	require.NoError(t, err)

	parser := parser.New(tokens)
	program, err := parser.Parse()
	require.NoError(t, err)

	langCompiler := compiler.New("test_program", "test", program)
	testResult, err := langCompiler.Compile()
	require.NoError(t, err)

	textCompiler := text.NewCompiler(strings.NewReader(expected))
	expectedResult, err := textCompiler.Compile()
	require.NoError(t, err)

	if !assert.Equal(t, expectedResult.Code(), testResult.Code().Code()) {
		d1 := text.NewDecompiler(expectedResult)
		d2 := text.NewDecompiler(*testResult.Code())
		require.Equal(t, d1.Decompile(), d2.Decompile())
	}
}