constructor and method call using this

This commit is contained in:
mrab 2024-05-14 12:29:55 +02:00
parent 86e15b5856
commit d1d9a5d6e1
4 changed files with 72 additions and 61 deletions

View File

@ -42,6 +42,7 @@ data Operation = Opiadd
| Opreturn
| Opireturn
| Opareturn
| Opinvokespecial Word16
| Opgoto Word16
| Opsipush Word16
| Opldc_w Word16
@ -108,6 +109,7 @@ opcodeEncodingLength Opaconst_null = 1
opcodeEncodingLength Opreturn = 1
opcodeEncodingLength Opireturn = 1
opcodeEncodingLength Opareturn = 1
opcodeEncodingLength (Opinvokespecial _) = 3
opcodeEncodingLength (Opgoto _) = 3
opcodeEncodingLength (Opsipush _) = 3
opcodeEncodingLength (Opldc_w _) = 3
@ -157,6 +159,7 @@ instance Serializable Operation where
serialize Opreturn = [0xB1]
serialize Opireturn = [0xAC]
serialize Opareturn = [0xB0]
serialize (Opinvokespecial index) = 0xB7 : unpackWord16 index
serialize (Opgoto index) = 0xA7 : unpackWord16 index
serialize (Opsipush index) = 0x11 : unpackWord16 index
serialize (Opldc_w index) = 0x13 : unpackWord16 index

View File

@ -201,8 +201,17 @@ comparisonOperation CompareGreaterOrEqual branchLocation = Opif_icmpge branchLoc
assembleMethod :: Assembler MethodDeclaration
assembleMethod (MethodDeclaration _ _ _ (TypedStatement _ (Block statements))) (constants, ops) =
foldr assembleStatement (constants, ops) statements
assembleMethod (MethodDeclaration _ name _ (TypedStatement _ (Block statements))) (constants, ops)
| name == "<init>" = let
(constants_a, ops_a) = foldr assembleStatement (constants, ops) statements
init_ops = [Opaload 0, Opinvokespecial 2]
in
(constants_a, init_ops ++ ops_a)
| otherwise = let
(constants_a, ops_a) = foldr assembleStatement (constants, ops) statements
init_ops = [Opaload 0]
in
(constants_a, init_ops ++ ops_a)
assembleMethod (MethodDeclaration _ _ _ stmt) (_, _) = error ("Block expected for method body, got: " ++ show stmt)
assembleStatement :: Assembler Statement
@ -223,9 +232,8 @@ assembleExpression (TypedExpression _ (BinaryOperation op a b)) (constants, ops)
| elem op [CompareEqual, CompareNotEqual, CompareLessThan, CompareLessOrEqual, CompareGreaterThan, CompareGreaterOrEqual] = let
(aConstants, aOps) = assembleExpression a (constants, ops)
(bConstants, bOps) = assembleExpression b (aConstants, aOps)
current_offset = sum (map opcodeEncodingLength bOps)
cmp_op = comparisonOperation op (fromIntegral (current_offset + 10))
cmp_ops = [cmp_op, Opsipush 1, Opgoto (fromIntegral (current_offset + 13)), Opsipush 0]
cmp_op = comparisonOperation op 9
cmp_ops = [cmp_op, Opsipush 0, Opgoto 6, Opsipush 1]
in
(bConstants, bOps ++ cmp_ops)
assembleExpression (TypedExpression _ (CharacterLiteral literal)) (constants, ops) =

View File

@ -9,7 +9,7 @@ import ByteCode.ClassFile
import Data.ByteString (pack, writeFile)
main = do
let untypedAST = parse $ alexScanTokens "class Testklasse {void something(){return;}}"
let untypedAST = parse $ alexScanTokens "class Testklasse {Testklasse(){} boolean something(){return 5 + 8 - 12 * 22 > 9 - 2 + 3;}}"
let typedAST = head (typeCheckCompilationUnit untypedAST)
let abstractClassFile = classBuilder typedAST emptyClassFile
let assembledClassFile = pack (serialize abstractClassFile)

View File

@ -302,7 +302,7 @@ typeCheckStatement (Return expr) symtab classes =
Nothing -> Nothing
in case expr' of
Just e' -> TypedStatement (getTypeFromExpr e') (Return (Just e'))
Nothing -> TypedStatement "Void" (Return Nothing)
Nothing -> TypedStatement "void" (Return Nothing)
typeCheckStatement (StatementExpressionStatement stmtExpr) symtab classes =
let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes