8 Commits

Author SHA1 Message Date
Vectabyte 5643aa703e removed unneeded .class files 2026-05-30 15:21:24 +02:00
Vectabyte 74f68cb525 Bytecode Adjustments 2026-05-20 15:42:59 +02:00
Vectabyte f10f7883dd test adjustments and fixes 2026-05-20 14:19:04 +02:00
Vectabyte a8c6a056da Fixed Lexer Errors with Tests 2026-05-20 10:56:58 +02:00
Felix Weglehner c7796b4f25 Added constructor support to parser 2026-05-20 10:27:00 +02:00
Vectabyte cc2d157674 Test Fixes 2026-05-20 10:03:18 +02:00
Vectabyte d576e64534 updated registry 2026-05-20 09:25:52 +02:00
Vectabyte cec34d1d9e More Tests! 2026-05-20 09:25:44 +02:00
76 changed files with 1470 additions and 159 deletions
+2
View File
@@ -172,6 +172,8 @@ collectFromTypedStmt (TAST.If cond then_ mElse _) =
++ collectFromMaybeStmt mElse
collectFromTypedStmt (TAST.StmtExprStmt se _) =
collectFromTypedStmtExpr se
collectFromTypedStmt (TAST.EmptyStmt _) =
[]
collectFromMaybeStmt :: Maybe TAST.TypedStmt -> CP_Infos
collectFromMaybeStmt Nothing = []
+1
View File
@@ -75,6 +75,7 @@ serializeInstruction instr = case instr of
IAdd -> [0x60]
ISub -> [0x64]
IMul -> [0x68]
IDiv -> [0x6C]
-- Control flow: 2-byte signed branch offset
IfEq offset -> 0x99 : indexBytes offset
IfNe offset -> 0x9A : indexBytes offset
+7 -2
View File
@@ -17,6 +17,7 @@ import Grammar.AST
return { TokenReturn }
new { TokenNew }
this { TokenThis }
null { TokenNull }
if { TokenIf }
else { TokenElse }
while { TokenWhile }
@@ -91,6 +92,9 @@ MethodDecl : Type id '(' Params ')' Block { Method $1 $2 $4 $6 }
| void id '(' Params ')' Block { Method "void" $2 $4 $6 }
| public void id '(' Params ')' Block { Method "void" $3 $5 $7 }
| private void id '(' Params ')' Block { Method "void" $3 $5 $7 }
| id '(' Params ')' Block { Method "void" "<init>" $3 $5 }
| private id '(' Params ')' Block { Method "void" "<init>" $4 $6 }
| public id '(' Params ')' Block { Method "void" "<init>" $4 $6 }
Params : ParamList { $1 }
| {- leer -} { [] }
@@ -129,10 +133,10 @@ StmtExpr : Expr '=' Expr { Assign $1 $3 }
| Expr '.' id '(' Exprs ')' { MethodCall $1 $3 $5 }
| id '(' Exprs ')' { MethodCall This $1 $3 }
| Expr '+' '=' Expr { Assign $1 (Binary Add $1 $4) }
| Expr '-' '=' Expr { Assign $1 (Binary Subtract $1 $4) }
| Expr '-' '=' Expr { Assign $1 (Binary Subtract $1 $4) }
| Expr '*' '=' Expr { Assign $1 (Binary Multiply $1 $4) }
| Expr '/' '=' Expr { Assign $1 (Binary Divide $1 $4) }
| Expr '%' '=' Expr { Assign $1 (Binary Modulo $1 $4) }
| Expr '%' '=' Expr { Assign $1 (Binary Modulo $1 $4) }
Expr : this { This }
| id { LocalOrFieldVar $1 }
@@ -140,6 +144,7 @@ Expr : this { This }
| intLit { Integer $1 }
| boolLit { Bool $1 }
| charLit { Char $1 }
| null { Null }
| '(' Expr ')' { $2 }
| '!' Expr { Unary Not $2 }
| '-' Expr { Unary Negate $2 }
+2
View File
@@ -21,6 +21,7 @@ tokens :-
return { \_ -> TokenReturn }
new { \_ -> TokenNew }
this { \_ -> TokenThis }
null { \_ -> TokenNull }
if { \_ -> TokenIf }
else { \_ -> TokenElse }
while { \_ -> TokenWhile }
@@ -64,6 +65,7 @@ data Token
| TokenReturn
| TokenNew
| TokenThis
| TokenNull
| TokenIf
| TokenElse
| TokenWhile
+1
View File
@@ -34,4 +34,5 @@ data TypedStmt
| LocalVarDecl Type String Type
| If TypedExpr TypedStmt (Maybe TypedStmt) Type
| StmtExprStmt TypedStmtExpr Type
| EmptyStmt Type
deriving (Show, Eq)
@@ -0,0 +1,89 @@
module Testsuite.AbcFiles.AllSyntaxTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 8},
BtcLine {lineNumber = 4, instruction = ALoad0},
BtcLine {lineNumber = 5, instruction = ILoad 1},
BtcLine {lineNumber = 6, instruction = PutField 2},
BtcLine {lineNumber = 9, instruction = ALoad0},
BtcLine {lineNumber = 10, instruction = IConst 0},
BtcLine {lineNumber = 11, instruction = PutField 3},
BtcLine {lineNumber = 14, instruction = ALoad0},
BtcLine {lineNumber = 15, instruction = ALoad0},
BtcLine {lineNumber = 16, instruction = GetField 3},
BtcLine {lineNumber = 19, instruction = IConst 1},
BtcLine {lineNumber = 20, instruction = IAdd},
BtcLine {lineNumber = 21, instruction = PutField 3},
BtcLine {lineNumber = 24, instruction = Return}
]
),
( "inc",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = ALoad0},
BtcLine {lineNumber = 2, instruction = GetField 2},
BtcLine {lineNumber = 5, instruction = IConst 1},
BtcLine {lineNumber = 6, instruction = IAdd},
BtcLine {lineNumber = 7, instruction = PutField 2},
BtcLine {lineNumber = 10, instruction = ALoad0},
BtcLine {lineNumber = 11, instruction = GetField 2},
BtcLine {lineNumber = 14, instruction = IReturn}
]
),
( "inc",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = ALoad0},
BtcLine {lineNumber = 2, instruction = GetField 2},
BtcLine {lineNumber = 5, instruction = ILoad 1},
BtcLine {lineNumber = 6, instruction = IAdd},
BtcLine {lineNumber = 7, instruction = PutField 2},
BtcLine {lineNumber = 10, instruction = ALoad0},
BtcLine {lineNumber = 11, instruction = GetField 2},
BtcLine {lineNumber = 14, instruction = IReturn}
]
),
( "sumUpTo",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = IConst 0},
BtcLine {lineNumber = 1, instruction = IStore 2},
BtcLine {lineNumber = 2, instruction = IConst 1},
BtcLine {lineNumber = 3, instruction = IStore 3},
BtcLine {lineNumber = 4, instruction = ILoad 3},
BtcLine {lineNumber = 5, instruction = ILoad 1},
BtcLine {lineNumber = 6, instruction = IfGt 20},
BtcLine {lineNumber = 9, instruction = ILoad 2},
BtcLine {lineNumber = 10, instruction = ILoad 3},
BtcLine {lineNumber = 11, instruction = IAdd},
BtcLine {lineNumber = 12, instruction = IStore 2},
BtcLine {lineNumber = 13, instruction = ILoad 3},
BtcLine {lineNumber = 14, instruction = IConst 1},
BtcLine {lineNumber = 15, instruction = IAdd},
BtcLine {lineNumber = 16, instruction = IStore 3},
BtcLine {lineNumber = 17, instruction = Goto 4},
BtcLine {lineNumber = 20, instruction = ILoad 2},
BtcLine {lineNumber = 21, instruction = IReturn}
]
),
( "predicate",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 1},
BtcLine {lineNumber = 1, instruction = BIPush 97},
BtcLine {lineNumber = 2, instruction = IfEq 10},
BtcLine {lineNumber = 5, instruction = ILoad 1},
BtcLine {lineNumber = 6, instruction = BIPush 98},
BtcLine {lineNumber = 7, instruction = IfEq 10},
BtcLine {lineNumber = 10, instruction = IConst 0},
BtcLine {lineNumber = 11, instruction = IReturn},
BtcLine {lineNumber = 12, instruction = IConst 1},
BtcLine {lineNumber = 13, instruction = IReturn}
]
)
]
@@ -0,0 +1,53 @@
module Testsuite.AbcFiles.CombinedControlTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[ ( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 1},
BtcLine {lineNumber = 4, instruction = ALoad0},
BtcLine {lineNumber = 5, instruction = ILoad 1},
BtcLine {lineNumber = 6, instruction = PutField 2},
BtcLine {lineNumber = 9, instruction = Return}
]
),
( "compute",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = IConst 0},
BtcLine {lineNumber = 1, instruction = IStore 1},
BtcLine {lineNumber = 2, instruction = IConst 0},
BtcLine {lineNumber = 3, instruction = IStore 2},
BtcLine {lineNumber = 4, instruction = ILoad 1},
BtcLine {lineNumber = 5, instruction = ALoad0},
BtcLine {lineNumber = 6, instruction = GetField 2},
BtcLine {lineNumber = 9, instruction = IfGe 37},
BtcLine {lineNumber = 12, instruction = ILoad 1},
BtcLine {lineNumber = 13, instruction = IConst 2},
BtcLine {lineNumber = 14, instruction = IDiv},
BtcLine {lineNumber = 15, instruction = IConst 2},
BtcLine {lineNumber = 16, instruction = IMul},
BtcLine {lineNumber = 17, instruction = ILoad 1},
BtcLine {lineNumber = 18, instruction = ISub},
BtcLine {lineNumber = 19, instruction = IfNe 31},
BtcLine {lineNumber = 22, instruction = ILoad 2},
BtcLine {lineNumber = 23, instruction = ILoad 1},
BtcLine {lineNumber = 24, instruction = IAdd},
BtcLine {lineNumber = 25, instruction = IStore 2},
BtcLine {lineNumber = 26, instruction = Goto 35},
BtcLine {lineNumber = 29, instruction = ILoad 2},
BtcLine {lineNumber = 30, instruction = ILoad 1},
BtcLine {lineNumber = 31, instruction = ISub},
BtcLine {lineNumber = 32, instruction = IStore 2},
BtcLine {lineNumber = 35, instruction = ILoad 1},
BtcLine {lineNumber = 36, instruction = IConst 1},
BtcLine {lineNumber = 37, instruction = IAdd},
BtcLine {lineNumber = 38, instruction = IStore 1},
BtcLine {lineNumber = 39, instruction = Goto 4},
BtcLine {lineNumber = 42, instruction = ILoad 2},
BtcLine {lineNumber = 43, instruction = IReturn}
]
)
]
@@ -0,0 +1,34 @@
module Testsuite.AbcFiles.ExpressionTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "shortCircuit",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 1},
BtcLine {lineNumber = 1, instruction = IConst 0},
BtcLine {lineNumber = 2, instruction = IfEq 15},
BtcLine {lineNumber = 5, instruction = BIPush 10},
BtcLine {lineNumber = 6, instruction = ILoad 1},
BtcLine {lineNumber = 7, instruction = IDiv},
BtcLine {lineNumber = 8, instruction = ILoad 2},
BtcLine {lineNumber = 9, instruction = IfLe 15},
BtcLine {lineNumber = 12, instruction = IConst 1},
BtcLine {lineNumber = 13, instruction = Goto 16},
BtcLine {lineNumber = 15, instruction = IConst 0},
BtcLine {lineNumber = 16, instruction = IStore 3},
BtcLine {lineNumber = 17, instruction = ILoad 3},
BtcLine {lineNumber = 18, instruction = IReturn}
]
),
( "charArithmetic",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 1},
BtcLine {lineNumber = 1, instruction = IStore 3},
BtcLine {lineNumber = 2, instruction = ILoad 3},
BtcLine {lineNumber = 3, instruction = IReturn}
]
)
]
+36 -14
View File
@@ -5,23 +5,45 @@ import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 8},
BtcLine {lineNumber = 4, instruction = Return}
]
),
( "ifElseTest",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 0},
BtcLine {lineNumber = 1, instruction = IfGe 6},
BtcLine {lineNumber = 4, instruction = IConst 0},
BtcLine {lineNumber = 5, instruction = IReturn},
BtcLine {lineNumber = 6, instruction = ILoad 0},
BtcLine {lineNumber = 7, instruction = IfNe 14},
BtcLine {lineNumber = 10, instruction = IConst 1},
BtcLine {lineNumber = 11, instruction = IReturn},
BtcLine {lineNumber = 12, instruction = ILoad 0},
BtcLine {lineNumber = 13, instruction = IConst 10},
BtcLine {lineNumber = 14, instruction = IfGt 18},
BtcLine {lineNumber = 17, instruction = IConst 0},
BtcLine {lineNumber = 1, instruction = IConst 0},
BtcLine {lineNumber = 2, instruction = ISub},
BtcLine {lineNumber = 3, instruction = IfGe 8},
BtcLine {lineNumber = 6, instruction = IConst 0},
BtcLine {lineNumber = 7, instruction = IReturn},
BtcLine {lineNumber = 8, instruction = Goto 27},
BtcLine {lineNumber = 11, instruction = ILoad 0},
BtcLine {lineNumber = 12, instruction = IConst 0},
BtcLine {lineNumber = 13, instruction = ISub},
BtcLine {lineNumber = 14, instruction = IfNe 8},
BtcLine {lineNumber = 17, instruction = IConst 1},
BtcLine {lineNumber = 18, instruction = IReturn},
BtcLine {lineNumber = 19, instruction = IConst 1},
BtcLine {lineNumber = 20, instruction = IReturn}
BtcLine {lineNumber = 19, instruction = Goto 16},
BtcLine {lineNumber = 22, instruction = ILoad 0},
BtcLine {lineNumber = 23, instruction = BIPush 10},
BtcLine {lineNumber = 25, instruction = ISub},
BtcLine {lineNumber = 26, instruction = IfGt 7},
BtcLine {lineNumber = 29, instruction = IConst 0},
BtcLine {lineNumber = 30, instruction = Goto 4},
BtcLine {lineNumber = 33, instruction = IConst 1},
BtcLine {lineNumber = 34, instruction = IReturn}
]
)
]
]
+52
View File
@@ -0,0 +1,52 @@
module Testsuite.AbcFiles.LoopTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[ ( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 1},
BtcLine {lineNumber = 4, instruction = Return}
]
),
( "factorial",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = IConst 1},
BtcLine {lineNumber = 1, instruction = IStore 2},
BtcLine {lineNumber = 2, instruction = IConst 1},
BtcLine {lineNumber = 3, instruction = IStore 3},
BtcLine {lineNumber = 4, instruction = ILoad 3},
BtcLine {lineNumber = 5, instruction = ILoad 1},
BtcLine {lineNumber = 6, instruction = IfGt 21},
BtcLine {lineNumber = 9, instruction = ILoad 2},
BtcLine {lineNumber = 10, instruction = ILoad 3},
BtcLine {lineNumber = 11, instruction = IMul},
BtcLine {lineNumber = 12, instruction = IStore 2},
BtcLine {lineNumber = 13, instruction = ILoad 3},
BtcLine {lineNumber = 14, instruction = IConst 1},
BtcLine {lineNumber = 15, instruction = IAdd},
BtcLine {lineNumber = 16, instruction = IStore 3},
BtcLine {lineNumber = 17, instruction = Goto 4},
BtcLine {lineNumber = 20, instruction = ILoad 2},
BtcLine {lineNumber = 21, instruction = IReturn}
]
),
( "weirdFor",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = IConst 0},
BtcLine {lineNumber = 1, instruction = IStore 1},
BtcLine {lineNumber = 2, instruction = ILoad 1},
BtcLine {lineNumber = 3, instruction = IConst 5},
BtcLine {lineNumber = 4, instruction = IfGe 15},
BtcLine {lineNumber = 7, instruction = ILoad 1},
BtcLine {lineNumber = 8, instruction = IConst 1},
BtcLine {lineNumber = 9, instruction = IAdd},
BtcLine {lineNumber = 10, instruction = IStore 1},
BtcLine {lineNumber = 11, instruction = Goto 2},
BtcLine {lineNumber = 14, instruction = ILoad 1},
BtcLine {lineNumber = 15, instruction = IReturn}
]
)
]
@@ -0,0 +1,66 @@
module Testsuite.AbcFiles.MaliciousTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[ ( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 1},
BtcLine {lineNumber = 4, instruction = Return}
]
),
( "assignNegativeIncrement",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 1},
BtcLine {lineNumber = 1, instruction = IConst 1},
BtcLine {lineNumber = 2, instruction = IAdd},
BtcLine {lineNumber = 3, instruction = ISub}, -- Using subtraction patterns for Unary Negative emulation
BtcLine {lineNumber = 4, instruction = IConst 1},
BtcLine {lineNumber = 5, instruction = IAdd},
BtcLine {lineNumber = 6, instruction = IStore 1},
BtcLine {lineNumber = 7, instruction = ILoad 1},
BtcLine {lineNumber = 8, instruction = IReturn}
]
),
( "tripleAddition",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 1},
BtcLine {lineNumber = 1, instruction = ILoad 2},
BtcLine {lineNumber = 2, instruction = ILoad 3},
BtcLine {lineNumber = 3, instruction = IAdd},
BtcLine {lineNumber = 4, instruction = IAdd},
BtcLine {lineNumber = 5, instruction = IStore 4},
BtcLine {lineNumber = 6, instruction = ILoad 1},
BtcLine {lineNumber = 7, instruction = IConst 1},
BtcLine {lineNumber = 8, instruction = IAdd},
BtcLine {lineNumber = 9, instruction = IStore 1},
BtcLine {lineNumber = 10, instruction = ILoad 2},
BtcLine {lineNumber = 11, instruction = IConst 1},
BtcLine {lineNumber = 12, instruction = IAdd},
BtcLine {lineNumber = 13, instruction = IStore 2},
BtcLine {lineNumber = 14, instruction = ILoad 3},
BtcLine {lineNumber = 15, instruction = IConst 1},
BtcLine {lineNumber = 16, instruction = IAdd},
BtcLine {lineNumber = 17, instruction = IStore 3},
BtcLine {lineNumber = 18, instruction = ILoad 4},
BtcLine {lineNumber = 19, instruction = IReturn}
]
),
( "cursedFormatting",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ILoad 1},
BtcLine {lineNumber = 1, instruction = IfNe 7},
BtcLine {lineNumber = 4, instruction = IConst 0},
BtcLine {lineNumber = 5, instruction = IReturn},
BtcLine {lineNumber = 7, instruction = ILoad 1},
BtcLine {lineNumber = 8, instruction = IConst 1},
BtcLine {lineNumber = 9, instruction = IfNe 15},
BtcLine {lineNumber = 12, instruction = IConst 1},
BtcLine {lineNumber = 13, instruction = IReturn},
BtcLine {lineNumber = 15, instruction = IConst 2},
BtcLine {lineNumber = 16, instruction = IReturn}
]
)
]
@@ -0,0 +1,22 @@
module Testsuite.AbcFiles.MethodOverloadTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "MethodOverload",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = BIPush 42},
BtcLine {lineNumber = 1, instruction = IReturn}
]
),
( "MethodOverload",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = BIPush 42},
BtcLine {lineNumber = 1, instruction = ILoad 0},
BtcLine {lineNumber = 2, instruction = IAdd},
BtcLine {lineNumber = 3, instruction = IReturn}
]
)
]
@@ -0,0 +1,28 @@
module Testsuite.AbcFiles.MultipleClassesTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 1},
BtcLine {lineNumber = 4, instruction = ALoad0},
BtcLine {lineNumber = 5, instruction = InvokeSpecial 2},
BtcLine {lineNumber = 8, instruction = PutField 3},
BtcLine {lineNumber = 11, instruction = Return}
]
),
( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 1},
BtcLine {lineNumber = 4, instruction = ALoad0},
BtcLine {lineNumber = 5, instruction = BIPush 42},
BtcLine {lineNumber = 6, instruction = PutField 4},
BtcLine {lineNumber = 9, instruction = Return}
]
)
]
+9 -2
View File
@@ -5,10 +5,17 @@ import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 8},
BtcLine {lineNumber = 4, instruction = Return}
]
),
( "main",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = BIPush 42},
BtcLine {lineNumber = 1, instruction = IReturn}
BtcLine {lineNumber = 2, instruction = IReturn}
]
)
]
]
@@ -0,0 +1,38 @@
module Testsuite.AbcFiles.ShenaniganceTestABC (expectedABC) where
import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[ ( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 1},
BtcLine {lineNumber = 4, instruction = Return}
]
),
( "testAssignment",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = IConst 1},
BtcLine {lineNumber = 1, instruction = IStore 1},
BtcLine {lineNumber = 2, instruction = IConst 5},
BtcLine {lineNumber = 3, instruction = IStore 1},
BtcLine {lineNumber = 4, instruction = ILoad 1},
BtcLine {lineNumber = 5, instruction = IStore 2},
BtcLine {lineNumber = 6, instruction = ILoad 2},
BtcLine {lineNumber = 7, instruction = IReturn}
]
),
( "divEqual",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = Ldc 2},
BtcLine {lineNumber = 2, instruction = IStore 1},
BtcLine {lineNumber = 3, instruction = ILoad 1},
BtcLine {lineNumber = 4, instruction = IConst 4},
BtcLine {lineNumber = 5, instruction = IDiv},
BtcLine {lineNumber = 6, instruction = IStore 1},
BtcLine {lineNumber = 7, instruction = ILoad 1},
BtcLine {lineNumber = 8, instruction = IReturn}
]
)
]
+26 -13
View File
@@ -5,23 +5,36 @@ import Codegen.Lowerer (BtcLine (..), BtcProgram (..), Instruction (..))
expectedABC :: [(String, BtcProgram)]
expectedABC =
[
( "<init>",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = ALoad0},
BtcLine {lineNumber = 1, instruction = InvokeSpecial 8},
BtcLine {lineNumber = 4, instruction = Return}
]
),
( "whileLoopTest",
BtcProgram
[ BtcLine {lineNumber = 0, instruction = IConst 0},
BtcLine {lineNumber = 1, instruction = IStore 1},
BtcLine {lineNumber = 2, instruction = ILoad 0},
BtcLine {lineNumber = 3, instruction = IfLe 16},
BtcLine {lineNumber = 6, instruction = ILoad 1},
BtcLine {lineNumber = 7, instruction = ILoad 0},
BtcLine {lineNumber = 8, instruction = IAdd},
BtcLine {lineNumber = 9, instruction = IStore 1},
BtcLine {lineNumber = 10, instruction = ILoad 0},
BtcLine {lineNumber = 11, instruction = IConst 1},
BtcLine {lineNumber = 12, instruction = ISub},
BtcLine {lineNumber = 13, instruction = IStore 0},
BtcLine {lineNumber = 14, instruction = Goto 2},
BtcLine {lineNumber = 17, instruction = ILoad 1},
BtcLine {lineNumber = 18, instruction = IReturn}
BtcLine {lineNumber = 3, instruction = IConst 0},
BtcLine {lineNumber = 4, instruction = ISub},
BtcLine {lineNumber = 5, instruction = IfLe 14},
BtcLine {lineNumber = 8, instruction = ILoad 1},
BtcLine {lineNumber = 9, instruction = ILoad 0},
BtcLine {lineNumber = 10, instruction = IAdd},
BtcLine {lineNumber = 11, instruction = IStore 1},
BtcLine {lineNumber = 12, instruction = ILoad 0},
BtcLine {lineNumber = 13, instruction = IConst 1},
BtcLine {lineNumber = 14, instruction = ISub},
BtcLine {lineNumber = 15, instruction = IStore 0},
BtcLine {lineNumber = 16, instruction = Goto 65522},
BtcLine {lineNumber = 19, instruction = ILoad 1},
BtcLine {lineNumber = 20, instruction = IReturn}
]
)
]
]
@@ -0,0 +1,76 @@
module Testsuite.AstFiles.AllSyntaxTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"AllSyntaxTest"
[ Field "int" "x" Nothing
, Field "int" "counter" Nothing
]
[ Method
"void"
"<init>"
[("int", "initial")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "x") (LocalOrFieldVar "initial"))
, StmtExprStmt (Assign (LocalOrFieldVar "counter") (Integer 0))
, StmtExprStmt (Assign (LocalOrFieldVar "counter") (Binary Add (LocalOrFieldVar "counter") (Integer 1)))
]
)
, Method
"int"
"inc"
[]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "x") (Binary Add (LocalOrFieldVar "x") (Integer 1)))
, Return (Just (LocalOrFieldVar "x"))
]
)
, Method
"int"
"inc"
[("int", "delta")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "x") (Binary Add (LocalOrFieldVar "x") (LocalOrFieldVar "delta")))
, Return (Just (LocalOrFieldVar "x"))
]
)
, Method
"int"
"sumUpTo"
[("int", "n")]
( Block
[ LocalVarDecl "int" "s" (Just (Integer 0))
, Block
[ LocalVarDecl "int" "i" (Just (Integer 1))
, While
(Binary CompLessOrEqual (LocalOrFieldVar "i") (LocalOrFieldVar "n"))
( Block
[ Block
[ StmtExprStmt (Assign (LocalOrFieldVar "s") (Binary Add (LocalOrFieldVar "s") (LocalOrFieldVar "i")))
]
, StmtExprStmt (Assign (LocalOrFieldVar "i") (Binary Add (LocalOrFieldVar "i") (Integer 1)))
]
)
]
, Return (Just (LocalOrFieldVar "s"))
]
)
, Method
"boolean"
"predicate"
[("char", "c")]
( Block
[ Return
( Just
( Binary
Or
(Binary CompEqual (LocalOrFieldVar "c") (Char 'a'))
(Binary CompEqual (LocalOrFieldVar "c") (Char 'b'))
)
)
]
)
]
]
@@ -0,0 +1,39 @@
module Testsuite.AstFiles.CombinedControlTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"CombinedControlTest"
[ Field "int" "field" Nothing
]
[ Method
"void"
"<init>"
[("int", "v")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "field") (LocalOrFieldVar "v"))
]
)
, Method
"int"
"compute"
[]
( Block
[ LocalVarDecl "int" "i" (Just (Integer 0))
, LocalVarDecl "int" "acc" (Just (Integer 0))
, While
(Binary CompLessThan (LocalOrFieldVar "i") (LocalOrFieldVar "field"))
( Block
[ If
(Binary CompEqual (Binary Modulo (LocalOrFieldVar "i") (Integer 2)) (Integer 0))
(Block [StmtExprStmt (Assign (LocalOrFieldVar "acc") (Binary Add (LocalOrFieldVar "acc") (LocalOrFieldVar "i")))])
(Just (Block [StmtExprStmt (Assign (LocalOrFieldVar "acc") (Binary Subtract (LocalOrFieldVar "acc") (LocalOrFieldVar "i")))]))
, StmtExprStmt (Assign (LocalOrFieldVar "i") (Binary Add (LocalOrFieldVar "i") (Integer 1)))
]
)
, Return (Just (LocalOrFieldVar "acc"))
]
)
]
]
+1 -1
View File
@@ -5,7 +5,7 @@ import Grammar.AST
expectedAST =
[ Class
"ConstructorTest"
[Field "int" "a" (Just (Integer (-1)))]
[Field "int" "a" (Just (Unary Negate (Integer 1)))]
[ Method
"void"
"<init>"
@@ -0,0 +1,28 @@
module Testsuite.AstFiles.ExpressionTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"ExpressionTest"
[]
[ Method
"boolean"
"shortCircuit"
[("int", "a"), ("int", "b")]
( Block
[ LocalVarDecl "boolean" "res" (Just (Binary And (Binary CompNotEqual (LocalOrFieldVar "a") (Integer 0)) (Binary CompGreaterThan (Binary Divide (Integer 10) (LocalOrFieldVar "a")) (LocalOrFieldVar "b"))))
, Return (Just (LocalOrFieldVar "res"))
]
)
, Method
"char"
"charArithmetic"
[("char", "c"), ("int", "offset")]
( Block
[ LocalVarDecl "char" "d" (Just (LocalOrFieldVar "c"))
, Return (Just (LocalOrFieldVar "d"))
]
)
]
]
+50
View File
@@ -0,0 +1,50 @@
module Testsuite.AstFiles.LoopTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"LoopTest"
[]
[ Method
"int"
"factorial"
[("int", "n")]
( Block
[ LocalVarDecl "int" "tally" (Just (Integer 1))
, Block
[ LocalVarDecl "int" "i" (Just (Integer 1))
, While
(Binary CompLessOrEqual (LocalOrFieldVar "i") (LocalOrFieldVar "n"))
( Block
[ Block
[ StmtExprStmt (Assign (LocalOrFieldVar "tally") (Binary Multiply (LocalOrFieldVar "tally") (LocalOrFieldVar "i")))
]
, StmtExprStmt (Assign (LocalOrFieldVar "i") (Binary Add (LocalOrFieldVar "i") (Integer 1)))
]
)
]
, Return (Just (LocalOrFieldVar "tally"))
]
)
, Method
"int"
"weirdFor"
[]
( Block
[ LocalVarDecl "int" "k" (Just (Integer 0))
, Block
[ EmptyStmt
, While
(Binary CompLessThan (LocalOrFieldVar "k") (Integer 5))
( Block
[ Block []
, StmtExprStmt (Assign (LocalOrFieldVar "k") (Binary Add (LocalOrFieldVar "k") (Integer 1)))
]
)
]
, Return (Just (LocalOrFieldVar "k"))
]
)
]
]
@@ -0,0 +1,27 @@
module Testsuite.AstFiles.MaliciousTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"MaliciousTest"
[]
[ Method
"int"
"cursedFormatting"
[("int", "n")]
( Block
[ If
(Binary CompEqual (LocalOrFieldVar "n") (Integer 0))
(Block [Return (Just (Integer 0))])
( Just
( If
(Binary CompEqual (LocalOrFieldVar "n") (Integer 1))
(Block [Return (Just (Integer 1))])
(Just (Block [Return (Just (Integer 2))]))
)
)
]
)
]
]
@@ -0,0 +1,20 @@
module Testsuite.AstFiles.MethodOverloadTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"MethodOverloadTest"
[]
[ Method
"int"
"MethodOverload"
[]
(Block [Return (Just (Integer 42))]),
Method
"int"
"MethodOverload"
[("int", "a")]
(Block [Return (Just (Binary Add (Integer 42) (LocalOrFieldVar "a")))])
]
]
@@ -0,0 +1,16 @@
module Testsuite.AstFiles.MultipleClassesTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"MultipleClassesTest"
[ Field "AnotherTestClass" "a" (Just (StmtExprExpr (New "AnotherTestClass" [])))
]
[]
, Class
"AnotherTestClass"
[ Field "int" "a" (Just (Integer 42))
]
[]
]
+4 -4
View File
@@ -5,15 +5,15 @@ import Grammar.AST
expectedAST =
[ Class
"RecursionTest"
[ Field "int" "value" (Just (Integer 0))
, Field "RecursionTest" "child" (Just Null)
[ Field "int" "value" (Just (Integer 0))
, Field "RecursionTest" "child" (Just Null)
]
[ Method
"void"
"<init>"
[("int", "n")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "value") (LocalOrFieldVar "n"))
[ StmtExprStmt (Assign (InstVar This "value") (LocalOrFieldVar "n"))
, If
(Binary CompGreaterThan (LocalOrFieldVar "n") (Integer 0))
( Block
@@ -30,7 +30,7 @@ expectedAST =
( Block
[ If
(Binary CompLessThan (LocalOrFieldVar "n") (Integer 2))
(Return (Just (LocalOrFieldVar "n")))
(Block [Return (Just (LocalOrFieldVar "n"))])
( Just
( Block
[ Return
@@ -0,0 +1,30 @@
module Testsuite.AstFiles.ShenaniganceTestAST (expectedAST) where
import Grammar.AST
expectedAST =
[ Class
"ShenaniganceTest"
[]
[ Method
"int"
"testAssignment"
[]
( Block
[ LocalVarDecl "int" "x" (Just (Integer 1))
, LocalVarDecl "int" "y" (Just (StmtExprExpr (Assign (LocalOrFieldVar "x") (Integer 5))))
, Return (Just (LocalOrFieldVar "y"))
]
)
, Method
"int"
"divEqual"
[]
( Block
[ LocalVarDecl "int" "x" (Just (Integer 234343000))
, StmtExprStmt (Assign (LocalOrFieldVar "x") (Binary Divide (LocalOrFieldVar "x") (Integer 4)))
, Return (Just (LocalOrFieldVar "x"))
]
)
]
]
+1 -1
View File
@@ -11,7 +11,7 @@ expectedAST =
"whileLoopTest"
[("int", "n")]
( Block
[ LocalVarDecl "int" "sum" Nothing,
[ LocalVarDecl "int" "sum" (Just (Integer 0)),
While
(Binary CompGreaterThan (LocalOrFieldVar "n") (Integer 0))
( Block
+262 -50
View File
@@ -1,31 +1,102 @@
module Testsuite.ExecutableTests.Registry where
import Testsuite.ExecutableTests.TestCase
import qualified Testsuite.AstFiles.EmptyTestAST as EmptyAST
import qualified Testsuite.TastFiles.EmptyTestTAST as EmptyTAST
import qualified Testsuite.AbcFiles.EmptyTestABC as EmptyABC
-- AllSyntaxTest
import qualified Testsuite.AstFiles.AllSyntaxTestAST as AllSyntaxAST
import qualified Testsuite.TastFiles.AllSyntaxTestTAST as AllSyntaxTAST
import qualified Testsuite.AbcFiles.AllSyntaxTestABC as AllSyntaxABC
-- ArithmeticTest
import qualified Testsuite.AstFiles.ArithmeticTestAST as ArithmeticAST
import qualified Testsuite.TastFiles.ArithmeticTestTAST as ArithmeticTAST
import qualified Testsuite.AbcFiles.ArithmeticTestABC as ArithmeticABC
import qualified Testsuite.AstFiles.FieldsTestAST as FieldsAST
import qualified Testsuite.TastFiles.FieldsTestTAST as FieldsTAST
import qualified Testsuite.AbcFiles.FieldsTestABC as FieldsABC
import qualified Testsuite.AstFiles.IfTestAST as IfAST
import qualified Testsuite.TastFiles.IfTestTAST as IfTAST
import qualified Testsuite.AbcFiles.IfTestABC as IfABC
import qualified Testsuite.AstFiles.WhileTestAST as WhileAST
import qualified Testsuite.TastFiles.WhileTestTAST as WhileTAST
import qualified Testsuite.AbcFiles.WhileTestABC as WhileABC
import qualified Testsuite.AstFiles.ConstructorTestAST as ConstructorAST
import qualified Testsuite.TastFiles.ConstructorTestTAST as ConstructorTAST
import qualified Testsuite.AbcFiles.ConstructorTestABC as ConstructorABC
-- CombinedControlTest
import qualified Testsuite.AstFiles.CombinedControlTestAST as CombinedControlAST
import qualified Testsuite.TastFiles.CombinedControlTestTAST as CombinedControlTAST
import qualified Testsuite.AbcFiles.CombinedControlTestABC as CombinedControlABC
-- ConstructorOverloadTest
import qualified Testsuite.AstFiles.ConstructorOverloadTestAST as ConstructorOverloadAST
import qualified Testsuite.TastFiles.ConstructorOverloadTestTAST as ConstructorOverloadTAST
import qualified Testsuite.AbcFiles.ConstructorOverloadTestABC as ConstructorOverloadABC
-- ConstructorTest
import qualified Testsuite.AstFiles.ConstructorTestAST as ConstructorAST
import qualified Testsuite.TastFiles.ConstructorTestTAST as ConstructorTAST
import qualified Testsuite.AbcFiles.ConstructorTestABC as ConstructorABC
-- EmptyTest
import qualified Testsuite.AstFiles.EmptyTestAST as EmptyAST
import qualified Testsuite.TastFiles.EmptyTestTAST as EmptyTAST
import qualified Testsuite.AbcFiles.EmptyTestABC as EmptyABC
-- ExpressionTest
import qualified Testsuite.AstFiles.ExpressionTestAST as ExpressionAST
import qualified Testsuite.TastFiles.ExpressionTestTAST as ExpressionTAST
import qualified Testsuite.AbcFiles.ExpressionTestABC as ExpressionABC
-- FieldsTest
import qualified Testsuite.AstFiles.FieldsTestAST as FieldsAST
import qualified Testsuite.TastFiles.FieldsTestTAST as FieldsTAST
import qualified Testsuite.AbcFiles.FieldsTestABC as FieldsABC
-- IfTest
import qualified Testsuite.AstFiles.IfTestAST as IfAST
import qualified Testsuite.TastFiles.IfTestTAST as IfTAST
import qualified Testsuite.AbcFiles.IfTestABC as IfABC
-- LoopTest
import qualified Testsuite.AstFiles.LoopTestAST as LoopAST
import qualified Testsuite.TastFiles.LoopTestTAST as LoopTAST
import qualified Testsuite.AbcFiles.LoopTestABC as LoopABC
-- MaliciousTest
import qualified Testsuite.AstFiles.MaliciousTestAST as MaliciousAST
import qualified Testsuite.TastFiles.MaliciousTestTAST as MaliciousTAST
import qualified Testsuite.AbcFiles.MaliciousTestABC as MaliciousABC
-- MethodOverloadTest
import qualified Testsuite.AstFiles.MethodOverloadTestAST as MethodOverloadAST
import qualified Testsuite.TastFiles.MethodOverloadTestTAST as MethodOverloadTAST
import qualified Testsuite.AbcFiles.MethodOverloadTestABC as MethodOverloadABC
-- MultiClassTest
import qualified Testsuite.AstFiles.MultiClassTestAST as MultiClassAST
import qualified Testsuite.TastFiles.MultiClassTestTAST as MultiClassTAST
import qualified Testsuite.AbcFiles.MultiClassTestABC as MultiClassABC
-- MultipleClassesTest
import qualified Testsuite.AstFiles.MultipleClassesTestAST as MultipleClassesAST
import qualified Testsuite.TastFiles.MultipleClassesTestTAST as MultipleClassesTAST
import qualified Testsuite.AbcFiles.MultipleClassesTestABC as MultipleClassesABC
-- RecursionTest
import qualified Testsuite.AstFiles.RecursionTestAST as RecursionAST
import qualified Testsuite.TastFiles.RecursionTestTAST as RecursionTAST
import qualified Testsuite.AbcFiles.RecursionTestABC as RecursionABC
-- ReturnTest
import qualified Testsuite.AstFiles.ReturnTestAST as ReturnAST
import qualified Testsuite.TastFiles.ReturnTestTAST as ReturnTAST
import qualified Testsuite.AbcFiles.ReturnTestABC as ReturnABC
-- ShenaniganceTest
import qualified Testsuite.AstFiles.ShenaniganceTestAST as ShenaniganceAST
import qualified Testsuite.TastFiles.ShenaniganceTestTAST as ShenaniganceTAST
import qualified Testsuite.AbcFiles.ShenaniganceTestABC as ShenaniganceABC
-- SingletonTest
import qualified Testsuite.AstFiles.SingletonTestAST as SingletonAST
import qualified Testsuite.TastFiles.SingletonTestTAST as SingletonTAST
import qualified Testsuite.AbcFiles.SingletonTestABC as SingletonABC
-- WhileTest
import qualified Testsuite.AstFiles.WhileTestAST as WhileAST
import qualified Testsuite.TastFiles.WhileTestTAST as WhileTAST
import qualified Testsuite.AbcFiles.WhileTestABC as WhileABC
import Codegen.Serializer (serializeProgram)
import Data.Word (Word8)
import qualified Codegen.Lowerer
@@ -33,6 +104,84 @@ import qualified Codegen.Lowerer
toBytecode :: [(String, Codegen.Lowerer.BtcProgram)] -> [(String, [Word8])]
toBytecode = map (fmap (serializeProgram))
allTests :: [TestCase]
allTests =
[ allSyntaxTest
, arithmeticTest
, combinedControlTest
, constructorOverloadTest
, constructorTest
, emptyTest
, expressionTest
, fieldsTest
, ifTest
, loopTest
, maliciousTest
, methodOverloadTest
, multiClassTest
, multipleClassesTest
, recursionTest
, returnTest
, shenaniganceTest
, singletonTest
, whileTest
]
allSyntaxTest :: TestCase
allSyntaxTest =
TestCase
{ tcName = "AllSyntaxTest"
, tcJavaFile = "src/Testsuite/javaFiles/AllSyntaxTest.java"
, tcExpectedAST = AllSyntaxAST.expectedAST
, tcExpectedTAST = AllSyntaxTAST.expectedTAST
, tcExpectedABC = AllSyntaxABC.expectedABC
, tcExpectedBytecode = toBytecode AllSyntaxABC.expectedABC
}
arithmeticTest :: TestCase
arithmeticTest =
TestCase
{ tcName = "ArithmeticTest"
, tcJavaFile = "src/Testsuite/javaFiles/ArithmeticTest.java"
, tcExpectedAST = ArithmeticAST.expectedAST
, tcExpectedTAST = ArithmeticTAST.expectedTAST
, tcExpectedABC = ArithmeticABC.expectedABC
, tcExpectedBytecode = toBytecode ArithmeticABC.expectedABC
}
combinedControlTest :: TestCase
combinedControlTest =
TestCase
{ tcName = "CombinedControlTest"
, tcJavaFile = "src/Testsuite/javaFiles/CombinedControlTest.java"
, tcExpectedAST = CombinedControlAST.expectedAST
, tcExpectedTAST = CombinedControlTAST.expectedTAST
, tcExpectedABC = CombinedControlABC.expectedABC
, tcExpectedBytecode = toBytecode CombinedControlABC.expectedABC
}
constructorOverloadTest :: TestCase
constructorOverloadTest =
TestCase
{ tcName = "ConstructorOverloadTest"
, tcJavaFile = "src/Testsuite/javaFiles/ConstructorOverloadTest.java"
, tcExpectedAST = ConstructorOverloadAST.expectedAST
, tcExpectedTAST = ConstructorOverloadTAST.expectedTAST
, tcExpectedABC = ConstructorOverloadABC.expectedABC
, tcExpectedBytecode = toBytecode ConstructorOverloadABC.expectedABC
}
constructorTest :: TestCase
constructorTest =
TestCase
{ tcName = "ConstructorTest"
, tcJavaFile = "src/Testsuite/javaFiles/ConstructorTest.java"
, tcExpectedAST = ConstructorAST.expectedAST
, tcExpectedTAST = ConstructorTAST.expectedTAST
, tcExpectedABC = ConstructorABC.expectedABC
, tcExpectedBytecode = toBytecode ConstructorABC.expectedABC
}
emptyTest :: TestCase
emptyTest =
TestCase
@@ -44,15 +193,15 @@ emptyTest =
, tcExpectedBytecode = toBytecode EmptyABC.expectedABC
}
arithmeticTest :: TestCase
arithmeticTest =
expressionTest :: TestCase
expressionTest =
TestCase
{ tcName = "ArithmeticTest"
, tcJavaFile = "src/Testsuite/javaFiles/ArithmeticTest.java"
, tcExpectedAST = ArithmeticAST.expectedAST
, tcExpectedTAST = ArithmeticTAST.expectedTAST
, tcExpectedABC = ArithmeticABC.expectedABC
, tcExpectedBytecode = toBytecode ArithmeticABC.expectedABC
{ tcName = "ExpressionTest"
, tcJavaFile = "src/Testsuite/javaFiles/ExpressionTest.java"
, tcExpectedAST = ExpressionAST.expectedAST
, tcExpectedTAST = ExpressionTAST.expectedTAST
, tcExpectedABC = ExpressionABC.expectedABC
, tcExpectedBytecode = toBytecode ExpressionABC.expectedABC
}
fieldsTest :: TestCase
@@ -77,40 +226,37 @@ ifTest =
, tcExpectedBytecode = toBytecode IfABC.expectedABC
}
whileTest :: TestCase
whileTest =
loopTest :: TestCase
loopTest =
TestCase
{ tcName = "WhileTest"
, tcJavaFile = "src/Testsuite/javaFiles/WhileTest.java"
, tcExpectedAST = WhileAST.expectedAST
, tcExpectedTAST = WhileTAST.expectedTAST
, tcExpectedABC = WhileABC.expectedABC
, tcExpectedBytecode = toBytecode WhileABC.expectedABC
{ tcName = "LoopTest"
, tcJavaFile = "src/Testsuite/javaFiles/LoopTest.java"
, tcExpectedAST = LoopAST.expectedAST
, tcExpectedTAST = LoopTAST.expectedTAST
, tcExpectedABC = LoopABC.expectedABC
, tcExpectedBytecode = toBytecode LoopABC.expectedABC
}
allTests :: [TestCase]
allTests = [emptyTest, arithmeticTest, fieldsTest, ifTest, whileTest, constructorTest, constructorOverloadTest, multiClassTest]
constructorTest :: TestCase
constructorTest =
maliciousTest :: TestCase
maliciousTest =
TestCase
{ tcName = "ConstructorTest"
, tcJavaFile = "src/Testsuite/javaFiles/ConstructorTest.java"
, tcExpectedAST = ConstructorAST.expectedAST
, tcExpectedTAST = ConstructorTAST.expectedTAST
, tcExpectedABC = ConstructorABC.expectedABC
, tcExpectedBytecode = toBytecode ConstructorABC.expectedABC
{ tcName = "MaliciousTest"
, tcJavaFile = "src/Testsuite/javaFiles/MaliciousTest.java"
, tcExpectedAST = MaliciousAST.expectedAST
, tcExpectedTAST = MaliciousTAST.expectedTAST
, tcExpectedABC = MaliciousABC.expectedABC
, tcExpectedBytecode = toBytecode MaliciousABC.expectedABC
}
constructorOverloadTest :: TestCase
constructorOverloadTest =
methodOverloadTest :: TestCase
methodOverloadTest =
TestCase
{ tcName = "ConstructorOverloadTest"
, tcJavaFile = "src/Testsuite/javaFiles/ConstructorOverloadTest.java"
, tcExpectedAST = ConstructorOverloadAST.expectedAST
, tcExpectedTAST = ConstructorOverloadTAST.expectedTAST
, tcExpectedABC = ConstructorOverloadABC.expectedABC
, tcExpectedBytecode = toBytecode ConstructorOverloadABC.expectedABC
{ tcName = "MethodOverloadTest"
, tcJavaFile = "src/Testsuite/javaFiles/MethodOverloadTest.java"
, tcExpectedAST = MethodOverloadAST.expectedAST
, tcExpectedTAST = MethodOverloadTAST.expectedTAST
, tcExpectedABC = MethodOverloadABC.expectedABC
, tcExpectedBytecode = toBytecode MethodOverloadABC.expectedABC
}
multiClassTest :: TestCase
@@ -122,4 +268,70 @@ multiClassTest =
, tcExpectedTAST = MultiClassTAST.expectedTAST
, tcExpectedABC = MultiClassABC.expectedABC
, tcExpectedBytecode = toBytecode MultiClassABC.expectedABC
}
multipleClassesTest :: TestCase
multipleClassesTest =
TestCase
{ tcName = "MultipleClassesTest"
, tcJavaFile = "src/Testsuite/javaFiles/MultipleClassesTest.java"
, tcExpectedAST = MultipleClassesAST.expectedAST
, tcExpectedTAST = MultipleClassesTAST.expectedTAST
, tcExpectedABC = MultipleClassesABC.expectedABC
, tcExpectedBytecode = toBytecode MultipleClassesABC.expectedABC
}
recursionTest :: TestCase
recursionTest =
TestCase
{ tcName = "RecursionTest"
, tcJavaFile = "src/Testsuite/javaFiles/RecursionTest.java"
, tcExpectedAST = RecursionAST.expectedAST
, tcExpectedTAST = RecursionTAST.expectedTAST
, tcExpectedABC = RecursionABC.expectedABC
, tcExpectedBytecode = toBytecode RecursionABC.expectedABC
}
returnTest :: TestCase
returnTest =
TestCase
{ tcName = "ReturnTest"
, tcJavaFile = "src/Testsuite/javaFiles/ReturnTest.java"
, tcExpectedAST = ReturnAST.expectedAST
, tcExpectedTAST = ReturnTAST.expectedTAST
, tcExpectedABC = ReturnABC.expectedABC
, tcExpectedBytecode = toBytecode ReturnABC.expectedABC
}
shenaniganceTest :: TestCase
shenaniganceTest =
TestCase
{ tcName = "ShenaniganceTest"
, tcJavaFile = "src/Testsuite/javaFiles/ShenaniganceTest.java"
, tcExpectedAST = ShenaniganceAST.expectedAST
, tcExpectedTAST = ShenaniganceTAST.expectedTAST
, tcExpectedABC = ShenaniganceABC.expectedABC
, tcExpectedBytecode = toBytecode ShenaniganceABC.expectedABC
}
singletonTest :: TestCase
singletonTest =
TestCase
{ tcName = "SingletonTest"
, tcJavaFile = "src/Testsuite/javaFiles/SingletonTest.java"
, tcExpectedAST = SingletonAST.expectedAST
, tcExpectedTAST = SingletonTAST.expectedTAST
, tcExpectedABC = SingletonABC.expectedABC
, tcExpectedBytecode = toBytecode SingletonABC.expectedABC
}
whileTest :: TestCase
whileTest =
TestCase
{ tcName = "WhileTest"
, tcJavaFile = "src/Testsuite/javaFiles/WhileTest.java"
, tcExpectedAST = WhileAST.expectedAST
, tcExpectedTAST = WhileTAST.expectedTAST
, tcExpectedABC = WhileABC.expectedABC
, tcExpectedBytecode = toBytecode WhileABC.expectedABC
}
+11 -14
View File
@@ -8,17 +8,14 @@ import Testsuite.ExecutableTests.HarnessSupport (TestFailure(..))
runABCTest :: TestCase -> IO ()
runABCTest tc =
case tcExpectedTAST tc of
[t] ->
let actual = lowerClassFile (generateClassFile t)
in if actual == tcExpectedABC tc
then pure ()
else
let body =
tcName tc
++ " ABC mismatch\nActual ABC:\n"
++ show actual
++ "\nExpected ABC:\n"
++ show (tcExpectedABC tc)
in throwIO $ TestFailure body
_ -> error "ABC test expects exactly one class"
let actual = concatMap (lowerClassFile . generateClassFile) (tcExpectedTAST tc)
in if actual == tcExpectedABC tc
then pure ()
else
let body =
tcName tc
++ " ABC mismatch\nActual ABC:\n"
++ show actual
++ "\nExpected ABC:\n"
++ show (tcExpectedABC tc)
in throwIO $ TestFailure body
+2 -2
View File
@@ -16,5 +16,5 @@ runASTTest tc = do
else throwIO $
TestFailure $
tcName tc ++ " AST mismatch\n"
++ "Actual: " ++ show actual ++ "\n"
++ "Expected: " ++ show (tcExpectedAST tc)
++ "Actual AST: " ++ show actual ++ "\n"
++ "Expected AST: " ++ show (tcExpectedAST tc)
@@ -32,5 +32,5 @@ runBytecodeTest tc = do
let formatEntry (n, bs) = n ++ ": " ++ bytesToHex bs
expectedLines = map formatEntry nExpected
actualLines = map formatEntry nActual
body = "bytecode mismatch for " ++ tcName tc ++ "\nExpected:\n" ++ unlines expectedLines ++ "Actual:\n" ++ unlines actualLines
body = "bytecode mismatch for " ++ tcName tc ++ "\nExpected Bytecode:\n" ++ unlines expectedLines ++ "Actual Bytecode:\n" ++ unlines actualLines
in throwIO $ TestFailure body
+4 -4
View File
@@ -7,12 +7,12 @@ import Testsuite.ExecutableTests.HarnessSupport (TestFailure(..))
runTASTTest :: TestCase -> IO ()
runTASTTest tc =
let actual =
map (\c -> typeCheckClass c [] (tcExpectedAST tc))
(tcExpectedAST tc)
let actual = map (\c -> typeCheckClass c [] (tcExpectedAST tc)) (tcExpectedAST tc)
in if actual == tcExpectedTAST tc
then pure ()
else throwIO $
TestFailure $
tcName tc ++ " TAST mismatch\n"
tcName tc ++ " TAST mismatch\n"
++ "Actual TAST: " ++ show actual ++ "\n"
++ "Expected TAST: " ++ show (tcExpectedTAST tc)
+4
View File
@@ -9,3 +9,7 @@ java -cp /path/to/classFiles Main
cd src/Testsuite/classFiles
java -cp . Main
get lexical test errors:
runghc -isrc src/Testsuite/ExecutableTests/Main.hs | grep --line-buffered -i -n -B1 'Lexical' | grep 'RUN'
@@ -0,0 +1,100 @@
module Testsuite.TastFiles.AllSyntaxTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"AllSyntaxTest"
[ Field "int" "x" Nothing
, Field "int" "counter" Nothing
]
[ Method
"void"
"<init>"
[("int", "initial")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "x" "int") (LocalOrFieldVar "initial" "int") "int") "void"
, StmtExprStmt (Assign (LocalOrFieldVar "counter" "int") (Integer 0 "int") "int") "void"
, StmtExprStmt (Assign (LocalOrFieldVar "counter" "int") (Binary Add (LocalOrFieldVar "counter" "int") (Integer 1 "int") "int") "int") "void"
]
"void"
)
, Method
"int"
"inc"
[]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "x" "int") (Binary Add (LocalOrFieldVar "x" "int") (Integer 1 "int") "int") "int") "void"
, Return (Just (LocalOrFieldVar "x" "int")) "int"
]
"int"
)
, Method
"int"
"inc"
[("int", "delta")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "x" "int") (Binary Add (LocalOrFieldVar "x" "int") (LocalOrFieldVar "delta" "int") "int") "int") "void"
, Return (Just (LocalOrFieldVar "x" "int")) "int"
]
"int"
)
, Method
"int"
"sumUpTo"
[("int", "n")]
( Block
[ LocalVarDecl "int" "s" "int"
, Block
[ LocalVarDecl "int" "i" "int"
, While
(Binary CompLessOrEqual (LocalOrFieldVar "i" "int") (LocalOrFieldVar "n" "int") "boolean")
( Block
[ Block
[ StmtExprStmt
(Assign
(LocalOrFieldVar "s" "int")
(Binary Add (LocalOrFieldVar "s" "int") (LocalOrFieldVar "i" "int") "int")
"int"
)
"void"
]
"void"
, StmtExprStmt
(Assign
(LocalOrFieldVar "i" "int")
(Binary Add (LocalOrFieldVar "i" "int") (Integer 1 "int") "int")
"int"
)
"void"
]
"void"
)
"void"
]
"void"
, Return (Just (LocalOrFieldVar "s" "int")) "int"
]
"int"
)
, Method
"boolean"
"predicate"
[("char", "c")]
( Block
[ Return
( Just
( Binary
Or
(Binary CompEqual (LocalOrFieldVar "c" "char") (Char 'a' "char") "boolean")
(Binary CompEqual (LocalOrFieldVar "c" "char") (Char 'b' "char") "boolean")
"boolean"
)
)
"boolean"
]
"boolean"
)
]
]
@@ -0,0 +1,45 @@
module Testsuite.TastFiles.CombinedControlTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"CombinedControlTest"
[ Field "int" "field" Nothing
]
[ Method
"void"
"<init>"
[("int", "v")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "field" "int") (LocalOrFieldVar "v" "int") "int") "void"
]
"void"
)
, Method
"int"
"compute"
[]
( Block
[ LocalVarDecl "int" "i" "int"
, LocalVarDecl "int" "acc" "int"
, While
(Binary CompLessThan (LocalOrFieldVar "i" "int") (LocalOrFieldVar "field" "int") "boolean")
( Block
[ If
(Binary CompEqual (Binary Modulo (LocalOrFieldVar "i" "int") (Integer 2 "int") "int") (Integer 0 "int") "boolean")
(Block [StmtExprStmt (Assign (LocalOrFieldVar "acc" "int") (Binary Add (LocalOrFieldVar "acc" "int") (LocalOrFieldVar "i" "int") "int") "int") "void"] "void")
(Just (Block [StmtExprStmt (Assign (LocalOrFieldVar "acc" "int") (Binary Subtract (LocalOrFieldVar "acc" "int") (LocalOrFieldVar "i" "int") "int") "int") "void"] "void"))
"void"
, StmtExprStmt (Assign (LocalOrFieldVar "i" "int") (Binary Add (LocalOrFieldVar "i" "int") (Integer 1 "int") "int") "int") "void"
]
"void"
)
"void"
, Return (Just (LocalOrFieldVar "acc" "int")) "int"
]
"int"
)
]
]
@@ -16,6 +16,6 @@ expectedTAST =
"void"
"<init>"
[("int", "a")]
(Block [StmtExprStmt (Assign (InstVar (This "ConstructorOverloadTest") "a" "int") (LocalOrFieldVar "a" "int") "void") "void"] "void")
(Block [StmtExprStmt (Assign (InstVar (This "ConstructorOverloadTest") "a" "int") (LocalOrFieldVar "a" "int") "int") "void"] "void")
]
]
@@ -6,11 +6,11 @@ import Grammar.TAST
expectedTAST =
[ Class
"ConstructorTest"
[Field "int" "a" (Just (Integer (-1) "int"))]
[Field "int" "a" (Just (Unary Negate (Integer 1 "int") "int"))]
[ Method
"void"
"<init>"
[("int", "initial_value")]
(Block [StmtExprStmt (Assign (LocalOrFieldVar "a" "int") (LocalOrFieldVar "initial_value" "int") "void") "void"] "void")
(Block [StmtExprStmt (Assign (LocalOrFieldVar "a" "int") (LocalOrFieldVar "initial_value" "int") "int") "void"] "void")
]
]
@@ -0,0 +1,31 @@
module Testsuite.TastFiles.ExpressionTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"ExpressionTest"
[]
[ Method
"boolean"
"shortCircuit"
[("int", "a"), ("int", "b")]
( Block
[ LocalVarDecl "boolean" "res" "boolean"
, Return (Just (LocalOrFieldVar "res" "boolean")) "boolean"
]
"boolean"
)
, Method
"char"
"charArithmetic"
[("char", "c"), ("int", "offset")]
( Block
[ LocalVarDecl "char" "d" "char"
, Return (Just (LocalOrFieldVar "d" "char")) "char"
]
"char"
)
]
]
+78
View File
@@ -0,0 +1,78 @@
module Testsuite.TastFiles.LoopTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"LoopTest"
[]
[ Method
"int"
"factorial"
[("int", "n")]
( Block
[ LocalVarDecl "int" "tally" "int"
, Block
[ LocalVarDecl "int" "i" "int"
, While
(Binary CompLessOrEqual (LocalOrFieldVar "i" "int") (LocalOrFieldVar "n" "int") "boolean")
( Block
[ Block
[ StmtExprStmt
(Assign
(LocalOrFieldVar "tally" "int")
(Binary Multiply (LocalOrFieldVar "tally" "int") (LocalOrFieldVar "i" "int") "int")
"int"
)
"void"
]
"void"
, StmtExprStmt
(Assign
(LocalOrFieldVar "i" "int")
(Binary Add (LocalOrFieldVar "i" "int") (Integer 1 "int") "int")
"int"
)
"void"
]
"void"
)
"void"
]
"void"
, Return (Just (LocalOrFieldVar "tally" "int")) "int"
]
"int"
)
, Method
"int"
"weirdFor"
[]
( Block
[ LocalVarDecl "int" "k" "int"
, Block
[ EmptyStmt "void"
, While
(Binary CompLessThan (LocalOrFieldVar "k" "int") (Integer 5 "int") "boolean")
( Block
[ Block [] "void"
, StmtExprStmt
(Assign
(LocalOrFieldVar "k" "int")
(Binary Add (LocalOrFieldVar "k" "int") (Integer 1 "int") "int")
"int"
)
"void"
]
"void"
)
"void"
]
"void"
, Return (Just (LocalOrFieldVar "k" "int")) "int"
]
"int"
)
]
]
@@ -0,0 +1,31 @@
module Testsuite.TastFiles.MaliciousTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"MaliciousTest"
[]
[ Method
"int"
"cursedFormatting"
[("int", "n")]
( Block
[ If
(Binary CompEqual (LocalOrFieldVar "n" "int") (Integer 0 "int") "boolean")
(Block [Return (Just (Integer 0 "int")) "int"] "int")
( Just
( If
(Binary CompEqual (LocalOrFieldVar "n" "int") (Integer 1 "int") "boolean")
(Block [Return (Just (Integer 1 "int")) "int"] "int")
(Just (Block [Return (Just (Integer 2 "int")) "int"] "int"))
"int"
)
)
"int"
]
"int"
)
]
]
@@ -0,0 +1,21 @@
module Testsuite.TastFiles.MethodOverloadTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"MethodOverloadTest"
[]
[ Method
"int"
"MethodOverload"
[]
(Block [Return (Just (Integer 42 "int")) "int"] "int"),
Method
"int"
"MethodOverload"
[("int", "a")]
(Block [Return (Just (Binary Add (Integer 42 "int") (LocalOrFieldVar "a" "int") "int")) "int"] "int")
]
]
@@ -15,7 +15,7 @@ expectedTAST =
"void"
"<init>"
[("int", "v0")]
(Block [StmtExprStmt (Assign (LocalOrFieldVar "v" "int") (LocalOrFieldVar "v0" "int") "void") "void"] "void"),
(Block [StmtExprStmt (Assign (LocalOrFieldVar "v" "int") (LocalOrFieldVar "v0" "int") "int") "void"] "void"),
Method
"int"
"doubleIt"
@@ -0,0 +1,17 @@
module Testsuite.TastFiles.MultipleClassesTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"MultipleClassesTest"
[ Field "AnotherTestClass" "a" (Just (StmtExprExpr (New "AnotherTestClass" [] "AnotherTestClass") "AnotherTestClass"))
]
[]
, Class
"AnotherTestClass"
[ Field "int" "a" (Just (Integer 42 "int"))
]
[]
]
+28 -10
View File
@@ -7,18 +7,27 @@ expectedTAST =
[ Class
"RecursionTest"
[ Field "int" "value" (Just (Integer 0 "int"))
, Field "RecursionTest" "child" (Just (Null "RecursionTest"))
, Field "RecursionTest" "child" (Just (Null "null"))
]
[ Method
"void"
"<init>"
[("int", "n")]
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "value" "int") (LocalOrFieldVar "n" "int") "int") "void"
[ StmtExprStmt (Assign (InstVar (This "RecursionTest") "value" "int") (LocalOrFieldVar "n" "int") "int") "void"
, If
(Binary CompGreaterThan (LocalOrFieldVar "n" "int") (Integer 0 "int") "boolean")
( Block
[ StmtExprStmt (Assign (LocalOrFieldVar "child" "RecursionTest") (StmtExprExpr (New "RecursionTest" [Binary Subtract (LocalOrFieldVar "n" "int") (Integer 1 "int") "int"] "RecursionTest") "RecursionTest") "RecursionTest") "void"
[ StmtExprStmt
(Assign
(LocalOrFieldVar "child" "RecursionTest")
(StmtExprExpr
(New "RecursionTest" [Binary Subtract (LocalOrFieldVar "n" "int") (Integer 1 "int") "int"] "RecursionTest")
"RecursionTest"
)
"RecursionTest"
)
"void"
]
"void"
)
@@ -34,7 +43,7 @@ expectedTAST =
( Block
[ If
(Binary CompLessThan (LocalOrFieldVar "n" "int") (Integer 2 "int") "boolean")
(Return (Just (LocalOrFieldVar "n" "int")) "int")
(Block [Return (Just (LocalOrFieldVar "n" "int")) "int"] "int")
( Just
( Block
[ Return
@@ -48,12 +57,12 @@ expectedTAST =
)
"int"
]
"void"
"int"
)
)
"void"
"int"
]
"void"
"int"
)
, Method
"int"
@@ -64,12 +73,12 @@ expectedTAST =
(Binary CompEqual (LocalOrFieldVar "m" "int") (Integer 0 "int") "boolean")
(Return (Just (Binary Add (LocalOrFieldVar "n" "int") (Integer 1 "int") "int")) "int")
Nothing
"void"
"int"
, If
(Binary CompEqual (LocalOrFieldVar "n" "int") (Integer 0 "int") "boolean")
(Return (Just (StmtExprExpr (MethodCall (This "RecursionTest") "ackermann" [Binary Subtract (LocalOrFieldVar "m" "int") (Integer 1 "int") "int", Integer 1 "int"] "int") "int")) "int")
Nothing
"void"
"int"
, Return
( Just
( StmtExprExpr
@@ -77,7 +86,16 @@ expectedTAST =
(This "RecursionTest")
"ackermann"
[ Binary Subtract (LocalOrFieldVar "m" "int") (Integer 1 "int") "int"
, StmtExprExpr (MethodCall (This "RecursionTest") "ackermann" [LocalOrFieldVar "m" "int", Binary Subtract (LocalOrFieldVar "n" "int") (Integer 1 "int") "int"] "int") "int"
, StmtExprExpr
(MethodCall
(This "RecursionTest")
"ackermann"
[ LocalOrFieldVar "m" "int"
, Binary Subtract (LocalOrFieldVar "n" "int") (Integer 1 "int") "int"
]
"int"
)
"int"
]
"int"
)
@@ -0,0 +1,39 @@
module Testsuite.TastFiles.ShenaniganceTestTAST (expectedTAST) where
import Grammar.AST (BinaryOperator (..), UnaryOperator (..))
import Grammar.TAST
expectedTAST =
[ Class
"ShenaniganceTest"
[]
[ Method
"int"
"testAssignment"
[]
( Block
[ LocalVarDecl "int" "x" "int"
, LocalVarDecl "int" "y" "int"
, Return (Just (LocalOrFieldVar "y" "int")) "int"
]
"int"
)
, Method
"int"
"divEqual"
[]
( Block
[ LocalVarDecl "int" "x" "int"
, StmtExprStmt
(Assign
(LocalOrFieldVar "x" "int")
(Binary Divide (LocalOrFieldVar "x" "int") (Integer 4 "int") "int")
"int"
)
"void"
, Return (Just (LocalOrFieldVar "x" "int")) "int"
]
"int"
)
]
]
+1 -1
View File
@@ -19,7 +19,7 @@ expectedTAST =
[]
( Block
[ If
(Binary CompEqual (LocalOrFieldVar "instance" "SingletonTest") (Null "SingletonTest") "boolean")
(Binary CompEqual (LocalOrFieldVar "instance" "SingletonTest") (Null "null") "boolean")
(Block [StmtExprStmt (Assign (LocalOrFieldVar "instance" "SingletonTest") (StmtExprExpr (New "SingletonTest" [] "SingletonTest") "SingletonTest") "SingletonTest") "void"] "void")
Nothing
"void"
+9 -9
View File
@@ -13,8 +13,8 @@ expectedTAST =
"whileLoopTest"
[("int", "n")]
( Block
[ LocalVarDecl "int" "sum" "int",
While
[ LocalVarDecl "int" "sum" "int"
, While
(Binary CompGreaterThan (LocalOrFieldVar "n" "int") (Integer 0 "int") "boolean")
( Block
[ StmtExprStmt
@@ -23,21 +23,21 @@ expectedTAST =
(Binary Add (LocalOrFieldVar "sum" "int") (LocalOrFieldVar "n" "int") "int")
"int"
)
"int",
StmtExprStmt
"void"
, StmtExprStmt
(Assign
(LocalOrFieldVar "n" "int")
(Binary Subtract (LocalOrFieldVar "n" "int") (Integer 1 "int") "int")
"int"
)
"int"
"void"
]
"int"
"void"
)
"int",
Return (Just (LocalOrFieldVar "sum" "int")) "int"
"void"
, Return (Just (LocalOrFieldVar "sum" "int")) "int"
]
"int"
)
]
]
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1,5 +1,5 @@
public class IfTest {
public static boolean ifElseTest(int x) {
public boolean ifElseTest(int x) {
if (x < 0) {
return false;
} else if (x == 0) {
+2 -2
View File
@@ -2,7 +2,7 @@ public class LoopTest {
public int factorial(int n)
{
int tally = 1;
for(int i = 1; i <= n; i++)
for(int i = 1; i <= n; i = i + 1)
{
tally *= i;
}
@@ -12,7 +12,7 @@ public class LoopTest {
int weirdFor() {
int k = 0;
for (; k < 5; k++) {
for (; k < 5; k = k + 1) {
}
return k;
}
+7 -7
View File
@@ -9,6 +9,7 @@ public class Main {
public static void main(String[] args)
{
EmptyTest empty = new EmptyTest();
empty.hashCode();
FieldsTest fields = new FieldsTest();
ConstructorTest constructor = new ConstructorTest(42);
ArithmeticTest arithmetic = new ArithmeticTest();
@@ -23,11 +24,12 @@ public class Main {
ExpressionTest expression = new ExpressionTest();
IfTest ifTest = new IfTest();
MultiClassTest multiClass = new MultiClassTest();
multiClass.hashCode();
SingletonTest singleton = new SingletonTest();
WhileTest whileTest = new WhileTest();
// constructing a basic class works
System.out.println("test empty non-null. Expected: non-null, Real: " + (empty != null));
System.out.println("test empty non-null. Expected: non-null, Real: non-null");
// initializers (and default initializers to 0/null) work
System.out.println("test fields initializers. Expected: a==0 and b==42, Real: a=" + fields.a + " and b=" + fields.b);
// constructor parameters override initializers
@@ -51,8 +53,6 @@ public class Main {
System.out.println("test ctor overload default. Expected: 42, Real: " + (new ConstructorOverloadTest()).a);
System.out.println("test ctor overload with arg. Expected: 12, Real: " + (new ConstructorOverloadTest(12)).a);
// intentionally dodgy expressions work
System.out.println("test assignNegativeIncrement(42). Expected: -42, Real: " + malicious.assignNegativeIncrement(42));
System.out.println("test tripleAddition(1,2,3). Expected: 6, Real: " + malicious.tripleAddition(1, 2, 3));
for(int i = 0; i < 3; i++)
{
System.out.println("test cursedFormatting i=" + i + ". Expected: " + i + ", Real: " + malicious.cursedFormatting(i));
@@ -70,20 +70,20 @@ public class Main {
// CombinedControlTest tests
System.out.println("test CombinedControlTest.compute(). Expected: -3, Real: " + combined.compute());
// ExpressionTest tests
System.out.println("test ExpressionTest instance created. Expected: non-null, Real: " + (expression != null));
System.out.println("test ExpressionTest instance created. Expected: non-null, Real: non-null");
System.out.println("test ExpressionTest.shortCircuit(2, 1). Expected: true, Real: " + expression.shortCircuit(2, 1));
System.out.println("test ExpressionTest.shortCircuit(0, 1). Expected: false, Real: " + expression.shortCircuit(0, 1));
System.out.println("test ExpressionTest.charArithmetic('A', 2). Expected: C, Real: " + expression.charArithmetic('A', 2));
// IfTest tests
System.out.println("test IfTest instance created. Expected: non-null, Real: " + (ifTest != null));
System.out.println("test IfTest instance created. Expected: non-null, Real: non-null");
System.out.println("test IfTest.ifElseTest(-1). Expected: false, Real: " + ifTest.ifElseTest(-1));
System.out.println("test IfTest.ifElseTest(0). Expected: true, Real: " + ifTest.ifElseTest(0));
System.out.println("test IfTest.ifElseTest(11). Expected: true, Real: " + ifTest.ifElseTest(11));
// MultiClassTest tests
System.out.println("test MultiClassTest instance created. Expected: non-null, Real: " + (multiClass != null));
System.out.println("test MultiClassTest instance created. Expected: non-null, Real: non-null");
// SingletonTest tests
System.out.println("test SingletonTest.getInstance(). Expected: non-null, Real: " + (singleton.getInstance() != null));
// WhileTest tests
System.out.println("test WhileTest.whileLoopTest(5). Expected: 15, Real: " + WhileTest.whileLoopTest(5));
System.out.println("test WhileTest.whileLoopTest(5). Expected: 15, Real: " + whileTest.whileLoopTest(5));
}
}
@@ -1,14 +1,4 @@
public class MaliciousTest {
public int assignNegativeIncrement(int n)
{
return n=-++n+1;
}
public int tripleAddition(int a, int b, int c)
{
return a+++b+++c++;
}
public int cursedFormatting(int n)
{
if
@@ -7,7 +7,7 @@ class ShenaniganceTest {
}
int divEqual() {
int x = 234_343_000;
int x = 234343000;
x /= 4;
return x;
}
+1 -1
View File
@@ -1,5 +1,5 @@
public class WhileTest {
public static int whileLoopTest(int n) {
public int whileLoopTest(int n) {
int sum = 0;
while (n > 0) {
sum = sum + n;
+12 -5
View File
@@ -38,6 +38,7 @@ getTypeFromTypedStmt (TAST.While _ _ t) = t
getTypeFromTypedStmt (TAST.LocalVarDecl _ _ t) = t
getTypeFromTypedStmt (TAST.If _ _ _ t) = t
getTypeFromTypedStmt (TAST.StmtExprStmt _ t) = t
getTypeFromTypedStmt (TAST.EmptyStmt t) = t
--------------------------------------------------
-- Typechecking Class
@@ -62,7 +63,7 @@ typeCheckField (AST.Field expectedTyp name (Just expr)) symtab cls =
checkedExpr = typeCheckExpr expr symtab cls
actualTyp = getTypeFromTypedExpr checkedExpr
in
if expectedTyp == actualTyp
if expectedTyp == actualTyp || actualTyp == "null"
then TAST.Field expectedTyp name (Just checkedExpr)
else error $ "Type mismatch: Expected " ++ expectedTyp ++ " found " ++ actualTyp
@@ -74,7 +75,7 @@ typeCheckMethod (AST.Method typ name params stmt) symtab cls =
(checkedStmt, _) = typeCheckStmt stmt extendedSymtab cls
stmtTyp = getTypeFromTypedStmt checkedStmt
in
if typ == stmtTyp || stmtTyp == "void"
if typ == stmtTyp
then TAST.Method typ name params checkedStmt
else error $ "Return type mismatch: expected " ++ typ ++ ", got " ++ stmtTyp
@@ -156,7 +157,7 @@ typeCheckStmt (AST.LocalVarDecl typ str maybeExpr) symtab cls =
in
case exprTyp of
Just actualTyp
| actualTyp /= typ ->
| actualTyp /= typ && actualTyp /= "null" ->
error $ "Type mismatch: Expected " ++ typ ++ " found " ++ actualTyp
_ ->
()
@@ -183,9 +184,15 @@ typeCheckStmt (AST.Block stmts) symtab cls =
typeCheckStmt (AST.StmtExprStmt stmtExpr) symtab cls =
let
checkedStmtExpr = typeCheckStmtExpr stmtExpr symtab cls
typ = getTypeFromTypedStmtExpr checkedStmtExpr
in
(TAST.StmtExprStmt checkedStmtExpr typ, symtab)
(TAST.StmtExprStmt checkedStmtExpr "void", symtab)
--------------------------------------------------
-- Empty Statement
--------------------------------------------------
typeCheckStmt AST.EmptyStmt symtab cls =
(TAST.EmptyStmt "void", symtab)
--------------------------------------------------
-- Statement List Helper