Add initial typechecker for AST #2

Merged
mrab merged 121 commits from typedAST into master 2024-06-14 07:53:30 +00:00
2 changed files with 24 additions and 17 deletions
Showing only changes of commit 86e15b5856 - Show all commits

View File

@ -140,13 +140,7 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l
memberAccessFlags = accessPublic, memberAccessFlags = accessPublic,
memberNameIndex = (fromIntegral baseIndex), memberNameIndex = (fromIntegral baseIndex),
memberDescriptorIndex = (fromIntegral (baseIndex + 1)), memberDescriptorIndex = (fromIntegral (baseIndex + 1)),
memberAttributes = [ memberAttributes = []
CodeAttribute {
attributeMaxStack = 420,
attributeMaxLocals = 420,
attributeCode = [Opreturn]
}
]
} }
in in
input { input {
@ -157,14 +151,26 @@ methodBuilder (MethodDeclaration returntype name parameters statement) input = l
methodAssembler :: ClassFileBuilder MethodDeclaration methodAssembler :: ClassFileBuilder MethodDeclaration
methodAssembler (MethodDeclaration returntype name parameters statement) input = let methodAssembler (MethodDeclaration returntype name parameters statement) input = let
code = CodeAttribute { methodConstantIndex = findMethodIndex input name
in case methodConstantIndex of
Nothing -> error ("Cannot find method entry in method pool for method: " ++ name)
Just index -> let
declaration = MethodDeclaration returntype name parameters statement
(pre, method : post) = splitAt index (methods input)
(_, bytecode) = assembleMethod declaration (constantPool input, [])
assembledMethod = method {
memberAttributes = [
CodeAttribute {
attributeMaxStack = 420, attributeMaxStack = 420,
attributeMaxLocals = 420, attributeMaxLocals = 420,
attributeCode = [Opiadd] attributeCode = bytecode
}
]
} }
--methodConstantIndex =
in in
input input {
methods = pre ++ (assembledMethod : post)
}
@ -195,8 +201,9 @@ comparisonOperation CompareGreaterOrEqual branchLocation = Opif_icmpge branchLoc
assembleMethod :: Assembler MethodDeclaration assembleMethod :: Assembler MethodDeclaration
assembleMethod (MethodDeclaration _ _ _ (Block statements)) (constants, ops) = assembleMethod (MethodDeclaration _ _ _ (TypedStatement _ (Block statements))) (constants, ops) =
foldr assembleStatement (constants, ops) statements foldr assembleStatement (constants, ops) statements
assembleMethod (MethodDeclaration _ _ _ stmt) (_, _) = error ("Block expected for method body, got: " ++ show stmt)
assembleStatement :: Assembler Statement assembleStatement :: Assembler Statement
assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case expr of assembleStatement (TypedStatement stype (Return expr)) (constants, ops) = case expr of

View File

@ -9,7 +9,7 @@ import ByteCode.ClassFile
import Data.ByteString (pack, writeFile) import Data.ByteString (pack, writeFile)
main = do main = do
let untypedAST = parse $ alexScanTokens "class Testklasse {int a; int b; void something(){} void something_else(){}}" let untypedAST = parse $ alexScanTokens "class Testklasse {void something(){return;}}"
let typedAST = head (typeCheckCompilationUnit untypedAST) let typedAST = head (typeCheckCompilationUnit untypedAST)
let abstractClassFile = classBuilder typedAST emptyClassFile let abstractClassFile = classBuilder typedAST emptyClassFile
let assembledClassFile = pack (serialize abstractClassFile) let assembledClassFile = pack (serialize abstractClassFile)