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 80 additions and 70 deletions
Showing only changes of commit ebf54bf4cb - Show all commits

View File

@ -68,6 +68,13 @@ testBlockWithEmptyStatement = TestCase $
assertEqual "expect empty block" (Block []) $ assertEqual "expect empty block" (Block []) $
parseBlock [LBRACKET,SEMICOLON,SEMICOLON,RBRACKET] parseBlock [LBRACKET,SEMICOLON,SEMICOLON,RBRACKET]
testExpressionIntLiteral = TestCase $
assertEqual "expect IntLiteral" (IntegerLiteral 3) $
parseExpression [INTEGERLITERAL 3]
testFieldWithInitialization = TestCase $
assertEqual "expect Class with initialized field" [Class "WithInitField" [] [VariableDeclaration "int" "number" $ Just $ IntegerLiteral 3]] $
parse [CLASS,IDENTIFIER "WithInitField",LBRACKET,INT,IDENTIFIER "number",ASSIGN,INTEGERLITERAL 3,SEMICOLON,RBRACKET]
tests = TestList [ tests = TestList [
testSingleEmptyClass, testSingleEmptyClass,
testTwoEmptyClasses, testTwoEmptyClasses,
@ -87,5 +94,7 @@ tests = TestList [
testBlockWithLocalVarDecl, testBlockWithLocalVarDecl,
testBlockWithMultipleLocalVarDecls, testBlockWithMultipleLocalVarDecls,
testNestedBlocks, testNestedBlocks,
testBlockWithEmptyStatement testBlockWithEmptyStatement,
testExpressionIntLiteral,
testFieldWithInitialization
] ]

View File

@ -1,11 +1,12 @@
{ {
module Parser.JavaParser (parse, parseBlock) where module Parser.JavaParser (parse, parseBlock, parseExpression) where
import Ast import Ast
import Parser.Lexer import Parser.Lexer
} }
%name parse %name parse
%name parseBlock block %name parseBlock block
%name parseExpression expression
%tokentype { Token } %tokentype { Token }
%error { parseError } %error { parseError }
%errorhandlertype explist %errorhandlertype explist
@ -182,7 +183,7 @@ referencetype : classorinterfacetype { $1 }
variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } variabledeclarator : variabledeclaratorid { Declarator $1 Nothing }
-- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 (Just $3) }
blockstatement : localvariabledeclarationstatement { $1 } blockstatement : localvariabledeclarationstatement { $1 }
| statement { $1 } | statement { $1 }
@ -196,7 +197,7 @@ numerictype : integraltype { $1 }
variabledeclaratorid : IDENTIFIER { $1 } variabledeclaratorid : IDENTIFIER { $1 }
variableinitializer : expression { } variableinitializer : expression { $1 }
localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 } localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 }
@ -206,7 +207,7 @@ statement : statementwithouttrailingsubstatement{ $1 }
-- | whilestatement { } -- | whilestatement { }
expression : assignmentexpression { } expression : assignmentexpression { $1 }
integraltype : INT { "int" } integraltype : INT { "int" }
| CHAR { "char" } | CHAR { "char" }
@ -224,8 +225,8 @@ ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE state
whilestatement : WHILE LBRACE expression RBRACE statement { } whilestatement : WHILE LBRACE expression RBRACE statement { }
assignmentexpression : conditionalexpression { } assignmentexpression : conditionalexpression { $1 }
| assignment{ } -- | assignment { }
emptystatement : SEMICOLON { Block [] } emptystatement : SEMICOLON { Block [] }
@ -238,8 +239,8 @@ statementnoshortif : statementwithouttrailingsubstatement { }
| ifthenelsestatementnoshortif { } | ifthenelsestatementnoshortif { }
| whilestatementnoshortif { } | whilestatementnoshortif { }
conditionalexpression : conditionalorexpression { } conditionalexpression : conditionalorexpression { $1 }
| conditionalorexpression QUESMARK expression COLON conditionalexpression { } -- | conditionalorexpression QUESMARK expression COLON conditionalexpression { }
assignment : lefthandside assignmentoperator assignmentexpression { } assignment : lefthandside assignmentoperator assignmentexpression { }
@ -257,23 +258,23 @@ ifthenelsestatementnoshortif :IF LBRACE expression RBRACE statementnoshortif
whilestatementnoshortif : WHILE LBRACE expression RBRACE statementnoshortif { } whilestatementnoshortif : WHILE LBRACE expression RBRACE statementnoshortif { }
conditionalorexpression : conditionalandexpression { } conditionalorexpression : conditionalandexpression { $1 }
| conditionalorexpression LOGICALOR conditionalandexpression{ } -- | conditionalorexpression LOGICALOR conditionalandexpression{ }
lefthandside : name { } lefthandside : name { $1 }
assignmentoperator : ASSIGN{ } assignmentoperator : ASSIGN{ }
| TIMESEQUAL { } -- | TIMESEQUAL { }
| DIVIDEEQUAL { } -- | DIVIDEEQUAL { }
| MODULOEQUAL { } -- | MODULOEQUAL { }
| PLUSEQUAL { } -- | PLUSEQUAL { }
| MINUSEQUAL { } -- | MINUSEQUAL { }
| SHIFTLEFTEQUAL { } -- | SHIFTLEFTEQUAL { }
| SIGNEDSHIFTRIGHTEQUAL { } -- | SIGNEDSHIFTRIGHTEQUAL { }
| UNSIGNEDSHIFTRIGHTEQUAL { } -- | UNSIGNEDSHIFTRIGHTEQUAL { }
| ANDEQUAL { } -- | ANDEQUAL { }
| XOREQUAL { } -- | XOREQUAL { }
| OREQUAL{ } -- | OREQUAL{ }
preincrementexpression : INCREMENT unaryexpression { } preincrementexpression : INCREMENT unaryexpression { }
@ -291,73 +292,73 @@ methodinvocation : name LBRACE RBRACE { }
classinstancecreationexpression : NEW classtype LBRACE RBRACE { } classinstancecreationexpression : NEW classtype LBRACE RBRACE { }
| NEW classtype LBRACE argumentlist RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { }
conditionalandexpression : inclusiveorexpression { } conditionalandexpression : inclusiveorexpression { $1 }
fieldaccess : primary DOT IDENTIFIER { } fieldaccess : primary DOT IDENTIFIER { }
unaryexpression : preincrementexpression { } unaryexpression : unaryexpressionnotplusminus { $1 }
| predecrementexpression { } -- | predecrementexpression { }
| PLUS unaryexpression { } -- | PLUS unaryexpression { }
| MINUS unaryexpression { } -- | MINUS unaryexpression { }
| unaryexpressionnotplusminus { } -- | preincrementexpression { $1 }
postfixexpression : primary { } postfixexpression : primary { $1 }
| name { } -- | name { }
| postincrementexpression { } -- | postincrementexpression { }
| postdecrementexpression{ } -- | postdecrementexpression{ }
primary : primarynonewarray { } primary : primarynonewarray { $1 }
inclusiveorexpression : exclusiveorexpression { } inclusiveorexpression : exclusiveorexpression { $1 }
| inclusiveorexpression OR exclusiveorexpression { } -- | inclusiveorexpression OR exclusiveorexpression { }
primarynonewarray : literal { } primarynonewarray : literal { $1 }
| THIS { } -- | THIS { }
| LBRACE expression RBRACE { } -- | LBRACE expression RBRACE { }
| classinstancecreationexpression { } -- | classinstancecreationexpression { }
| fieldaccess { } -- | fieldaccess { }
| methodinvocation { } -- | methodinvocation { }
unaryexpressionnotplusminus : postfixexpression { } unaryexpressionnotplusminus : postfixexpression { $1 }
| TILDE unaryexpression { } -- | TILDE unaryexpression { }
| EXCLMARK unaryexpression { } -- | EXCLMARK unaryexpression { }
| castexpression{ } -- | castexpression{ }
exclusiveorexpression : andexpression { } exclusiveorexpression : andexpression { $1 }
| exclusiveorexpression XOR andexpression { } -- | exclusiveorexpression XOR andexpression { }
literal : INTLITERAL { } literal : INTLITERAL { IntegerLiteral $1 }
| BOOLLITERAL { } -- | BOOLLITERAL { }
| CHARLITERAL { } -- | CHARLITERAL { }
| JNULL { } -- | JNULL { }
castexpression : LBRACE primitivetype RBRACE unaryexpression { } castexpression : LBRACE primitivetype RBRACE unaryexpression { }
| LBRACE expression RBRACE unaryexpressionnotplusminus{ } | LBRACE expression RBRACE unaryexpressionnotplusminus{ }
andexpression : equalityexpression { } andexpression : equalityexpression { $1 }
| andexpression AND equalityexpression { } -- | andexpression AND equalityexpression { }
equalityexpression : relationalexpression { } equalityexpression : relationalexpression { $1 }
-- | equalityexpression EQUAL relationalexpression { } -- | equalityexpression EQUAL relationalexpression { }
-- | equalityexpression NOTEQUAL relationalexpression { } -- | equalityexpression NOTEQUAL relationalexpression { }
relationalexpression : shiftexpression { } relationalexpression : shiftexpression { $1 }
| relationalexpression LESS shiftexpression { } -- | relationalexpression LESS shiftexpression { }
| relationalexpression GREATER shiftexpression { } -- | relationalexpression GREATER shiftexpression { }
| relationalexpression LESSEQUAL shiftexpression { } -- | relationalexpression LESSEQUAL shiftexpression { }
| relationalexpression GREATEREQUAL shiftexpression { } -- | relationalexpression GREATEREQUAL shiftexpression { }
| relationalexpression INSTANCEOF referencetype { } -- | relationalexpression INSTANCEOF referencetype { }
shiftexpression : additiveexpression { } shiftexpression : additiveexpression { $1 }
additiveexpression : multiplicativeexpression { } additiveexpression : multiplicativeexpression { $1 }
| additiveexpression PLUS multiplicativeexpression { } -- | additiveexpression PLUS multiplicativeexpression { }
| additiveexpression MINUS multiplicativeexpression { } -- | additiveexpression MINUS multiplicativeexpression { }
multiplicativeexpression : unaryexpression { } multiplicativeexpression : unaryexpression { $1 }
| multiplicativeexpression MUL unaryexpression { } -- | multiplicativeexpression MUL unaryexpression { }
| multiplicativeexpression DIV unaryexpression { } -- | multiplicativeexpression DIV unaryexpression { }
| multiplicativeexpression MOD unaryexpression { } -- | multiplicativeexpression MOD unaryexpression { }
{ {