Skip to content

Commit

Permalink
Gruppe 8: SymbolTableBuilder Integration Tests (#63)
Browse files Browse the repository at this point in the history
Added new integration tests for additive expression, atomic expression,
compare expression, logical expression, multiplicative expression and
prefix expression
  • Loading branch information
TadaXD20 committed Jun 11, 2024
1 parent 096b4ab commit 5acab10
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ExprResult visitLogicalExpr(ASTLogicalExprNode node) {
for (int i = 1; i < node.operands().size(); i++){
ExprResult right = visit(node.operands().get(i));
if (!left.getType().equals(right.getType())){
throw new RuntimeException("Type mismatch in logical expression");
throw new SemaError(node, "Type mismatch in logical expression");
}
}
Type resultType = left.getType();
Expand All @@ -90,7 +90,7 @@ public ExprResult visitAdditiveExpr(ASTAdditiveExprNode node) {
for (int i = 1; i < node.operands().size(); i++){
ExprResult right = visit(node.operands().get(i));
if(!left.getType().equals(right.getType())){
throw new RuntimeException("Type mismatch in additive expression");
throw new SemaError(node, "Type mismatch in additive expression");
}
}
Type resultType = left.getType();
Expand All @@ -116,7 +116,7 @@ public ExprResult visitCompareExpr(ASTCompareExprNode node) {
ExprResult right = visit(node.operands().get(1));

if(!left.getType().equals(right.getType())){
throw new RuntimeException("Type mismatch in compare expression");
throw new SemaError(node, "Type mismatch in compare expression");
}
return new ExprResult(node.setEvaluatedSymbolType(new Type(SuperType.TY_BOOL)));
}
Expand Down Expand Up @@ -162,7 +162,7 @@ public ExprResult visitMultiplicativeExpr(ASTMultiplicativeExprNode node) {
for (int i = 1; i < node.operands().size(); i++){
ExprResult right = visit(node.operands().get(i));
if(!left.getType().equals(right.getType())){
throw new RuntimeException("Type mismatch in multiplicative expression");
throw new SemaError(node, "Type mismatch in multiplicative expression");
}
}
Type resultType = left.getType();
Expand All @@ -178,7 +178,7 @@ public ExprResult visitPrefixExpr(ASTPrefixExprNode node) {
return new ExprResult(node.setEvaluatedSymbolType(resultType));
}
else {
throw new RuntimeException("Type mismatch in prefix expression");
throw new SemaError(node, "Type mismatch in prefix expression");
}
}
return left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,4 +75,17 @@ void additiveExprIntegrationTest() {
assertEquals(ASTAtomicExprNode.AtomicType.INT_LIT, additiveExpr.operands().getLast().operands().getFirst().operand().getExprType());
assertEquals(2, additiveExpr.operands().getLast().operands().getFirst().operand().getIntLit());
}

@Test
@DisplayName("Integration test")
void integrationTestSymbolTableBuilder() {
String code = "1 + 2";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);
ASTAdditiveExprNode additiveExpr = parser.parseAdditiveExpression();
SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

symbolTableBuilder.visitAdditiveExpr(additiveExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -60,4 +61,17 @@ void atomicExprIntegrationTest() {
assertEquals(ASTAtomicExprNode.AtomicType.STRING_LIT, atomicExpr.getExprType());
assertEquals("Roethig", atomicExpr.getStringLit());
}

@Test
@DisplayName("Integration test")
void integrationTestSymbolTableBuilder() {
String code = "\"Roethig\"";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);
ASTAtomicExprNode atomicExpr = parser.parseAtomicExpression();
SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

symbolTableBuilder.visitAtomicExpr(atomicExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,4 +75,17 @@ void compareExprIntegrationTest() {
assertEquals(ASTAtomicExprNode.AtomicType.INT_LIT, compareExpr.operands().getLast().operands().getFirst().operands().getFirst().operand().getExprType());
assertEquals(3, compareExpr.operands().getLast().operands().getFirst().operands().getFirst().operand().getIntLit());
}

@Test
@DisplayName("Integration test")
void integrationTestSymbolTableBuilder() {
String code = "2 != 3";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);
ASTCompareExprNode compareExpr = parser.parseCompareExpression();
SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

symbolTableBuilder.visitCompareExpr(compareExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,4 +75,17 @@ void logicalExprIntegrationTest() {
assertEquals(ASTAtomicExprNode.AtomicType.IDENTIFIER, logicalExpr.operands().getLast().operands().getFirst().operands().getFirst().operands().getFirst().operand().getExprType());
assertEquals("b", logicalExpr.operands().getLast().operands().getFirst().operands().getFirst().operands().getFirst().operand().getIdentifier());
}

@Test
@DisplayName("Integration test")
void integrationTestSymbolTableBuilder() {
String code = "a && b";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);
ASTLogicalExprNode logicalExpr = parser.parseLogicalExpression();
SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

symbolTableBuilder.visitLogicalExpr(logicalExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,4 +75,17 @@ void multiplicativeExprIntegrationTest() {
assertEquals(ASTAtomicExprNode.AtomicType.INT_LIT, multiplicativeExpr.operands().getLast().operand().getExprType());
assertEquals(3, multiplicativeExpr.operands().getLast().operand().getIntLit());
}

@Test
@DisplayName("Integration test")
void integrationTestSymbolTableBuilder() {
String code = "2 * 3";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);
ASTMultiplicativeExprNode multiplicativeExpr = parser.parseMultiplicativeExpression();
SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

symbolTableBuilder.visitMultiplicativeExpr(multiplicativeExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.auberer.compilerdesignlectureproject.lexer.TokenType;
import com.auberer.compilerdesignlectureproject.reader.CodeLoc;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import com.auberer.compilerdesignlectureproject.sema.SymbolTableBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -71,4 +72,17 @@ void prefixExprIntegrationTest() {
assertEquals(ASTAtomicExprNode.AtomicType.INT_LIT, prefixExpr.operand().getExprType());
assertEquals(4, prefixExpr.operand().getIntLit());
}

@Test
@DisplayName("Integration test")
void integrationTestSymbolTableBuilder() {
String code = "-4";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);
ASTPrefixExprNode prefixExpr = parser.parsePrefixExpression();
SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

symbolTableBuilder.visitPrefixExpr(prefixExpr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.auberer.compilerdesignlectureproject.sema;

import com.auberer.compilerdesignlectureproject.ast.*;
import com.auberer.compilerdesignlectureproject.lexer.Lexer;
import com.auberer.compilerdesignlectureproject.parser.Parser;
import com.auberer.compilerdesignlectureproject.reader.Reader;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class AdditiveExpressionNodeTest {
@Test
@DisplayName("Integration test")
void additiveExprIntegrationTest() {
String code = """
1 + 2
""";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);

SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();

ASTAdditiveExprNode astAdditiveExprNode = parser.parseAdditiveExpression();
symbolTableBuilder.visitAdditiveExpr(astAdditiveExprNode);

assertNotNull(astAdditiveExprNode);
assertInstanceOf(ASTAdditiveExprNode.class, astAdditiveExprNode);
assertEquals(ASTAtomicExprNode.AtomicType.INT_LIT, astAdditiveExprNode.operands().getFirst().operands().getFirst().operand().getExprType());
assertEquals(1, astAdditiveExprNode.operands().getFirst().operands().getFirst().operand().getIntLit());
assertEquals(ASTAdditiveExprNode.AdditiveOperator.PLUS, astAdditiveExprNode.operatorList.getFirst());
assertEquals(ASTAtomicExprNode.AtomicType.INT_LIT, astAdditiveExprNode.operands().getLast().operands().getFirst().operand().getExprType());
assertEquals(2, astAdditiveExprNode.operands().getLast().operands().getFirst().operand().getIntLit());
}

@Test
@DisplayName("Integration test should throw SemaError with wrong operand type")
void additiveExprIntegrationTestWrongType() {
String code = """
1 + "a"
""";
Reader reader = new Reader(code);
Lexer lexer = new Lexer(reader, false);
Parser parser = new Parser(lexer);

SymbolTableBuilder symbolTableBuilder = new SymbolTableBuilder();
TypeChecker typeChecker = new TypeChecker();

ASTAdditiveExprNode astAdditiveExprNode = parser.parseAdditiveExpression();
symbolTableBuilder.visitAdditiveExpr(astAdditiveExprNode);

assertNotNull(astAdditiveExprNode);
assertInstanceOf(ASTAdditiveExprNode.class, astAdditiveExprNode);
assertEquals(ASTAdditiveExprNode.AdditiveOperator.PLUS, astAdditiveExprNode.operatorList.getFirst());

SemaError exception = Assertions.assertThrows(SemaError.class, () -> typeChecker.visitAdditiveExpr(astAdditiveExprNode));
assertTrue(exception.getMessage().contains("Type mismatch in additive expression"));

}
}
Loading

0 comments on commit 5acab10

Please sign in to comment.