From c347e6c630e65d5848303f35ca80860f9a49dca4 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:31:58 +0200 Subject: [PATCH 01/12] add test --- Test/TestParser.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index bf874fc..5869961 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -179,6 +179,9 @@ testStatementIfThenElse = TestCase $ testStatementWhile = TestCase $ assertEqual "expect while" [While (Reference "a") (Block [Block []])] $ parseStatement [WHILE,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] +testStatementAssign = TestCase $ + assertEqual "expect assign 5" [StatementExpressionStatement (Assignment (Reference "a") (IntegerLiteral 5))] $ + parseStatement [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5,SEMICOLON] @@ -233,5 +236,6 @@ tests = TestList [ testExpressionPrecedence, testStatementIfThen, testStatementIfThenElse, - testStatementWhile + testStatementWhile, + testStatementAssign ] \ No newline at end of file From 3c70f9f1f64bf77028ff7c308ab52ea18261be89 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:49:52 +0200 Subject: [PATCH 02/12] parser add methodcall no params --- Test/TestParser.hs | 12 +++++++++++- src/Parser/JavaParser.y | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 5869961..e97d793 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -170,6 +170,10 @@ testExpressionPrecedence = TestCase $ assertEqual "expect times to be inner expression" (BinaryOperation Addition (BinaryOperation Multiplication (Reference "b") (Reference "a")) (IntegerLiteral 3)) $ parseExpression [IDENTIFIER "b",TIMES,IDENTIFIER "a",PLUS,INTEGERLITERAL 3] +testExpressionMethodCallNoParams = TestCase $ + assertEqual "expect methodcall no params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ + parseExpression [IDENTIFIER "foo",LBRACE,RBRACE] + testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ parseStatement [IF,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] @@ -183,6 +187,10 @@ testStatementAssign = TestCase $ assertEqual "expect assign 5" [StatementExpressionStatement (Assignment (Reference "a") (IntegerLiteral 5))] $ parseStatement [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5,SEMICOLON] +testStatementMethodCallNoParams = TestCase $ + assertEqual "expect methodcall statement no params" [StatementExpressionStatement (MethodCall (Reference "this") "foo" [])] $ + parseStatement [IDENTIFIER "foo",LBRACE,RBRACE,SEMICOLON] + tests = TestList [ @@ -234,8 +242,10 @@ tests = TestList [ testExpressionBraced, testExpressionThis, testExpressionPrecedence, + testExpressionMethodCallNoParams, testStatementIfThen, testStatementIfThenElse, testStatementWhile, - testStatementAssign + testStatementAssign, + testStatementMethodCallNoParams ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 7002706..07c11b5 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -253,7 +253,7 @@ statementexpression : assignment { $1 } -- | predecrementexpression { } -- | postincrementexpression { } -- | postdecrementexpression { } - -- | methodinvocation { } + | methodinvocation { $1 } -- | classinstancecreationexpression { } ifthenelsestatementnoshortif :IF LBRACE expression RBRACE statementnoshortif @@ -287,10 +287,10 @@ postincrementexpression : postfixexpression INCREMENT { UnaryOperation PostIncre postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecrement $1 } -methodinvocation : name LBRACE RBRACE { } - | name LBRACE argumentlist RBRACE { } - | primary DOT IDENTIFIER LBRACE RBRACE { } - | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } +methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } + -- | name LBRACE argumentlist RBRACE { } + -- | primary DOT IDENTIFIER LBRACE RBRACE { } + -- | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } classinstancecreationexpression : NEW classtype LBRACE RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { } @@ -320,7 +320,7 @@ primarynonewarray : literal { $1 } | LBRACE expression RBRACE { $2 } -- | classinstancecreationexpression { } -- | fieldaccess { } - -- | methodinvocation { } + | methodinvocation { StatementExpressionExpression $1 } unaryexpressionnotplusminus : postfixexpression { $1 } -- | TILDE unaryexpression { } From 24c2920c9c0c38410f53c6c67a8b83871aadfa71 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:58:27 +0200 Subject: [PATCH 03/12] parser implement methodcall with params --- Test/TestParser.hs | 8 ++++++++ src/Parser/JavaParser.y | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index e97d793..c22968c 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -173,6 +173,12 @@ testExpressionPrecedence = TestCase $ testExpressionMethodCallNoParams = TestCase $ assertEqual "expect methodcall no params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ parseExpression [IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionMethodCallOneParam = TestCase $ + assertEqual "expect methodcall one param" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a"])) $ + parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",RBRACE] +testExpressionMethodCallTwoParams = TestCase $ + assertEqual "expect methocall two params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a", IntegerLiteral 5])) $ + parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",COMMA,INTEGERLITERAL 5,RBRACE] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -243,6 +249,8 @@ tests = TestList [ testExpressionThis, testExpressionPrecedence, testExpressionMethodCallNoParams, + testExpressionMethodCallOneParam, + testExpressionMethodCallTwoParams, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 07c11b5..068a821 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -189,8 +189,8 @@ blockstatement : localvariabledeclarationstatement { $1 } -- expected ty formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } -argumentlist : expression { } - | argumentlist COMMA expression { } +argumentlist : expression { [$1] } + | argumentlist COMMA expression { $1 ++ [$3] } numerictype : integraltype { $1 } @@ -288,7 +288,7 @@ postincrementexpression : postfixexpression INCREMENT { UnaryOperation PostIncre postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecrement $1 } methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } - -- | name LBRACE argumentlist RBRACE { } + | simplename LBRACE argumentlist RBRACE { MethodCall (Reference "this") $1 $3 } -- | primary DOT IDENTIFIER LBRACE RBRACE { } -- | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } From f4d31a85ccd49aafeb2835900ce5ba4f9977dec3 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 28 May 2024 23:27:23 +0200 Subject: [PATCH 04/12] parser add dot method call --- Test/TestParser.hs | 8 ++++++++ src/Parser/JavaParser.y | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index c22968c..63f4dc1 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -179,6 +179,12 @@ testExpressionMethodCallOneParam = TestCase $ testExpressionMethodCallTwoParams = TestCase $ assertEqual "expect methocall two params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a", IntegerLiteral 5])) $ parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",COMMA,INTEGERLITERAL 5,RBRACE] +testExpressionThisMethodCall = TestCase $ + assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ + parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionThisMethodCallParam = TestCase $ + assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "x"])) $ + parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,IDENTIFIER "x",RBRACE] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -251,6 +257,8 @@ tests = TestList [ testExpressionMethodCallNoParams, testExpressionMethodCallOneParam, testExpressionMethodCallTwoParams, + testExpressionThisMethodCall, + testExpressionThisMethodCallParam, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 068a821..d51b376 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -289,8 +289,8 @@ postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecre methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } | simplename LBRACE argumentlist RBRACE { MethodCall (Reference "this") $1 $3 } - -- | primary DOT IDENTIFIER LBRACE RBRACE { } - -- | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } + | primary DOT IDENTIFIER LBRACE RBRACE { MethodCall $1 $3 [] } + | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { MethodCall $1 $3 $5 } classinstancecreationexpression : NEW classtype LBRACE RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { } From 1e59ba9e277f291285983f21347a605deefa3811 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 28 May 2024 23:47:34 +0200 Subject: [PATCH 05/12] parser implement constructor with statements --- Test/TestParser.hs | 8 ++++++++ src/Parser/JavaParser.y | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 63f4dc1..8e8db60 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -52,6 +52,12 @@ testClassWithMethodAndField = TestCase $ testClassWithConstructor = TestCase $ assertEqual "expect class with constructor" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithParams = TestCase $ + assertEqual "expect constructor with params" [Class "WithParams" [MethodDeclaration "void" "" [ParameterDeclaration "int" "p1"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParams",LBRACKET,IDENTIFIER "WithParams",LBRACE,INT,IDENTIFIER "p1",RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithStatements = TestCase $ + assertEqual "expect constructor with statement" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [Return Nothing])] []] $ + parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RETURN,SEMICOLON,RBRACKET,RBRACKET] testEmptyBlock = TestCase $ assertEqual "expect empty block" [Block []] $ parseStatement [LBRACKET,RBRACKET] @@ -220,6 +226,8 @@ tests = TestList [ testEmptyMethodWithParams, testClassWithMethodAndField, testClassWithConstructor, + testConstructorWithParams, + testConstructorWithStatements, testEmptyBlock, testBlockWithLocalVarDecl, testBlockWithMultipleLocalVarDecls, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index d51b376..45206a5 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -127,8 +127,8 @@ classorinterfacetype : simplename { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } -constructordeclaration : constructordeclarator constructorbody { case $1 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $2 } - | modifiers constructordeclarator constructorbody { case $2 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $3 } +constructordeclaration : constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $1 $2 } + | modifiers constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $2 $3 } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } @@ -138,12 +138,12 @@ methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, par block : LBRACKET RBRACKET { Block [] } | LBRACKET blockstatements RBRACKET { Block $2 } -constructordeclarator : simplename LBRACE RBRACE { ($1, []) } - | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } +constructordeclarator : simplename LBRACE RBRACE { [] } + | simplename LBRACE formalparameterlist RBRACE { $3 } constructorbody : LBRACKET RBRACKET { Block [] } -- | LBRACKET explicitconstructorinvocation RBRACKET { } - -- | LBRACKET blockstatements RBRACKET { } + | LBRACKET blockstatements RBRACKET { Block $2 } -- | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } methodheader : type methoddeclarator { ($1, $2) } From 25c0c331091e90e8fcd071e534cdf75a910e9ef3 Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:00:12 +0200 Subject: [PATCH 06/12] make UnaryOperation a statementexpression --- src/Ast.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Ast.hs b/src/Ast.hs index a20b8e8..9fb1cea 100644 --- a/src/Ast.hs +++ b/src/Ast.hs @@ -24,10 +24,7 @@ data StatementExpression | ConstructorCall DataType [Expression] | MethodCall Expression Identifier [Expression] | TypedStatementExpression DataType StatementExpression - | PostIncrement Expression - | PostDecrement Expression - | PreIncrement Expression - | PreDecrement Expression + | UnaryOperation UnaryOperator Expression deriving (Show, Eq) data BinaryOperator @@ -53,6 +50,10 @@ data BinaryOperator data UnaryOperator = Not | Minus + | PostIncrement Expression + | PostDecrement Expression + | PreIncrement Expression + | PreDecrement Expression deriving (Show, Eq) data Expression @@ -64,7 +65,6 @@ data Expression | LocalVariable Identifier | FieldVariable Identifier | BinaryOperation BinaryOperator Expression Expression - | UnaryOperation UnaryOperator Expression | StatementExpressionExpression StatementExpression | TypedExpression DataType Expression deriving (Show, Eq) From 56cc1a93744ab85d49282fe333f94e50429e68e8 Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:02:53 +0200 Subject: [PATCH 07/12] remove expression from unary operators --- src/Ast.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Ast.hs b/src/Ast.hs index 9fb1cea..750200c 100644 --- a/src/Ast.hs +++ b/src/Ast.hs @@ -50,10 +50,10 @@ data BinaryOperator data UnaryOperator = Not | Minus - | PostIncrement Expression - | PostDecrement Expression - | PreIncrement Expression - | PreDecrement Expression + | PostIncrement + | PostDecrement + | PreIncrement + | PreDecrement deriving (Show, Eq) data Expression From c690b0139634a89bf6c7dca6013778ee3dd892d0 Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:07:01 +0200 Subject: [PATCH 08/12] change back UnaryOperation and move it to StatementExpression --- src/Typecheck.hs | 102 ++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/Typecheck.hs b/src/Typecheck.hs index ba49157..0564ce9 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -78,22 +78,6 @@ typeCheckExpression (BinaryOperation op expr1 expr2) symtab classes = Or -> checkLogicalOperation op expr1' expr2' type1 type2 NameResolution -> resolveNameResolution expr1' expr2 symtab classes -typeCheckExpression (UnaryOperation op expr) symtab classes = - let expr' = typeCheckExpression expr symtab classes - type' = getTypeFromExpr expr' - in case op of - Not -> - if type' == "boolean" - then - TypedExpression "boolean" (UnaryOperation op expr') - else - error "Logical NOT operation requires an operand of type boolean" - Minus -> - if type' == "int" || type' == "char" - then - TypedExpression type' (UnaryOperation op expr') - else - error "Unary minus operation requires an operand of type int or char" typeCheckExpression (StatementExpressionExpression stmtExpr) symtab classes = let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes @@ -163,41 +147,61 @@ typeCheckStatementExpression (MethodCall expr methodName args) symtab classes = Nothing -> error $ "Class for object type '" ++ objType ++ "' not found." _ -> error "Invalid object type for method call. Object must have a class type." -typeCheckStatementExpression (PostIncrement expr) symtab classes = +typeCheckStatementExpression (UnaryOperation op expr) symtab classes = let expr' = typeCheckExpression expr symtab classes type' = getTypeFromExpr expr' - in if type' == "int" || type' == "char" - then - TypedStatementExpression type' (PostIncrement expr') - else - error "Post-increment operation requires an operand of type int or char" - -typeCheckStatementExpression (PostDecrement expr) symtab classes = - let expr' = typeCheckExpression expr symtab classes - type' = getTypeFromExpr expr' - in if type' == "int" || type' == "char" - then - TypedStatementExpression type' (PostDecrement expr') - else - error "Post-decrement operation requires an operand of type int or char" - -typeCheckStatementExpression (PreIncrement expr) symtab classes = - let expr' = typeCheckExpression expr symtab classes - type' = getTypeFromExpr expr' - in if type' == "int" || type' == "char" - then - TypedStatementExpression type' (PreIncrement expr') - else - error "Pre-increment operation requires an operand of type int or char" - -typeCheckStatementExpression (PreDecrement expr) symtab classes = - let expr' = typeCheckExpression expr symtab classes - type' = getTypeFromExpr expr' - in if type' == "int" || type' == "char" - then - TypedStatementExpression type' (PreDecrement expr') - else - error "Pre-decrement operation requires an operand of type int or char" + in case op of + Not -> + if type' == "boolean" + then + TypedStatementExpression "boolean" (UnaryOperation op expr') + else + error "Logical NOT operation requires an operand of type boolean" + Minus -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Unary minus operation requires an operand of type int or char" + PostIncrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Post-increment operation requires an operand of type int or char" + PostDecrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Post-decrement operation requires an operand of type int or char" + PreIncrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Pre-increment operation requires an operand of type int or char" + PreDecrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Pre-decrement operation requires an operand of type int or char" -- ********************************** Type Checking: Statements ********************************** From 761244df747909f49b3e7aa99c9181bae2118b6a Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:34:42 +0200 Subject: [PATCH 09/12] Revert "change back UnaryOperation and move it to StatementExpression" This reverts commit c690b0139634a89bf6c7dca6013778ee3dd892d0. --- src/Typecheck.hs | 102 +++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/src/Typecheck.hs b/src/Typecheck.hs index 0564ce9..ba49157 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -78,6 +78,22 @@ typeCheckExpression (BinaryOperation op expr1 expr2) symtab classes = Or -> checkLogicalOperation op expr1' expr2' type1 type2 NameResolution -> resolveNameResolution expr1' expr2 symtab classes +typeCheckExpression (UnaryOperation op expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in case op of + Not -> + if type' == "boolean" + then + TypedExpression "boolean" (UnaryOperation op expr') + else + error "Logical NOT operation requires an operand of type boolean" + Minus -> + if type' == "int" || type' == "char" + then + TypedExpression type' (UnaryOperation op expr') + else + error "Unary minus operation requires an operand of type int or char" typeCheckExpression (StatementExpressionExpression stmtExpr) symtab classes = let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes @@ -147,61 +163,41 @@ typeCheckStatementExpression (MethodCall expr methodName args) symtab classes = Nothing -> error $ "Class for object type '" ++ objType ++ "' not found." _ -> error "Invalid object type for method call. Object must have a class type." -typeCheckStatementExpression (UnaryOperation op expr) symtab classes = +typeCheckStatementExpression (PostIncrement expr) symtab classes = let expr' = typeCheckExpression expr symtab classes type' = getTypeFromExpr expr' - in case op of - Not -> - if type' == "boolean" - then - TypedStatementExpression "boolean" (UnaryOperation op expr') - else - error "Logical NOT operation requires an operand of type boolean" - Minus -> - if type' == "int" - then - TypedStatementExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedStatementExpression "char" (UnaryOperation op expr') - else - error "Unary minus operation requires an operand of type int or char" - PostIncrement -> - if type' == "int" - then - TypedStatementExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedStatementExpression "char" (UnaryOperation op expr') - else - error "Post-increment operation requires an operand of type int or char" - PostDecrement -> - if type' == "int" - then - TypedStatementExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedStatementExpression "char" (UnaryOperation op expr') - else - error "Post-decrement operation requires an operand of type int or char" - PreIncrement -> - if type' == "int" - then - TypedStatementExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedStatementExpression "char" (UnaryOperation op expr') - else - error "Pre-increment operation requires an operand of type int or char" - PreDecrement -> - if type' == "int" - then - TypedStatementExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedStatementExpression "char" (UnaryOperation op expr') - else - error "Pre-decrement operation requires an operand of type int or char" + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PostIncrement expr') + else + error "Post-increment operation requires an operand of type int or char" + +typeCheckStatementExpression (PostDecrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PostDecrement expr') + else + error "Post-decrement operation requires an operand of type int or char" + +typeCheckStatementExpression (PreIncrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PreIncrement expr') + else + error "Pre-increment operation requires an operand of type int or char" + +typeCheckStatementExpression (PreDecrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PreDecrement expr') + else + error "Pre-decrement operation requires an operand of type int or char" -- ********************************** Type Checking: Statements ********************************** From d4f474ba543d905f0ebbbdad277ff19b323eef60 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Fri, 31 May 2024 10:34:47 +0200 Subject: [PATCH 10/12] fix increment --- Test/TestParser.hs | 61 ++++++++++++++++++++++++++++++++++++---- src/Parser/JavaParser.y | 62 ++++++++++++++++++++--------------------- 2 files changed, 87 insertions(+), 36 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 873f438..090fac1 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -52,6 +52,12 @@ testClassWithMethodAndField = TestCase $ testClassWithConstructor = TestCase $ assertEqual "expect class with constructor" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithParams = TestCase $ + assertEqual "expect constructor with params" [Class "WithParams" [MethodDeclaration "void" "" [ParameterDeclaration "int" "p1"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParams",LBRACKET,IDENTIFIER "WithParams",LBRACE,INT,IDENTIFIER "p1",RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithStatements = TestCase $ + assertEqual "expect constructor with statement" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [Return Nothing])] []] $ + parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RETURN,SEMICOLON,RBRACKET,RBRACKET] testEmptyBlock = TestCase $ assertEqual "expect empty block" [Block []] $ parseStatement [LBRACKET,RBRACKET] @@ -133,16 +139,16 @@ testExpressionOr = TestCase $ assertEqual "expect or expression" (BinaryOperation Or (Reference "bar") (Reference "baz")) $ parseExpression [IDENTIFIER "bar",OR,IDENTIFIER "baz"] testExpressionPostIncrement = TestCase $ - assertEqual "expect PostIncrement" (UnaryOperation PostIncrement (Reference "a")) $ + assertEqual "expect PostIncrement" (StatementExpressionExpression $ PostIncrement (Reference "a")) $ parseExpression [IDENTIFIER "a",INCREMENT] testExpressionPostDecrement = TestCase $ - assertEqual "expect PostDecrement" (UnaryOperation PostDecrement (Reference "a")) $ + assertEqual "expect PostDecrement" (StatementExpressionExpression $ PostDecrement (Reference "a")) $ parseExpression [IDENTIFIER "a",DECREMENT] testExpressionPreIncrement = TestCase $ - assertEqual "expect PreIncrement" (UnaryOperation PreIncrement (Reference "a")) $ + assertEqual "expect PreIncrement" (StatementExpressionExpression $ PreIncrement (Reference "a")) $ parseExpression [INCREMENT,IDENTIFIER "a"] testExpressionPreDecrement = TestCase $ - assertEqual "expect PreIncrement" (UnaryOperation PreDecrement (Reference "a")) $ + assertEqual "expect PreIncrement" (StatementExpressionExpression $ PreDecrement (Reference "a")) $ parseExpression [DECREMENT,IDENTIFIER "a"] testExpressionAssign = TestCase $ assertEqual "expect assign 5 to a" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $ @@ -159,6 +165,32 @@ testExpressionPlusEqual = TestCase $ testExpressionMinusEqual = TestCase $ assertEqual "expect assign and subtraction" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Subtraction (Reference "a") (IntegerLiteral 5)))) $ parseExpression [IDENTIFIER "a",MINUSEQUAL,INTEGERLITERAL 5] +testExpressionThis = TestCase $ + assertEqual "expect this" (Reference "this") $ + parseExpression [THIS] +testExpressionBraced = TestCase $ + assertEqual "expect braced expresssion" (BinaryOperation Multiplication (Reference "b") (BinaryOperation Addition (Reference "a") (IntegerLiteral 3))) $ + parseExpression [IDENTIFIER "b",TIMES,LBRACE,IDENTIFIER "a",PLUS,INTEGERLITERAL 3,RBRACE] + +testExpressionPrecedence = TestCase $ + assertEqual "expect times to be inner expression" (BinaryOperation Addition (BinaryOperation Multiplication (Reference "b") (Reference "a")) (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "b",TIMES,IDENTIFIER "a",PLUS,INTEGERLITERAL 3] + +testExpressionMethodCallNoParams = TestCase $ + assertEqual "expect methodcall no params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ + parseExpression [IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionMethodCallOneParam = TestCase $ + assertEqual "expect methodcall one param" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a"])) $ + parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",RBRACE] +testExpressionMethodCallTwoParams = TestCase $ + assertEqual "expect methocall two params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a", IntegerLiteral 5])) $ + parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",COMMA,INTEGERLITERAL 5,RBRACE] +testExpressionThisMethodCall = TestCase $ + assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ + parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionThisMethodCallParam = TestCase $ + assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "x"])) $ + parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,IDENTIFIER "x",RBRACE] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -169,6 +201,13 @@ testStatementIfThenElse = TestCase $ testStatementWhile = TestCase $ assertEqual "expect while" [While (Reference "a") (Block [Block []])] $ parseStatement [WHILE,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] +testStatementAssign = TestCase $ + assertEqual "expect assign 5" [StatementExpressionStatement (Assignment (Reference "a") (IntegerLiteral 5))] $ + parseStatement [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5,SEMICOLON] + +testStatementMethodCallNoParams = TestCase $ + assertEqual "expect methodcall statement no params" [StatementExpressionStatement (MethodCall (Reference "this") "foo" [])] $ + parseStatement [IDENTIFIER "foo",LBRACE,RBRACE,SEMICOLON] @@ -187,6 +226,8 @@ tests = TestList [ testEmptyMethodWithParams, testClassWithMethodAndField, testClassWithConstructor, + testConstructorWithParams, + testConstructorWithStatements, testEmptyBlock, testBlockWithLocalVarDecl, testBlockWithMultipleLocalVarDecls, @@ -218,7 +259,17 @@ tests = TestList [ testExpressionDivideEqual, testExpressionPlusEqual, testExpressionMinusEqual, + testExpressionBraced, + testExpressionThis, + testExpressionPrecedence, + testExpressionMethodCallNoParams, + testExpressionMethodCallOneParam, + testExpressionMethodCallTwoParams, + testExpressionThisMethodCall, + testExpressionThisMethodCallParam, testStatementIfThen, testStatementIfThenElse, - testStatementWhile + testStatementWhile, + testStatementAssign, + testStatementMethodCallNoParams ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 4e66850..21d7be1 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -127,8 +127,8 @@ classorinterfacetype : simplename { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } -constructordeclaration : constructordeclarator constructorbody { case $1 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $2 } - | modifiers constructordeclarator constructorbody { case $2 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $3 } +constructordeclaration : constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $1 $2 } + | modifiers constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $2 $3 } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } @@ -138,12 +138,12 @@ methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, par block : LBRACKET RBRACKET { Block [] } | LBRACKET blockstatements RBRACKET { Block $2 } -constructordeclarator : simplename LBRACE RBRACE { ($1, []) } - | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } +constructordeclarator : simplename LBRACE RBRACE { [] } + | simplename LBRACE formalparameterlist RBRACE { $3 } constructorbody : LBRACKET RBRACKET { Block [] } -- | LBRACKET explicitconstructorinvocation RBRACKET { } - -- | LBRACKET blockstatements RBRACKET { } + | LBRACKET blockstatements RBRACKET { Block $2 } -- | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } methodheader : type methoddeclarator { ($1, $2) } @@ -189,8 +189,8 @@ blockstatement : localvariabledeclarationstatement { $1 } -- expected ty formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } -argumentlist : expression { } - | argumentlist COMMA expression { } +argumentlist : expression { [$1] } + | argumentlist COMMA expression { $1 ++ [$3] } numerictype : integraltype { $1 } @@ -215,7 +215,7 @@ localvariabledeclaration : type variabledeclarators { map LocalVariableDeclarati statementwithouttrailingsubstatement : block { [$1] } | emptystatement { [] } - -- | expressionstatement { } + | expressionstatement { [$1] } | returnstatement { [$1] } ifthenstatement : IF LBRACE expression RBRACE statement { If $3 (Block $5) Nothing } @@ -229,7 +229,7 @@ assignmentexpression : conditionalexpression { $1 } emptystatement : SEMICOLON { Block [] } -expressionstatement : statementexpression SEMICOLON { } +expressionstatement : statementexpression SEMICOLON { StatementExpressionStatement $1 } returnstatement : RETURN SEMICOLON { Return Nothing } | RETURN expression SEMICOLON { Return $ Just $2 } @@ -248,13 +248,13 @@ assignment : lefthandside assignmentoperator assignmentexpression { } -statementexpression : assignment { } - | preincrementexpression { } - | predecrementexpression { } - | postincrementexpression { } - | postdecrementexpression { } - | methodinvocation { } - | classinstancecreationexpression { } +statementexpression : assignment { $1 } + -- | preincrementexpression { } + -- | predecrementexpression { } + -- | postincrementexpression { } + -- | postdecrementexpression { } + | methodinvocation { $1 } + -- | classinstancecreationexpression { } ifthenelsestatementnoshortif :IF LBRACE expression RBRACE statementnoshortif ELSE statementnoshortif { } @@ -279,18 +279,18 @@ assignmentoperator : ASSIGN { Nothing } | XOREQUAL { Just BitwiseXor } | OREQUAL{ Just BitwiseOr } -preincrementexpression : INCREMENT unaryexpression { UnaryOperation PreIncrement $2 } +preincrementexpression : INCREMENT unaryexpression { PreIncrement $2 } -predecrementexpression : DECREMENT unaryexpression { UnaryOperation PreDecrement $2 } +predecrementexpression : DECREMENT unaryexpression { PreDecrement $2 } -postincrementexpression : postfixexpression INCREMENT { UnaryOperation PostIncrement $1 } +postincrementexpression : postfixexpression INCREMENT { PostIncrement $1 } -postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecrement $1 } +postdecrementexpression : postfixexpression DECREMENT { PostDecrement $1 } -methodinvocation : name LBRACE RBRACE { } - | name LBRACE argumentlist RBRACE { } - | primary DOT IDENTIFIER LBRACE RBRACE { } - | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } +methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } + | simplename LBRACE argumentlist RBRACE { MethodCall (Reference "this") $1 $3 } + | primary DOT IDENTIFIER LBRACE RBRACE { MethodCall $1 $3 [] } + | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { MethodCall $1 $3 $5 } classinstancecreationexpression : NEW classtype LBRACE RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { } @@ -300,15 +300,15 @@ conditionalandexpression : inclusiveorexpression { $1 } fieldaccess : primary DOT IDENTIFIER { } unaryexpression : unaryexpressionnotplusminus { $1 } - | predecrementexpression { $1 } + | predecrementexpression { StatementExpressionExpression $1 } | PLUS unaryexpression { $2 } | MINUS unaryexpression { UnaryOperation Minus $2 } - | preincrementexpression { $1 } + | preincrementexpression { StatementExpressionExpression $1 } postfixexpression : primary { $1 } | name { $1 } - | postincrementexpression { $1 } - | postdecrementexpression{ $1 } + | postincrementexpression { StatementExpressionExpression $1 } + | postdecrementexpression { StatementExpressionExpression $1 } primary : primarynonewarray { $1 } @@ -316,11 +316,11 @@ inclusiveorexpression : exclusiveorexpression { $1 } | inclusiveorexpression OR exclusiveorexpression { BinaryOperation Or $1 $3 } primarynonewarray : literal { $1 } - -- | THIS { } - -- | LBRACE expression RBRACE { } + | THIS { Reference "this" } + | LBRACE expression RBRACE { $2 } -- | classinstancecreationexpression { } -- | fieldaccess { } - -- | methodinvocation { } + | methodinvocation { StatementExpressionExpression $1 } unaryexpressionnotplusminus : postfixexpression { $1 } -- | TILDE unaryexpression { } From 8cf022e6e07d66df90313b01bb035917b97fe724 Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:34:48 +0200 Subject: [PATCH 11/12] Revert "remove expression from unary operators" This reverts commit 56cc1a93744ab85d49282fe333f94e50429e68e8. --- src/Ast.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Ast.hs b/src/Ast.hs index 750200c..9fb1cea 100644 --- a/src/Ast.hs +++ b/src/Ast.hs @@ -50,10 +50,10 @@ data BinaryOperator data UnaryOperator = Not | Minus - | PostIncrement - | PostDecrement - | PreIncrement - | PreDecrement + | PostIncrement Expression + | PostDecrement Expression + | PreIncrement Expression + | PreDecrement Expression deriving (Show, Eq) data Expression From 4c82f5bfdd7062f8bfd0a06bdd7e458caf5671eb Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:34:52 +0200 Subject: [PATCH 12/12] Revert "make UnaryOperation a statementexpression" This reverts commit 25c0c331091e90e8fcd071e534cdf75a910e9ef3. --- src/Ast.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Ast.hs b/src/Ast.hs index 9fb1cea..a20b8e8 100644 --- a/src/Ast.hs +++ b/src/Ast.hs @@ -24,7 +24,10 @@ data StatementExpression | ConstructorCall DataType [Expression] | MethodCall Expression Identifier [Expression] | TypedStatementExpression DataType StatementExpression - | UnaryOperation UnaryOperator Expression + | PostIncrement Expression + | PostDecrement Expression + | PreIncrement Expression + | PreDecrement Expression deriving (Show, Eq) data BinaryOperator @@ -50,10 +53,6 @@ data BinaryOperator data UnaryOperator = Not | Minus - | PostIncrement Expression - | PostDecrement Expression - | PreIncrement Expression - | PreDecrement Expression deriving (Show, Eq) data Expression @@ -65,6 +64,7 @@ data Expression | LocalVariable Identifier | FieldVariable Identifier | BinaryOperation BinaryOperator Expression Expression + | UnaryOperation UnaryOperator Expression | StatementExpressionExpression StatementExpression | TypedExpression DataType Expression deriving (Show, Eq)