From e572975bdacdb3441e2430c526d9ab07197ec35a Mon Sep 17 00:00:00 2001 From: Christian Brier Date: Wed, 15 May 2024 11:45:41 +0200 Subject: [PATCH] add: inject default constructor --- src/ByteCode/ClassFile/Generator.hs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ByteCode/ClassFile/Generator.hs b/src/ByteCode/ClassFile/Generator.hs index 935de81..d78dae6 100644 --- a/src/ByteCode/ClassFile/Generator.hs +++ b/src/ByteCode/ClassFile/Generator.hs @@ -94,6 +94,11 @@ methodDescriptor (MethodDeclaration returntype _ parameters _) = let ++ ")" ++ methodParameterDescriptor returntype +injectDefaultConstructor :: [MethodDeclaration] -> [MethodDeclaration] +injectDefaultConstructor pre + | any (\(MethodDeclaration _ name _ _) -> name == "") pre = pre + | otherwise = pre ++ [MethodDeclaration "void" "" [] (TypedStatement "void" (Block []))] + classBuilder :: ClassFileBuilder Class classBuilder (Class name methods fields) _ = let @@ -117,9 +122,11 @@ classBuilder (Class name methods fields) _ = let attributes = [] } + methodsWithInjectedConstructor = injectDefaultConstructor methods + classFileWithFields = foldr fieldBuilder nakedClassFile fields - classFileWithMethods = foldr methodBuilder classFileWithFields methods - classFileWithAssembledMethods = foldr methodAssembler classFileWithMethods methods + classFileWithMethods = foldr methodBuilder classFileWithFields methodsWithInjectedConstructor + classFileWithAssembledMethods = foldr methodAssembler classFileWithMethods methodsWithInjectedConstructor in classFileWithAssembledMethods @@ -232,7 +239,7 @@ assembleMethod (constants, ops, lvars) (MethodDeclaration _ name _ (TypedStateme init_ops = [Opaload 0] in (constants_a, init_ops ++ ops_a, lvars_a) -assembleMethod _ (MethodDeclaration _ _ _ stmt) = error ("Block expected for method body, got: " ++ show stmt) +assembleMethod _ (MethodDeclaration _ _ _ stmt) = error ("Block expected for method body, got: " ++ show stmt) assembleStatement :: Assembler Statement assembleStatement (constants, ops, lvars) (TypedStatement stype (Return expr)) = case expr of