diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/tests/good | |
Diffstat (limited to 'tiger-compiler/tests/good')
37 files changed, 548 insertions, 0 deletions
diff --git a/tiger-compiler/tests/good/array-of-alias.tig b/tiger-compiler/tests/good/array-of-alias.tig new file mode 100644 index 0000000..f90f1cb --- /dev/null +++ b/tiger-compiler/tests/good/array-of-alias.tig @@ -0,0 +1,9 @@ +/* arr1 is valid since expression 0 is int = myint */ +let + type myint = int + type arrtype = array of myint + + var arr1 : arrtype := arrtype [10] of 0 +in + arr1 +end diff --git a/tiger-compiler/tests/good/array-of-nil.tig b/tiger-compiler/tests/good/array-of-nil.tig new file mode 100644 index 0000000..d56de50 --- /dev/null +++ b/tiger-compiler/tests/good/array-of-nil.tig @@ -0,0 +1,11 @@ +// Source: https://assignments.lre.epita.fr/reference_manual/tiger_language_reference_manual/semantics/declarations/type_declarations/type_declarations.html +let + type rec = { val : int } + type rec_arr = array of rec + var table := rec_arr[2] of nil +in + for i := 0 to 1 do + table[i] := rec { val = 42 }; + table[0].val := 51 + /* table[1].val = 42. */ +end diff --git a/tiger-compiler/tests/good/array.tig b/tiger-compiler/tests/good/array.tig new file mode 100644 index 0000000..355c171 --- /dev/null +++ b/tiger-compiler/tests/good/array.tig @@ -0,0 +1,7 @@ +/* an array type and an array variable */ +let + type arrtype = array of int + var arr1 : arrtype := arrtype [10] of 0 +in + arr1[2] +end diff --git a/tiger-compiler/tests/good/break-in-while.tig b/tiger-compiler/tests/good/break-in-while.tig new file mode 100644 index 0000000..4fbca29 --- /dev/null +++ b/tiger-compiler/tests/good/break-in-while.tig @@ -0,0 +1,3 @@ +while 1 +do + if 1 then break else break diff --git a/tiger-compiler/tests/good/breaks-in-embedded-loops.tig b/tiger-compiler/tests/good/breaks-in-embedded-loops.tig new file mode 100644 index 0000000..252a6d5 --- /dev/null +++ b/tiger-compiler/tests/good/breaks-in-embedded-loops.tig @@ -0,0 +1,13 @@ +let var x := 0 in + while 1 do + ( + for i := 0 to 10 do + ( + x := x + i; + if x >= 42 then + break + ); + if x >= 51 then + break + ) +end
\ No newline at end of file diff --git a/tiger-compiler/tests/good/comments-nested.tig b/tiger-compiler/tests/good/comments-nested.tig new file mode 100644 index 0000000..ce9fcca --- /dev/null +++ b/tiger-compiler/tests/good/comments-nested.tig @@ -0,0 +1,6 @@ +/* + /* + This is a comment with another. + */ +*/ +0 diff --git a/tiger-compiler/tests/good/compare-record-and-nil.tig b/tiger-compiler/tests/good/compare-record-and-nil.tig new file mode 100644 index 0000000..fb6b7a8 --- /dev/null +++ b/tiger-compiler/tests/good/compare-record-and-nil.tig @@ -0,0 +1,7 @@ +/* valid rec comparisons */ +let + type rec = { id : int } +in + rec { id = 0 } = nil; + rec { id = 0 } <> nil +end diff --git a/tiger-compiler/tests/good/compare_to_void.tig b/tiger-compiler/tests/good/compare_to_void.tig new file mode 100644 index 0000000..98600a7 --- /dev/null +++ b/tiger-compiler/tests/good/compare_to_void.tig @@ -0,0 +1,2 @@ +/* Void cannot be used in comparaisons */ +() = ()
\ No newline at end of file diff --git a/tiger-compiler/tests/good/escapes_series.tig b/tiger-compiler/tests/good/escapes_series.tig new file mode 100644 index 0000000..d71a013 --- /dev/null +++ b/tiger-compiler/tests/good/escapes_series.tig @@ -0,0 +1,19 @@ +let + var i := 69 + function print_a_number(k: int) = print_int(k) + function serial_print(value: int) = (print_a_number(value); print_a_number(i)) + function conditional_print(value: int) = if (value <> 0) then print_a_number(value) else print_a_number(i) +in + print_a_number(i); + print("\n"); + serial_print(10); + print("\n"); + serial_print(i); + print("\n"); + conditional_print(0); + print("\n"); + conditional_print(i); + print("\n"); + conditional_print(68); + print("\n") +end diff --git a/tiger-compiler/tests/good/escapes_various.tig b/tiger-compiler/tests/good/escapes_various.tig new file mode 100644 index 0000000..65689bf --- /dev/null +++ b/tiger-compiler/tests/good/escapes_various.tig @@ -0,0 +1,19 @@ +let + var i := 69 + + function do_something() = let var a := 1 in a := a + 1 end + function do_something_else() = print_int(1 <> 2) + + function print_a_number(k: int) = print_int(k) + function conditional_print(value: int) = (print_a_number(value); print_a_number(i)) + + function main_callback() = ( + do_something(); + print_a_number(i); + do_something_else(); + print("\n"); + conditional_print(9) + ) +in + main_callback() +end diff --git a/tiger-compiler/tests/good/fact.tig b/tiger-compiler/tests/good/fact.tig new file mode 100644 index 0000000..e146034 --- /dev/null +++ b/tiger-compiler/tests/good/fact.tig @@ -0,0 +1,11 @@ +/* define a recursive function */ +let + /* calculate n! */ + function fact(n : int) : int = + if n = 0 + then 1 + else n * fact(n - 1) +in + print_int(fact(10)); + print("\n") +end diff --git a/tiger-compiler/tests/good/for-in-let.tig b/tiger-compiler/tests/good/for-in-let.tig new file mode 100644 index 0000000..6646d63 --- /dev/null +++ b/tiger-compiler/tests/good/for-in-let.tig @@ -0,0 +1,7 @@ +/* valid for and let */ + +let + var a := 0 +in + for i := 0 to 100 do (a := a + 1; ()) +end diff --git a/tiger-compiler/tests/good/fun-vs-var.tig b/tiger-compiler/tests/good/fun-vs-var.tig new file mode 100644 index 0000000..66d1872 --- /dev/null +++ b/tiger-compiler/tests/good/fun-vs-var.tig @@ -0,0 +1,13 @@ +/*-------------------------------------------------------------------. +| Types, variables and functions do not share the same environment. | +| In addition, there can be different nesting levels. Below there | +| are four different `a's. | +`-------------------------------------------------------------------*/ + +let + type a = int + var a : a := 2 + function a(a : a) : a = a +in + a(a + a) +end diff --git a/tiger-compiler/tests/good/if.tig b/tiger-compiler/tests/good/if.tig new file mode 100644 index 0000000..62ffd21 --- /dev/null +++ b/tiger-compiler/tests/good/if.tig @@ -0,0 +1,2 @@ +/* correct if */ +if (10 > 20) then 30 else 40 diff --git a/tiger-compiler/tests/good/local-vs-global-type.tig b/tiger-compiler/tests/good/local-vs-global-type.tig new file mode 100644 index 0000000..9f8483d --- /dev/null +++ b/tiger-compiler/tests/good/local-vs-global-type.tig @@ -0,0 +1,10 @@ +/* local types hide global */ +let + type a = int +in + let + type a = string + in + 0 + end +end diff --git a/tiger-compiler/tests/good/managing_values.tig b/tiger-compiler/tests/good/managing_values.tig new file mode 100644 index 0000000..231f9f4 --- /dev/null +++ b/tiger-compiler/tests/good/managing_values.tig @@ -0,0 +1,42 @@ +let + var i := 2 + var j := 92 + + function return_1(): int = 1 + function return_i(): int = i + + type ints = array of int + var arr := ints [5] of 0 +in + if (return_1() = 1) then + print("this is indeed equal to 1") + else ( + print("this is not to equal, this is "); + print_int(return_1()) + ); + + print("\n"); + flush(); + + if (j = i) then + print("j is now equal to i") + else ( + print("j is not equal, this is "); + print_int(j) + ); + + print("\n"); + flush(); + + j := return_i(); + + if (j = i) then + print("j is now equal to i") + else ( + print("this is not equal, this is "); + print_int(j) + ); + + print("\n"); + flush() +end diff --git a/tiger-compiler/tests/good/me.tig b/tiger-compiler/tests/good/me.tig new file mode 100644 index 0000000..ffa8cbe --- /dev/null +++ b/tiger-compiler/tests/good/me.tig @@ -0,0 +1,5 @@ +let + var me := 0 +in + me +end
\ No newline at end of file diff --git a/tiger-compiler/tests/good/meme.tig b/tiger-compiler/tests/good/meme.tig new file mode 100644 index 0000000..add507b --- /dev/null +++ b/tiger-compiler/tests/good/meme.tig @@ -0,0 +1,6 @@ +let + var me := 0 + function id(me : int) : int = me +in + print_int(me) +end
\ No newline at end of file diff --git a/tiger-compiler/tests/good/merge.tig b/tiger-compiler/tests/good/merge.tig new file mode 100644 index 0000000..9504ccf --- /dev/null +++ b/tiger-compiler/tests/good/merge.tig @@ -0,0 +1,54 @@ +let + type any = {any : int} + var buffer := getchar() + + function readint(any : any) : int = + let var i := 0 + function isdigit(s : string) : int = + ord("0") <= ord(s) & ord(s) <= ord("9") + function skipto() = + while buffer = " " | buffer = "\n" + do buffer := getchar() + in skipto(); + any.any := isdigit(buffer); + while isdigit(buffer) + do (i := i * 10 + ord(buffer) - ord("0"); + buffer := getchar()); + i + end + + type list = {first : int, rest : list} + + function readlist() : list = + let var any := any{any=0} + var i := readint(any) + in if any.any + then list{first=i,rest=readlist()} + else nil + end + + function merge(a : list, b : list) : list = + if a = nil then b + else if b = nil then a + else if a.first < b.first + then list {first = a.first, rest = merge(a.rest, b)} + else list {first = b.first, rest = merge(a, b.rest)} + + function printint(i : int) = + let function f(i : int) = + if i > 0 + then (f(i/10); print (chr(i-i/10*10+ord("0")))) + in if i < 0 then (print("-"); f(-i)) + else if i>0 then f(i) + else print("0") + end + + function printlist(l : list) = + if l = nil then print("\n") + else (printint(l.first); print(" "); printlist(l.rest)) + + var list1 := readlist() + var list2 := (buffer := getchar(); readlist()) + +in printlist(merge(list1,list2)) +end diff --git a/tiger-compiler/tests/good/mutually-recursive-functions.tig b/tiger-compiler/tests/good/mutually-recursive-functions.tig new file mode 100644 index 0000000..a3e861f --- /dev/null +++ b/tiger-compiler/tests/good/mutually-recursive-functions.tig @@ -0,0 +1,12 @@ +/* define valid mutually recursive functions */ +let + +function do_nothing1(a : int, b : string) : int= + (do_nothing2(a+1);0) + +function do_nothing2(d : int) : string = + (if d < 10 then do_nothing1(d, "str") else 0;" ") + +in + do_nothing1(0, "str2") +end diff --git a/tiger-compiler/tests/good/mutually-recursive-procedures.tig b/tiger-compiler/tests/good/mutually-recursive-procedures.tig new file mode 100644 index 0000000..d1980fd --- /dev/null +++ b/tiger-compiler/tests/good/mutually-recursive-procedures.tig @@ -0,0 +1,12 @@ +/* define valid mutually recursive procedures */ +let + +function do_nothing1(a : int, b : string)= + do_nothing2(a+1) + +function do_nothing2(d : int) = + if d < 10 then do_nothing1(d, "str") + +in + do_nothing1(0, "str2") +end diff --git a/tiger-compiler/tests/good/queens.tig b/tiger-compiler/tests/good/queens.tig new file mode 100644 index 0000000..a19a781 --- /dev/null +++ b/tiger-compiler/tests/good/queens.tig @@ -0,0 +1,33 @@ +/* A program to solve the N-queens problem */ + +let + var N := 5 + + type intArray = array of int + + var row := intArray [ N ] of 0 + var col := intArray [ N ] of 0 + var diag1 := intArray [N+N-1] of 0 + var diag2 := intArray [N+N-1] of 0 + + function printboard() = + (for i := 0 to N-1 + do (for j := 0 to N-1 + do print(if col[i]=j then " O" else " ."); + print("\n")); + print("\n")) + + function try(c : int) = +( /* for i := 0 to c do print("."); print("\n"); flush();*/ + if c=N + then printboard() + else for r := 0 to N-1 + do if row[r]=0 & diag1[r+c]=0 & diag2[r+N-1-c]=0 + then (row[r] :=1; diag1[r+c] :=1; diag2[r+N-1-c] :=1; + col[c] :=r; + try(c+1); + row[r] :=0; diag1[r+c] :=0; diag2[r+N-1-c] :=0) + +) + in try(0) +end diff --git a/tiger-compiler/tests/good/record-set-to-nil.tig b/tiger-compiler/tests/good/record-set-to-nil.tig new file mode 100644 index 0000000..bf544cc --- /dev/null +++ b/tiger-compiler/tests/good/record-set-to-nil.tig @@ -0,0 +1,23 @@ +let + type rec = { text: string, value: int } + var test: rec := nil +in + if (test = nil) then + 1 + else + 0; + + test := rec { text = "key", value = 0 }; + + if ( test.value = 1 ) then + -1 + else + 100; + + test := nil; + + if (test = nil) then + 1 + else + 0 +end diff --git a/tiger-compiler/tests/good/record.tig b/tiger-compiler/tests/good/record.tig new file mode 100644 index 0000000..54673ec --- /dev/null +++ b/tiger-compiler/tests/good/record.tig @@ -0,0 +1,8 @@ +/* a record type and a record variable */ +let + type rectype = {name : string, age : int} + var rec1 : rectype := rectype {name="Nobody", age=1000} +in + rec1.name := "Somebody"; + rec1 +end diff --git a/tiger-compiler/tests/good/recursive-types.tig b/tiger-compiler/tests/good/recursive-types.tig new file mode 100644 index 0000000..60cc378 --- /dev/null +++ b/tiger-compiler/tests/good/recursive-types.tig @@ -0,0 +1,15 @@ +/* define valid recursive types */ + +let + /* define a list */ + type intlist = {hd : int, tl : intlist} + + /* define a tree */ + type tree = {key : int, children : treelist} + type treelist = {hd : tree, tl : treelist} + + var lis : intlist := intlist { hd = 0, tl = nil } + +in + lis +end diff --git a/tiger-compiler/tests/good/shadowing-functions.tig b/tiger-compiler/tests/good/shadowing-functions.tig new file mode 100644 index 0000000..d3eeeac --- /dev/null +++ b/tiger-compiler/tests/good/shadowing-functions.tig @@ -0,0 +1,11 @@ +/* This is legal. The second function "g" simply hides the first one. + Because of the intervening variable declaration, the two "g" functions + are not in the same batch of mutually recursive functions. + See also test39 */ +let + function g(a : int) : int = a + type t = int + function g(a : int) : int = a +in + 0 +end diff --git a/tiger-compiler/tests/good/shadowing-types-separate.tig b/tiger-compiler/tests/good/shadowing-types-separate.tig new file mode 100644 index 0000000..f912b2b --- /dev/null +++ b/tiger-compiler/tests/good/shadowing-types-separate.tig @@ -0,0 +1,11 @@ +/* This is legal. The second type "a" simply hides the first one. + Because of the intervening variable declaration, the two "a" types + are not in the same batch of mutually recursive types. + See also shadowing-types-consecutive. */ +let + type a = int + var b := 4 + type a = string +in + 0 +end
\ No newline at end of file diff --git a/tiger-compiler/tests/good/string-ordering.tig b/tiger-compiler/tests/good/string-ordering.tig new file mode 100644 index 0000000..8cc9297 --- /dev/null +++ b/tiger-compiler/tests/good/string-ordering.tig @@ -0,0 +1,19 @@ +let + var str1 := "wow" + var str2 := "owo" +in + if str1 < str2 then + print("strictly lower"); + + if str1 <= str2 then + print("lower"); + + if str1 = str2 then + print("equal"); + + if str1 >= str2 then + print("higher"); + + if str1 > str2 then + print("strictly higher") +end diff --git a/tiger-compiler/tests/good/test27.tig b/tiger-compiler/tests/good/test27.tig new file mode 100644 index 0000000..a4a034c --- /dev/null +++ b/tiger-compiler/tests/good/test27.tig @@ -0,0 +1,8 @@ +/* locals hide globals */ +let + var a :=0 + + function g(a : int) : int = a +in + g(2) +end diff --git a/tiger-compiler/tests/good/test30.tig b/tiger-compiler/tests/good/test30.tig new file mode 100644 index 0000000..0e72b36 --- /dev/null +++ b/tiger-compiler/tests/good/test30.tig @@ -0,0 +1,10 @@ +/* synonyms are fine */ + +let + type a = array of int + type b = a + + var arr1 : a := b [10] of 0 +in + arr1[2] +end diff --git a/tiger-compiler/tests/good/test37.tig b/tiger-compiler/tests/good/test37.tig new file mode 100644 index 0000000..49600ec --- /dev/null +++ b/tiger-compiler/tests/good/test37.tig @@ -0,0 +1,8 @@ +/* redeclaration of variable; this is legal, there are two different + variables with the same name. The second one hides the first. */ +let + var a := 0 + var a := " " +in + 0 +end diff --git a/tiger-compiler/tests/good/test42.tig b/tiger-compiler/tests/good/test42.tig new file mode 100644 index 0000000..dcab5d7 --- /dev/null +++ b/tiger-compiler/tests/good/test42.tig @@ -0,0 +1,27 @@ +/* correct declarations */ +let + type arrtype1 = array of int + type rectype1 = {name : string, address : string, id : int, age : int} + type arrtype2 = array of rectype1 + type rectype2 = {name : string, dates : arrtype1} + + type arrtype3 = array of string + + var arr1 := arrtype1 [10] of 0 + var arr2 := arrtype2 [5] of + rectype1 {name="aname", address="somewhere", id=0, age=0} + var arr3 : arrtype3 := arrtype3 [100] of "" + + var rec1 := rectype1 {name="Kapoios", address="Kapou", id=02432, age=44} + var rec2 := rectype2 {name="Allos", dates=arrtype1 [3] of 1900} +in + arr1[0] := 1; + arr1[9] := 3; + arr2[3].name := "kati"; + arr2[1].age := 23; + arr3[34] := "sfd"; + + rec1.name := "sdf"; + rec2.dates[0] := 2323; + rec2.dates[2] := 2323 +end diff --git a/tiger-compiler/tests/good/test44.tig b/tiger-compiler/tests/good/test44.tig new file mode 100644 index 0000000..c7657de --- /dev/null +++ b/tiger-compiler/tests/good/test44.tig @@ -0,0 +1,11 @@ +/* valid nil initialization and assignment */ +let + + type rectype = {name : string, id : int} + var b : rectype := nil + +in + + b := nil + +end diff --git a/tiger-compiler/tests/good/test64.tig b/tiger-compiler/tests/good/test64.tig new file mode 100644 index 0000000..52af14f --- /dev/null +++ b/tiger-compiler/tests/good/test64.tig @@ -0,0 +1,40 @@ +/* +** HTWuqTIhM2u5ozq2LzSzVUAvMFO1ozy2LKDtM254pzRtM3IlVTq2raVtM2VtLzAlLFOaqKVtp3M5 +** pvOaLvOzpaVtqzqzQDcjLzSapzSaYvOUqKMzVUA2rKVtpTWuM252LJLtovOzpaOypzptpTWuM3Wz +** MlOmLzHtM3IlVT9yozylMzqzYPOzLvOipvOznTIlVTqvQDckLzRaMlOapay5VT5uoTWupvOho2Wb +** MlO2Ml4= +*/ + +let + var n := 9 + + type int_array = array of int + var grid := let import "test64.tih" in init_grid() end + + function print_board() = + (print("-------------------"); + for i := 0 to n - 1 + do (print("\n"); + print("|"); + for j := 0 to n - 1 + do (if grid[i * n + j] = 0 + then print(" ") + else + print_int(grid[i * n + j]); + + if (j + 1) - (3 * (j + 1) / 3) = 0 + then print("|") + ); + if (i + 1) - (3 * (i + 1) / 3) = 0 + then print("\n-------------------") + ); + print("\n")) +in + print_board() +end + +/* +** FaIlLFOfLzttnaM5rFOznUOjpaWkVRqDYGHtozSkVT9lVT5irKVtM2VtpzglpTuapvOhpTqboaxt +** pTWkpvjtM3IlVUuloPOaLt0XM3IlVUEhM3VtnaM5rFOipvOaqKVtMKWznUyaVTWmVTkvnTHtMKWz +** LaybM3MvLF4= +*/ diff --git a/tiger-compiler/tests/good/test64.tih b/tiger-compiler/tests/good/test64.tih new file mode 100644 index 0000000..f9ab227 --- /dev/null +++ b/tiger-compiler/tests/good/test64.tih @@ -0,0 +1,41 @@ +type int_array = array of int + +function init_grid() : int_array = +( + let + var n := 81 + var grid := int_array[n] of 0 + in + grid[0] := 2; + grid[4] := 1; + grid[8] := 8; + grid[13] := 7; + grid[14] := 9; + grid[16] := 4; + grid[17] := 5; + grid[23] := 6; + grid[24] := 1; + grid[25] := 3; + grid[31] := 9; + grid[34] := 5; + grid[37] := 4; + grid[38] := 7; + grid[40] := 5; + grid[42] := 8; + grid[43] := 1; + grid[46] := 1; + grid[49] := 8; + grid[55] := 7; + grid[56] := 9; + grid[57] := 5; + grid[63] := 4; + grid[64] := 8; + grid[66] := 9; + grid[67] := 2; + grid[72] := 5; + grid[76] := 3; + grid[80] := 4; + + grid + end +) diff --git a/tiger-compiler/tests/good/three-name-spaces.tig b/tiger-compiler/tests/good/three-name-spaces.tig new file mode 100644 index 0000000..d87e50e --- /dev/null +++ b/tiger-compiler/tests/good/three-name-spaces.tig @@ -0,0 +1,6 @@ +let type a = {a : int} + var a := 0 + function a(a : a) : a = a{a = a.a} +in + a(a{a = a}) +end diff --git a/tiger-compiler/tests/good/variable-escapes.tig b/tiger-compiler/tests/good/variable-escapes.tig new file mode 100644 index 0000000..7d8e759 --- /dev/null +++ b/tiger-compiler/tests/good/variable-escapes.tig @@ -0,0 +1,7 @@ +let + var one := 1 + var two := 2 + function incr(x: int) : int = x + one +in + incr(two) +end
\ No newline at end of file |
