X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=compiler%2Fsrc%2Flib%2Ftiger%2Ftiger_test_cases.ml;h=bff7efc1d49884dcb08687dd90256a76de96fcd8;hb=3be8511c0587d12da978306068c143c71b49f57c;hp=f0a81d487ed637a0be2df01f78846a04d196e14b;hpb=d3bdde4b6b0b1a8cb41ee4de4fa73cd472ed23a4;p=tiger.ml.git diff --git a/compiler/src/lib/tiger/tiger_test_cases.ml b/compiler/src/lib/tiger/tiger_test_cases.ml index f0a81d4..bff7efc 100644 --- a/compiler/src/lib/tiger/tiger_test_cases.ml +++ b/compiler/src/lib/tiger/tiger_test_cases.ml @@ -1,3 +1,4 @@ +module Error = Tiger_error module Test = Tiger_test let book = @@ -130,79 +131,137 @@ let book = [ IF; LPAREN; INT 5; GT; INT 4; RPAREN; THEN; INT 13; ELSE; STRING " " ] ) - ] - -(* -let test_case_from_book_queens = - let code = - "\ - /* A program to solve the 8-queens problem */ \n\ - \n\ - let \n\ - var N := 8 \n\ - \n\ - type intArray = array of int \n\ - \n\ - var row := intArray [ N ] of 0 \n\ - var col := intArray [ N ] of 0 \n\ - var diag1 := intArray [N+N-1] of 0 \n\ - var diag2 := intArray [N+N-1] of 0 \n\ - \n\ - function printboard() = ( \n\ - for i := 0 to N-1 do ( \n\ - for j := 0 to N-1 do print(if col[i]=j then \" O\" else \" .\"); \n\ - print(\"\n\") \n\ - ); \n\ - print(\"\n\") \n\ - ) \n\ - \n\ - function try(c:int) = ( \n\ - /* for i:= 0 to c do print(\".\"); print(\"\n\"); flush();*/ \n\ - if c=N \n\ - then printboard() \n\ - else \n\ - for r := 0 to N-1 \n\ - do \n\ - if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 \n\ - then ( \n\ - row[r] := 1; \n\ - diag1[r+c] := 1; \n\ - diag2[r+7-c] := 1; \n\ - col[c] := r; \n\ - try(c+1); \n\ - row[r] := 0; \n\ - diag1[r+c] := 0; \n\ - diag2[r+7-c] := 0 \n\ + ~is_error_expected_semant:Error.is_wrong_type (* TODO: Be more specific *) + ; Test.case + "Book test: 8-queens" + ~code: + "\ + /* A program to solve the 8-queens problem */ \n\ + \n\ + let \n\ + var N := 8 \n\ + \n\ + type intArray = array of int \n\ + \n\ + var row := intArray [ N ] of 0 \n\ + var col := intArray [ N ] of 0 \n\ + var diag1 := intArray [N+N-1] of 0 \n\ + var diag2 := intArray [N+N-1] of 0 \n\ + \n\ + function printboard() = ( \n\ + for i := 0 to N-1 do ( \n\ + for j := 0 to N-1 do print(if col[i]=j then \" O\" else \" .\"); \n\ + print(\"\n\") \n\ + ); \n\ + print(\"\n\") \n\ ) \n\ - ) \n\ - in \n\ - try(0) \n\ - end \n\ - " - in - (code, code, []) -*) + \n\ + function try(c:int) = ( \n\ + /* for i:= 0 to c do print(\".\"); print(\"\n\"); flush();*/ \n\ + if c=N \n\ + then printboard() \n\ + else \n\ + for r := 0 to N-1 \n\ + do \n\ + if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 \n\ + then ( \n\ + row[r] := 1; \n\ + diag1[r+c] := 1; \n\ + diag2[r+7-c] := 1; \n\ + col[c] := r; \n\ + try(c+1); \n\ + row[r] := 0; \n\ + diag1[r+c] := 0; \n\ + diag2[r+7-c] := 0 \n\ + ) \n\ + ) \n\ + in \n\ + try(0) \n\ + end \n\ + " + ] let micro = let open Tiger_parser in [ (let code = "nil" in Test.case code ~code ~out_lexing:[NIL]) ; (let code = "5" in Test.case code ~code ~out_lexing:[INT 5]) ; (let code = "-5" in Test.case code ~code ~out_lexing:[MINUS; INT 5]) - ; (let code = "f()" in Test.case code ~code ~out_lexing:[ID "f"; LPAREN; RPAREN]) - ; (let code = "abc.i" in Test.case code ~code ~out_lexing:[ID "abc"; DOT; ID "i"]) - ; (let code = "abc[0]" in Test.case code ~code ~out_lexing:[ID "abc"; LBRACK; INT 0; RBRACK]) - - ; (let code = "abc[0] := foo()" in Test.case code ~code - ~out_lexing: - [ID "abc"; LBRACK; INT 0; RBRACK; ASSIGN; ID "foo"; LPAREN; RPAREN]) - - ; (let code = "abc [5] of nil" in Test.case code ~code - ~out_lexing: - [ID "abc"; LBRACK; INT 5; RBRACK; OF; NIL]) - - ; (let code = "f(\"a\", 3, foo)" in Test.case code ~code - ~out_lexing: - [ID "f"; LPAREN; STRING "a"; COMMA; INT 3; COMMA; ID "foo"; RPAREN]) + ; ( let code = "f()" in + Test.case + code + ~code + ~out_lexing:[ID "f"; LPAREN; RPAREN] + ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *) + ) + ; ( let code = "abc.i" in + Test.case + code + ~code + ~out_lexing:[ID "abc"; DOT; ID "i"] + ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *) + ) + ; ( let code = "abc[0]" in + Test.case + code + ~code + ~out_lexing:[ID "abc"; LBRACK; INT 0; RBRACK] + ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *) + ) + ; ( let code = "abc[0] := foo()" in + Test.case + code + ~code + ~out_lexing: + [ID "abc"; LBRACK; INT 0; RBRACK; ASSIGN; ID "foo"; LPAREN; RPAREN] + ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *) + ) + ; ( let code = "abc [5] of nil" in + Test.case + code + ~code + ~out_lexing:[ID "abc"; LBRACK; INT 5; RBRACK; OF; NIL] + ~is_error_expected_semant:Error.is_unknown_type (* TODO: Be more specific *) + ) + ; ( let code = "f(\"a\", 3, foo)" in + Test.case + code + ~code + ~out_lexing: + [ID "f"; LPAREN; STRING "a"; COMMA; INT 3; COMMA; ID "foo"; RPAREN] + ~is_error_expected_semant:Error.is_unknown_id + ) + ; ( let code = + "let + type a = int + type b = a + type c = b + var i : a := 2 + var j : c := 3 + in + i := j + end + " + in + Test.case + "Type aliases" + ~code + ) + ; ( let code = + "let + type a = {x:int, y:int} + type b = {x:int, y:int} /* new type generated */ + var foo : a := a {x = 1, y = 2} + var bar : b := b {x = 1, y = 2} + in + foo = bar /* incompatible types */ + end + " + in + Test.case + code + ~code + ~is_error_expected_semant:Error.is_wrong_type (* TODO: Be more specific *) + ) ] let all =