block and if assembly (no else)

This commit is contained in:
mrab 2024-05-14 13:20:37 +02:00
parent 5284d6ecba
commit f9f984568f
2 changed files with 13 additions and 2 deletions

View File

@ -206,7 +206,7 @@ assembleMethod (MethodDeclaration _ name _ (TypedStatement _ (Block statements))
(constants_a, ops_a) = foldr assembleStatement (constants, ops) statements
init_ops = [Opaload 0, Opinvokespecial 2]
in
(constants_a, init_ops ++ ops_a)
(constants_a, init_ops ++ ops_a ++ [Opreturn])
| otherwise = let
(constants_a, ops_a) = foldr assembleStatement (constants, ops) statements
init_ops = [Opaload 0]
@ -221,6 +221,15 @@ assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case e
(expr_constants, expr_ops) = assembleExpression expr (constants, ops)
in
(expr_constants, expr_ops ++ [returnOperation stype])
assembleStatement (TypedStatement _ (Block statements)) (constants, ops) =
foldr assembleStatement (constants, ops) statements
assembleStatement (TypedStatement _ (If expr if_stmt else_stmt)) (constants, ops) = let
(constants_cmp, ops_cmp) = assembleExpression expr (constants, [])
(constants_ifa, ops_ifa) = assembleStatement if_stmt (constants_cmp, [])
skip_length = sum (map opcodeEncodingLength ops_ifa)
in
(constants_ifa, ops ++ ops_cmp ++ [Opsipush 0, Opif_icmpeq skip_length] ++ ops_ifa)
assembleExpression :: Assembler Expression
assembleExpression (TypedExpression _ (BinaryOperation op a b)) (constants, ops)

View File

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