-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_test.go
More file actions
880 lines (765 loc) · 22.5 KB
/
codegen_test.go
File metadata and controls
880 lines (765 loc) · 22.5 KB
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
package codegen
import (
"strings"
"testing"
"github.com/codeassociates/occam2go/lexer"
"github.com/codeassociates/occam2go/parser"
)
func TestSimpleVarDecl(t *testing.T) {
input := `INT x:
`
output := transpile(t, input)
if !strings.Contains(output, "var x int") {
t.Errorf("expected 'var x int' in output, got:\n%s", output)
}
}
func TestMultipleVarDecl(t *testing.T) {
input := `INT x, y, z:
`
output := transpile(t, input)
if !strings.Contains(output, "var x, y, z int") {
t.Errorf("expected 'var x, y, z int' in output, got:\n%s", output)
}
}
func TestReal32VarDecl(t *testing.T) {
input := `REAL32 x:
`
output := transpile(t, input)
if !strings.Contains(output, "var x float32") {
t.Errorf("expected 'var x float32' in output, got:\n%s", output)
}
}
func TestReal64VarDecl(t *testing.T) {
input := `REAL64 x:
`
output := transpile(t, input)
if !strings.Contains(output, "var x float64") {
t.Errorf("expected 'var x float64' in output, got:\n%s", output)
}
}
func TestAssignment(t *testing.T) {
input := `x := 42
`
output := transpile(t, input)
if !strings.Contains(output, "x = 42") {
t.Errorf("expected 'x = 42' in output, got:\n%s", output)
}
}
func TestBinaryExpression(t *testing.T) {
input := `x := a + b
`
output := transpile(t, input)
if !strings.Contains(output, "x = (a + b)") {
t.Errorf("expected 'x = (a + b)' in output, got:\n%s", output)
}
}
func TestComparisonOperators(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"x := a = b\n", "x = (a == b)"},
{"x := a <> b\n", "x = (a != b)"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("expected %q in output, got:\n%s", tt.expected, output)
}
}
}
func TestSeqBlock(t *testing.T) {
input := `SEQ
INT x:
x := 10
`
output := transpile(t, input)
// SEQ becomes sequential Go code
if !strings.Contains(output, "var x int") {
t.Errorf("expected 'var x int' in output, got:\n%s", output)
}
if !strings.Contains(output, "x = 10") {
t.Errorf("expected 'x = 10' in output, got:\n%s", output)
}
}
func TestParBlock(t *testing.T) {
input := `PAR
x := 1
y := 2
`
output := transpile(t, input)
// PAR should use sync.WaitGroup
if !strings.Contains(output, "sync.WaitGroup") {
t.Errorf("expected sync.WaitGroup in output, got:\n%s", output)
}
if !strings.Contains(output, "wg.Add(2)") {
t.Errorf("expected wg.Add(2) in output, got:\n%s", output)
}
if !strings.Contains(output, "go func()") {
t.Errorf("expected 'go func()' in output, got:\n%s", output)
}
if !strings.Contains(output, "wg.Wait()") {
t.Errorf("expected wg.Wait() in output, got:\n%s", output)
}
}
func TestProcDecl(t *testing.T) {
input := `PROC foo(VAL INT x)
y := x
`
output := transpile(t, input)
if !strings.Contains(output, "func foo(x int)") {
t.Errorf("expected 'func foo(x int)' in output, got:\n%s", output)
}
}
func TestProcDeclWithRefParam(t *testing.T) {
input := `PROC bar(INT x)
x := 10
`
output := transpile(t, input)
// Non-VAL parameter should be pointer
if !strings.Contains(output, "func bar(x *int)") {
t.Errorf("expected 'func bar(x *int)' in output, got:\n%s", output)
}
}
func TestIfStatement(t *testing.T) {
input := `IF
x > 0
y := 1
x = 0
y := 0
`
output := transpile(t, input)
if !strings.Contains(output, "if (x > 0)") {
t.Errorf("expected 'if (x > 0)' in output, got:\n%s", output)
}
if !strings.Contains(output, "} else if (x == 0)") {
t.Errorf("expected '} else if (x == 0)' in output, got:\n%s", output)
}
}
func TestReplicatedIf(t *testing.T) {
input := `IF i = 0 FOR 5
i = 3
SKIP
`
output := transpile(t, input)
if !strings.Contains(output, "for i := 0; i < 0 + 5; i++") {
t.Errorf("expected for loop in output, got:\n%s", output)
}
if !strings.Contains(output, "if (i == 3)") {
t.Errorf("expected 'if (i == 3)' in output, got:\n%s", output)
}
if !strings.Contains(output, "break") {
t.Errorf("expected 'break' in output, got:\n%s", output)
}
}
func TestArrayDecl(t *testing.T) {
input := `[5]INT arr:
`
output := transpile(t, input)
if !strings.Contains(output, "arr := make([]int, 5)") {
t.Errorf("expected 'arr := make([]int, 5)' in output, got:\n%s", output)
}
}
func TestIndexedAssignment(t *testing.T) {
input := `arr[2] := 10
`
output := transpile(t, input)
if !strings.Contains(output, "arr[2] = 10") {
t.Errorf("expected 'arr[2] = 10' in output, got:\n%s", output)
}
}
func transpile(t *testing.T, input string) string {
t.Helper()
l := lexer.New(input)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) > 0 {
for _, err := range p.Errors() {
t.Errorf("parser error: %s", err)
}
t.FailNow()
}
gen := New()
return gen.Generate(program)
}
func TestBitwiseOperators(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"x := a /\\ b\n", "x = (a & b)"},
{"x := a \\/ b\n", "x = (a | b)"},
{"x := a >< b\n", "x = (a ^ b)"},
{"x := a << 2\n", "x = (a << 2)"},
{"x := a >> 2\n", "x = (a >> 2)"},
{"x := ~ a\n", "x = ^a"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestStringLiteral(t *testing.T) {
input := `x := "hello world"
`
output := transpile(t, input)
if !strings.Contains(output, `x = "hello world"`) {
t.Errorf("expected 'x = \"hello world\"' in output, got:\n%s", output)
}
}
func TestStringEscapeCodegen(t *testing.T) {
input := `x := "hello*c*n"
`
output := transpile(t, input)
// The *c*n should become \r\n in the Go output (via %q formatting)
if !strings.Contains(output, `x = "hello\r\n"`) {
t.Errorf("expected string with \\r\\n escape, got:\n%s", output)
}
}
func TestByteLiteral(t *testing.T) {
input := "x := 'A'\n"
output := transpile(t, input)
if !strings.Contains(output, "x = byte(65)") {
t.Errorf("expected 'x = byte(65)' in output, got:\n%s", output)
}
}
func TestByteLiteralEscape(t *testing.T) {
input := "x := '*n'\n"
output := transpile(t, input)
if !strings.Contains(output, "x = byte(10)") {
t.Errorf("expected 'x = byte(10)' in output, got:\n%s", output)
}
}
func TestStop(t *testing.T) {
input := "STOP\n"
output := transpile(t, input)
if !strings.Contains(output, `fmt.Fprintln(os.Stderr, "STOP encountered")`) {
t.Errorf("expected fmt.Fprintln(os.Stderr, ...) in output, got:\n%s", output)
}
if !strings.Contains(output, "select {}") {
t.Errorf("expected 'select {}' in output, got:\n%s", output)
}
if !strings.Contains(output, `"os"`) {
t.Errorf("expected os import in output, got:\n%s", output)
}
}
func TestTypeConversion(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"x := INT y\n", "x = int(y)"},
{"x := BYTE n\n", "x = byte(n)"},
{"x := REAL count\n", "x = float64(count)"},
{"x := BOOL flag\n", "x = ((flag) != 0)"},
{"x := REAL32 y\n", "x = float32(y)"},
{"x := REAL64 y\n", "x = float64(y)"},
{"x := INT16 y\n", "x = int16(y)"},
{"x := INT32 y\n", "x = int32(y)"},
{"x := INT64 y\n", "x = int64(y)"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestBoolTypeConversion(t *testing.T) {
tests := []struct {
input string
expected string
}{
// numeric → BOOL
{"x := BOOL b\n", "x = ((b) != 0)"},
// BOOL comparison → INT (known bool expression)
{"x := INT (a = b)\n", "x = _boolToInt((a == b))"},
// BOOL comparison → BYTE (known bool expression)
{"x := BYTE (a = b)\n", "x = byte(_boolToInt((a == b)))"},
// BOOL literal → INT
{"x := INT TRUE\n", "x = _boolToInt(true)"},
// NOT expr → INT
{"x := INT (NOT flag)\n", "x = _boolToInt(!flag)"},
// BOOL conversion → INT (nested BOOL target)
{"x := INT (BOOL n)\n", "x = _boolToInt(((n) != 0))"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestBoolToNumericHelper(t *testing.T) {
// The _boolToInt helper should be emitted when bool→numeric conversion is present
input := "x := INT (a = b)\n"
output := transpile(t, input)
if !strings.Contains(output, "func _boolToInt(b bool) int {") {
t.Errorf("expected _boolToInt helper in output, got:\n%s", output)
}
}
func TestBoolToNumericHelperNotEmitted(t *testing.T) {
// The _boolToInt helper should NOT be emitted when only numeric→bool conversion is present
input := "x := BOOL b\n"
output := transpile(t, input)
if strings.Contains(output, "_boolToInt") {
t.Errorf("did not expect _boolToInt helper in output, got:\n%s", output)
}
}
func TestTypeConversionRoundTrunc(t *testing.T) {
tests := []struct {
input string
expected string
}{
// float → int with ROUND: uses math.Round
{"x := INT ROUND y\n", "x = int(math.Round(float64(y)))"},
{"x := INT64 ROUND y\n", "x = int64(math.Round(float64(y)))"},
{"x := INT16 ROUND y\n", "x = int16(math.Round(float64(y)))"},
// float → int with TRUNC: plain cast (Go default truncates)
{"x := INT TRUNC y\n", "x = int(y)"},
{"x := INT64 TRUNC y\n", "x = int64(y)"},
// int → float with ROUND/TRUNC: qualifier irrelevant, plain cast
{"x := REAL32 ROUND y\n", "x = float32(y)"},
{"x := REAL64 TRUNC y\n", "x = float64(y)"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestTypeConversionRoundMathImport(t *testing.T) {
// INT ROUND should trigger math import
output := transpile(t, "x := INT ROUND y\n")
if !strings.Contains(output, `"math"`) {
t.Errorf("expected math import for INT ROUND, got:\n%s", output)
}
// INT TRUNC should NOT require math import (unless other constructs do)
output = transpile(t, "x := INT TRUNC y\n")
if strings.Contains(output, `"math"`) {
t.Errorf("did not expect math import for INT TRUNC, got:\n%s", output)
}
}
func TestMostNegMostPos(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"x := MOSTNEG INT\n", "x = math.MinInt"},
{"x := MOSTPOS INT\n", "x = math.MaxInt"},
{"x := MOSTNEG BYTE\n", "x = 0"},
{"x := MOSTPOS BYTE\n", "x = 255"},
{"x := MOSTNEG REAL32\n", "x = -math.MaxFloat32"},
{"x := MOSTPOS REAL32\n", "x = math.MaxFloat32"},
{"x := MOSTNEG REAL64\n", "x = -math.MaxFloat64"},
{"x := MOSTPOS REAL64\n", "x = math.MaxFloat64"},
{"x := MOSTNEG INT16\n", "x = math.MinInt16"},
{"x := MOSTPOS INT16\n", "x = math.MaxInt16"},
{"x := MOSTNEG INT32\n", "x = math.MinInt32"},
{"x := MOSTPOS INT32\n", "x = math.MaxInt32"},
{"x := MOSTNEG INT64\n", "x = math.MinInt64"},
{"x := MOSTPOS INT64\n", "x = math.MaxInt64"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestMostNegImportsMath(t *testing.T) {
output := transpile(t, "x := MOSTNEG INT\n")
if !strings.Contains(output, `"math"`) {
t.Errorf("expected math import in output, got:\n%s", output)
}
}
func TestMostNegByteNoMathImport(t *testing.T) {
output := transpile(t, "x := MOSTNEG BYTE\n")
if strings.Contains(output, `"math"`) {
t.Errorf("expected no math import for MOSTNEG BYTE, got:\n%s", output)
}
}
func TestStringLiteralInProcCall(t *testing.T) {
input := `print.string("hello")
`
output := transpile(t, input)
if !strings.Contains(output, `fmt.Println("hello")`) {
t.Errorf("expected 'fmt.Println(\"hello\")' in output, got:\n%s", output)
}
}
func TestCheckedArithmeticCodegen(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"x := a PLUS b\n", "x = (a + b)"},
{"x := a MINUS b\n", "x = (a - b)"},
{"x := a TIMES b\n", "x = (a * b)"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("expected %q in output for %q, got:\n%s", tt.expected, tt.input, output)
}
}
}
func TestSimpleProtocolType(t *testing.T) {
input := `PROTOCOL SIGNAL IS INT
`
output := transpile(t, input)
if !strings.Contains(output, "type _proto_SIGNAL = int") {
t.Errorf("expected 'type _proto_SIGNAL = int' in output, got:\n%s", output)
}
}
func TestSequentialProtocolType(t *testing.T) {
input := `PROTOCOL PAIR IS INT ; BYTE
`
output := transpile(t, input)
if !strings.Contains(output, "type _proto_PAIR struct {") {
t.Errorf("expected struct declaration in output, got:\n%s", output)
}
if !strings.Contains(output, "_0 int") {
t.Errorf("expected '_0 int' field in output, got:\n%s", output)
}
if !strings.Contains(output, "_1 byte") {
t.Errorf("expected '_1 byte' field in output, got:\n%s", output)
}
}
func TestVariantProtocolType(t *testing.T) {
input := `PROTOCOL MSG
CASE
text; INT
quit
`
output := transpile(t, input)
if !strings.Contains(output, "type _proto_MSG interface {") {
t.Errorf("expected interface declaration in output, got:\n%s", output)
}
if !strings.Contains(output, "_is_MSG()") {
t.Errorf("expected marker method in output, got:\n%s", output)
}
if !strings.Contains(output, "type _proto_MSG_text struct {") {
t.Errorf("expected text struct in output, got:\n%s", output)
}
if !strings.Contains(output, "type _proto_MSG_quit struct{}") {
t.Errorf("expected quit struct in output, got:\n%s", output)
}
}
func TestVariantProtocolDottedTags(t *testing.T) {
input := `PROTOCOL BAR.PROTO
CASE
bar.data; INT
bar.terminate
bar.blank; INT
`
output := transpile(t, input)
if !strings.Contains(output, "type _proto_BAR_PROTO interface {") {
t.Errorf("expected interface declaration in output, got:\n%s", output)
}
if !strings.Contains(output, "type _proto_BAR_PROTO_bar_data struct {") {
t.Errorf("expected bar_data struct in output, got:\n%s", output)
}
if !strings.Contains(output, "type _proto_BAR_PROTO_bar_terminate struct{}") {
t.Errorf("expected bar_terminate struct in output, got:\n%s", output)
}
if !strings.Contains(output, "type _proto_BAR_PROTO_bar_blank struct {") {
t.Errorf("expected bar_blank struct in output, got:\n%s", output)
}
}
func TestRecordType(t *testing.T) {
input := `RECORD POINT
INT x:
INT y:
`
output := transpile(t, input)
if !strings.Contains(output, "type POINT struct {") {
t.Errorf("expected 'type POINT struct {' in output, got:\n%s", output)
}
if !strings.Contains(output, "x int") {
t.Errorf("expected 'x int' field in output, got:\n%s", output)
}
if !strings.Contains(output, "y int") {
t.Errorf("expected 'y int' field in output, got:\n%s", output)
}
}
func TestRecordFieldAssignmentCodegen(t *testing.T) {
input := `RECORD POINT
INT x:
INT y:
SEQ
POINT p:
p[x] := 5
`
output := transpile(t, input)
if !strings.Contains(output, "p.x = 5") {
t.Errorf("expected 'p.x = 5' in output, got:\n%s", output)
}
}
func TestChanArrayDeclGen(t *testing.T) {
input := `[5]CHAN OF INT cs:
`
output := transpile(t, input)
if !strings.Contains(output, "cs := make([]chan int, 5)") {
t.Errorf("expected 'cs := make([]chan int, 5)' in output, got:\n%s", output)
}
if !strings.Contains(output, "for _i0 := range cs { cs[_i0] = make(chan int) }") {
t.Errorf("expected init loop in output, got:\n%s", output)
}
}
func TestIndexedSendGen(t *testing.T) {
input := `cs[0] ! 42
`
output := transpile(t, input)
if !strings.Contains(output, "cs[0] <- 42") {
t.Errorf("expected 'cs[0] <- 42' in output, got:\n%s", output)
}
}
func TestIndexedReceiveGen(t *testing.T) {
input := `cs[0] ? x
`
output := transpile(t, input)
if !strings.Contains(output, "x = <-cs[0]") {
t.Errorf("expected 'x = <-cs[0]' in output, got:\n%s", output)
}
}
func TestChanArrayParamGen(t *testing.T) {
input := `PROC worker([]CHAN OF INT cs)
SKIP
`
output := transpile(t, input)
if !strings.Contains(output, "func worker(cs []chan int)") {
t.Errorf("expected 'func worker(cs []chan int)' in output, got:\n%s", output)
}
}
func TestChanDirParamGen(t *testing.T) {
input := `PROC worker(CHAN OF INT input?, CHAN OF INT output!)
SEQ
INT x:
input ? x
output ! x
`
output := transpile(t, input)
if !strings.Contains(output, "func worker(input <-chan int, output chan<- int)") {
t.Errorf("expected directed channel types in output, got:\n%s", output)
}
}
func TestChanArrayDirParamGen(t *testing.T) {
input := `PROC worker([]CHAN OF INT cs?, []CHAN OF INT out!)
SKIP
`
output := transpile(t, input)
if !strings.Contains(output, "cs []chan int") {
t.Errorf("expected '[]chan int' for input chan array, got:\n%s", output)
}
if !strings.Contains(output, "out []chan int") {
t.Errorf("expected '[]chan int' for output chan array, got:\n%s", output)
}
}
func TestRecordFieldAccessCodegen(t *testing.T) {
input := `RECORD POINT
INT x:
INT y:
SEQ
POINT p:
INT v:
v := p[x]
`
output := transpile(t, input)
if !strings.Contains(output, "v = p.x") {
t.Errorf("expected 'v = p.x' in output, got:\n%s", output)
}
}
func TestSizeOperator(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"x := SIZE arr\n", "x = len(arr)"},
{"x := SIZE arr + 1\n", "x = (len(arr) + 1)"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestOpenArrayParamGen(t *testing.T) {
input := `PROC worker(VAL []INT arr, []BYTE data)
SKIP
`
output := transpile(t, input)
if !strings.Contains(output, "func worker(arr []int, data []byte)") {
t.Errorf("expected 'func worker(arr []int, data []byte)' in output, got:\n%s", output)
}
}
func TestAbbreviation(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"VAL INT x IS 42:\n", "var x int = 42"},
{"VAL BOOL flag IS TRUE:\n", "var flag bool = true"},
{"INT y IS z:\n", "var y int = z"},
{"INITIAL INT x IS 42:\n", "var x int = 42"},
{"INITIAL BOOL done IS FALSE:\n", "var done bool = false"},
{"VAL BYTE x IS 1:\n", "var x byte = 1"},
{"VAL INT16 x IS 42:\n", "var x int16 = 42"},
{"INITIAL BYTE done IS 0:\n", "var done byte = 0"},
}
for _, tt := range tests {
output := transpile(t, tt.input)
if !strings.Contains(output, tt.expected) {
t.Errorf("for input %q: expected %q in output, got:\n%s", tt.input, tt.expected, output)
}
}
}
func TestMultiAssignmentSimple(t *testing.T) {
input := `a, b := 1, 2
`
output := transpile(t, input)
if !strings.Contains(output, "a, b = 1, 2") {
t.Errorf("expected 'a, b = 1, 2' in output, got:\n%s", output)
}
}
func TestMultiAssignmentIndexed(t *testing.T) {
input := `x[0], x[1] := x[1], x[0]
`
output := transpile(t, input)
if !strings.Contains(output, "x[0], x[1] = x[1], x[0]") {
t.Errorf("expected 'x[0], x[1] = x[1], x[0]' in output, got:\n%s", output)
}
}
func TestMultiAssignmentMixed(t *testing.T) {
input := `a, x[0] := 1, 2
`
output := transpile(t, input)
if !strings.Contains(output, "a, x[0] = 1, 2") {
t.Errorf("expected 'a, x[0] = 1, 2' in output, got:\n%s", output)
}
}
func TestArrayLiteralCodegen(t *testing.T) {
input := `VAL x IS [10, 20, 30] :
`
output := transpile(t, input)
if !strings.Contains(output, "[]int{10, 20, 30}") {
t.Errorf("expected '[]int{10, 20, 30}' in output, got:\n%s", output)
}
}
func TestUntypedValCodegen(t *testing.T) {
input := `VAL x IS 42 :
PROC dummy()
SKIP
:
`
output := transpile(t, input)
if !strings.Contains(output, "var x = 42") {
t.Errorf("expected 'var x = 42' in output, got:\n%s", output)
}
}
func TestCAUSEERROR(t *testing.T) {
input := `PROC main()
CAUSEERROR()
:
`
output := transpile(t, input)
if !strings.Contains(output, `panic("CAUSEERROR")`) {
t.Errorf("expected 'panic(\"CAUSEERROR\")' in output, got:\n%s", output)
}
}
func TestGoIdentByteReserved(t *testing.T) {
input := `PROC main()
BYTE byte:
byte := 65
:
`
output := transpile(t, input)
if !strings.Contains(output, "_byte") {
t.Errorf("expected '_byte' in output, got:\n%s", output)
}
}
func TestMultiDimArrayDeclCodegen(t *testing.T) {
input := `[3][4]INT grid:
`
output := transpile(t, input)
if !strings.Contains(output, "grid := make([][]int, 3)") {
t.Errorf("expected 'grid := make([][]int, 3)' in output, got:\n%s", output)
}
if !strings.Contains(output, "grid[_i0] = make([]int, 4)") {
t.Errorf("expected 'grid[_i0] = make([]int, 4)' in output, got:\n%s", output)
}
}
func TestMultiDimChanDeclCodegen(t *testing.T) {
input := `[2][3]CHAN OF INT links:
`
output := transpile(t, input)
if !strings.Contains(output, "links := make([][]chan int, 2)") {
t.Errorf("expected 'links := make([][]chan int, 2)' in output, got:\n%s", output)
}
if !strings.Contains(output, "links[_i0] = make([]chan int, 3)") {
t.Errorf("expected 'links[_i0] = make([]chan int, 3)' in output, got:\n%s", output)
}
if !strings.Contains(output, "make(chan int)") {
t.Errorf("expected 'make(chan int)' in output, got:\n%s", output)
}
}
func TestMultiDimSendCodegen(t *testing.T) {
input := `cs[i][j] ! 42
`
output := transpile(t, input)
if !strings.Contains(output, "cs[i][j] <- 42") {
t.Errorf("expected 'cs[i][j] <- 42' in output, got:\n%s", output)
}
}
func TestMultiDimReceiveCodegen(t *testing.T) {
input := `cs[i][j] ? x
`
output := transpile(t, input)
if !strings.Contains(output, "x = <-cs[i][j]") {
t.Errorf("expected 'x = <-cs[i][j]' in output, got:\n%s", output)
}
}
func TestMultiDimAssignmentCodegen(t *testing.T) {
input := `grid[i][j] := 42
`
output := transpile(t, input)
if !strings.Contains(output, "grid[i][j] = 42") {
t.Errorf("expected 'grid[i][j] = 42' in output, got:\n%s", output)
}
}
func TestMultiDimProcParamCodegen(t *testing.T) {
input := `PROC fill([][]CHAN OF INT grid)
SKIP
`
output := transpile(t, input)
if !strings.Contains(output, "func fill(grid [][]chan int)") {
t.Errorf("expected 'func fill(grid [][]chan int)' in output, got:\n%s", output)
}
}
func TestEntryHarnessRawTerminal(t *testing.T) {
input := `PROC echo(CHAN OF BYTE keyboard?, screen!, error!)
BYTE ch:
SEQ
keyboard ? ch
screen ! ch
:
`
output := transpile(t, input)
// Should contain raw terminal mode setup
for _, want := range []string{
"term.IsTerminal",
"term.MakeRaw",
"term.Restore",
`"golang.org/x/term"`,
`"os/signal"`,
`"syscall"`,
"rawMode",
} {
if !strings.Contains(output, want) {
t.Errorf("expected %q in entry harness output, got:\n%s", want, output)
}
}
}