Compare commits

..

No commits in common. "09f70ca7890ad0a02b64d3864e5940045369bbaf" and "53e1afc9e460a80b5fd73576e1bb48b0ef6a46bd" have entirely different histories.

4 changed files with 21 additions and 59 deletions

View File

@ -144,21 +144,6 @@ testExpressionPreIncrement = TestCase $
testExpressionPreDecrement = TestCase $
assertEqual "expect PreIncrement" (UnaryOperation PreDecrement (Reference "a")) $
parseExpression [DECREMENT,IDENTIFIER "a"]
testExpressionAssign = TestCase $
assertEqual "expect assign 5 to a" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $
parseExpression [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5]
testExpressionTimesEqual = TestCase $
assertEqual "expect assign and multiplication" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Multiplication (Reference "a") (IntegerLiteral 5)))) $
parseExpression [IDENTIFIER "a",TIMESEQUAL,INTEGERLITERAL 5]
testExpressionDivideEqual = TestCase $
assertEqual "expect assign and division" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Division (Reference "a") (IntegerLiteral 5)))) $
parseExpression [IDENTIFIER "a",DIVEQUAL,INTEGERLITERAL 5]
testExpressionPlusEqual = TestCase $
assertEqual "expect assign and addition" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Addition (Reference "a") (IntegerLiteral 5)))) $
parseExpression [IDENTIFIER "a",PLUSEQUAL,INTEGERLITERAL 5]
testExpressionMinusEqual = TestCase $
assertEqual "expect assign and subtraction" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Subtraction (Reference "a") (IntegerLiteral 5)))) $
parseExpression [IDENTIFIER "a",MINUSEQUAL,INTEGERLITERAL 5]
testStatementIfThen = TestCase $
assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $
@ -166,10 +151,6 @@ testStatementIfThen = TestCase $
testStatementIfThenElse = TestCase $
assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) (Just (Block [Block []]))] $
parseStatement [IF,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET,ELSE,LBRACKET,RBRACKET]
testStatementWhile = TestCase $
assertEqual "expect while" [While (Reference "a") (Block [Block []])] $
parseStatement [WHILE,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET]
tests = TestList [
@ -212,13 +193,6 @@ tests = TestList [
testExpressionPostDecrement,
testExpressionPreIncrement,
testExpressionPreDecrement,
testExpressionAssign,
testExpressionTimesEqual,
testExpressionTimesEqual,
testExpressionDivideEqual,
testExpressionPlusEqual,
testExpressionMinusEqual,
testStatementIfThen,
testStatementIfThenElse,
testStatementWhile
testStatementIfThenElse
]

View File

@ -113,10 +113,10 @@ opcodeEncodingLength (Opinvokespecial _) = 3
opcodeEncodingLength (Opgoto _) = 3
opcodeEncodingLength (Opsipush _) = 3
opcodeEncodingLength (Opldc_w _) = 3
opcodeEncodingLength (Opaload _) = 4
opcodeEncodingLength (Opiload _) = 4
opcodeEncodingLength (Opastore _) = 4
opcodeEncodingLength (Opistore _) = 4
opcodeEncodingLength (Opaload _) = 3
opcodeEncodingLength (Opiload _) = 3
opcodeEncodingLength (Opastore _) = 3
opcodeEncodingLength (Opistore _) = 3
opcodeEncodingLength (Opputfield _) = 3
opcodeEncodingLength (Opgetfield _) = 3

View File

@ -262,14 +262,6 @@ assembleStatement (constants, ops, lvars) (TypedStatement _ (If expr if_stmt els
else_length = sum (map opcodeEncodingLength ops_elsea) + 3
in
(constants_ifa, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq if_length] ++ ops_ifa ++ [Opgoto else_length] ++ ops_elsea, lvars)
assembleStatement (constants, ops, lvars) (TypedStatement _ (While expr stmt)) = let
(constants_cmp, ops_cmp, _) = assembleExpression (constants, [], lvars) expr
(constants_stmta, ops_stmta, _) = assembleStatement (constants_cmp, [], lvars) stmt
-- +3 because we insert 2 gotos, one for the comparison, one for the goto back to the comparison
stmt_length = sum (map opcodeEncodingLength ops_stmta) + 6
entire_length = stmt_length + sum (map opcodeEncodingLength ops_cmp)
in
(constants_stmta, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq stmt_length] ++ ops_stmta ++ [Opgoto (-entire_length)], lvars)
assembleStatement (constants, ops, lvars) (TypedStatement _ (LocalVariableDeclaration (VariableDeclaration dtype name expr))) = let
isPrimitive = elem dtype ["char", "boolean", "int"]
(constants_init, ops_init, _) = case expr of

View File

@ -82,7 +82,7 @@ compilationunit : typedeclarations { $1 }
typedeclarations : typedeclaration { [$1] }
| typedeclarations typedeclaration { $1 ++ [$2] }
name : simplename { Reference $1 }
name : simplename { $1 }
-- | qualifiedname { }
typedeclaration : classdeclaration { $1 }
@ -122,7 +122,7 @@ classtype : classorinterfacetype{ }
classbodydeclaration : classmemberdeclaration { $1 }
| constructordeclaration { $1 }
classorinterfacetype : simplename { $1 }
classorinterfacetype : name { $1 }
classmemberdeclaration : fielddeclaration { $1 }
| methoddeclaration { $1 }
@ -203,7 +203,7 @@ localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 }
statement : statementwithouttrailingsubstatement{ $1 } -- statement returns a list of statements
| ifthenstatement { [$1] }
| ifthenelsestatement { [$1] }
| whilestatement { [$1] }
-- | whilestatement { }
expression : assignmentexpression { $1 }
@ -222,10 +222,10 @@ ifthenstatement : IF LBRACE expression RBRACE statement { If $3 (Block $5) No
ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE statement { If $3 (Block $5) (Just (Block $7)) }
whilestatement : WHILE LBRACE expression RBRACE statement { While $3 (Block $5) }
whilestatement : WHILE LBRACE expression RBRACE statement { }
assignmentexpression : conditionalexpression { $1 }
| assignment { StatementExpressionExpression $1 }
-- | assignment { }
emptystatement : SEMICOLON { Block [] }
@ -241,11 +241,7 @@ statementnoshortif : statementwithouttrailingsubstatement { $1 }
conditionalexpression : conditionalorexpression { $1 }
-- | conditionalorexpression QUESMARK expression COLON conditionalexpression { }
assignment : lefthandside assignmentoperator assignmentexpression {
case $2 of
Nothing -> Assignment $1 $3
Just operator -> Assignment $1 (BinaryOperation operator $1 $3)
}
assignment : lefthandside assignmentoperator assignmentexpression { }
statementexpression : assignment { }
@ -266,18 +262,18 @@ conditionalorexpression : conditionalandexpression { $1 }
lefthandside : name { $1 }
assignmentoperator : ASSIGN { Nothing }
| TIMESEQUAL { Just Multiplication }
| DIVIDEEQUAL { Just Division }
| MODULOEQUAL { Just Modulo }
| PLUSEQUAL { Just Addition }
| MINUSEQUAL { Just Subtraction }
assignmentoperator : ASSIGN{ }
-- | TIMESEQUAL { }
-- | DIVIDEEQUAL { }
-- | MODULOEQUAL { }
-- | PLUSEQUAL { }
-- | MINUSEQUAL { }
-- | SHIFTLEFTEQUAL { }
-- | SIGNEDSHIFTRIGHTEQUAL { }
-- | UNSIGNEDSHIFTRIGHTEQUAL { }
| ANDEQUAL { Just BitwiseAnd }
| XOREQUAL { Just BitwiseXor }
| OREQUAL{ Just BitwiseOr }
-- | ANDEQUAL { }
-- | XOREQUAL { }
-- | OREQUAL{ }
preincrementexpression : INCREMENT unaryexpression { UnaryOperation PreIncrement $2 }
@ -306,7 +302,7 @@ unaryexpression : unaryexpressionnotplusminus { $1 }
| preincrementexpression { $1 }
postfixexpression : primary { $1 }
| name { $1 }
| name { Reference $1 }
| postincrementexpression { $1 }
| postdecrementexpression{ $1 }