Skip to content

Commit

Permalink
Add support for comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
dilanSachi committed Feb 4, 2025
1 parent f9fa18b commit 432ddbe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/main/java/fi/helsinki/compiler/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ private Expression parseIfBlock() throws ParserException {

private Expression parseExpression() throws ParserException {
Expression left = parseTerm();
while (Arrays.asList("+", "-").contains(peek().getText())) {
Token operatorToken = consume();
Expression right = parseTerm();
while (Arrays.asList("=", "or", "and", "==", "!=", "<", "<=", ">", ">=").contains(peek().getText())) {
Token operatorToken = consume();
Expression right = parseExpression();
left = new BinaryOp(left, operatorToken, right);
}
while (Arrays.asList("=", "and", "or", "==", "!=", "<", "<=", ">", ">=").contains(peek().getText())) {
while (Arrays.asList("+", "-").contains(peek().getText())) {
Token operatorToken = consume();
Expression right = parseExpression();
Expression right = parseTerm();
left = new BinaryOp(left, operatorToken, right);
}
return left;
Expand Down Expand Up @@ -178,9 +178,9 @@ public Block parse2() throws ParserException {
consume(";");
nextToken = peek();
}
if (checkNextToken(TokenType.OPERATOR, Optional.of("="))) {
parseExpressionWithRightAssociativity();
}
// if (checkNextToken(TokenType.OPERATOR, Optional.of("="))) {
// parseExpressionWithRightAssociativity();
// }
}
if (tokenPosition < tokens.size()) {
throw new ParserException("Parsing failed. Invalid tokens found: " +
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/fi/helsinki/compiler/parser/ParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,46 @@ public void testDoubleEqualOperator() throws ParserException {
}

@Test
public void testComparisonOperatorWithConditional() throws ParserException {
Tokenizer tokenizer = new Tokenizer();
Parser testParser = new Parser(tokenizer.tokenize(
"if a > 2 then b = c + d else if e == 3 then f = g * h else i = j - k", "Testfile.dl"));
Block block = testParser.parse2();
assertEquals(block.getExpressionList().size(), 1);
ConditionalOp conditionalOp = (ConditionalOp) block.getExpressionList().get(0);
BinaryOp condition = (BinaryOp) conditionalOp.getCondition();
BinaryOp thenBlock = (BinaryOp) conditionalOp.getThenBlock();
ConditionalOp elseBlock = (ConditionalOp) conditionalOp.getElseBlock();
assertEquals(((Identifier) condition.getLeft()).getName(), "a");
assertEquals(condition.getOperatorToken().getText(), ">");
assertEquals(((Literal) condition.getRight()).getValue(), 2);
assertEquals(((Identifier) thenBlock.getLeft()).getName(), "b");
assertEquals(thenBlock.getOperatorToken().getText(), "=");
BinaryOp binaryOp = (BinaryOp) thenBlock.getRight();
assertEquals(((Identifier) binaryOp.getLeft()).getName(), "c");
assertEquals(binaryOp.getOperatorToken().getText(), "+");
assertEquals(((Identifier) binaryOp.getRight()).getName(), "d");
condition = (BinaryOp) elseBlock.getCondition();
thenBlock = (BinaryOp) elseBlock.getThenBlock();
assertEquals(((Identifier) condition.getLeft()).getName(), "e");
assertEquals(condition.getOperatorToken().getText(), "==");
assertEquals(((Literal) condition.getRight()).getValue(), 3);
assertEquals(((Identifier) thenBlock.getLeft()).getName(), "f");
assertEquals(thenBlock.getOperatorToken().getText(), "=");
binaryOp = (BinaryOp) thenBlock.getRight();
assertEquals(((Identifier) binaryOp.getLeft()).getName(), "g");
assertEquals(binaryOp.getOperatorToken().getText(), "*");
assertEquals(((Identifier) binaryOp.getRight()).getName(), "h");
BinaryOp elseBlock2 = (BinaryOp) elseBlock.getElseBlock();
assertEquals(((Identifier) elseBlock2.getLeft()).getName(), "i");
assertEquals(elseBlock2.getOperatorToken().getText(), "=");
binaryOp = (BinaryOp) elseBlock2.getRight();
assertEquals(((Identifier) binaryOp.getLeft()).getName(), "j");
assertEquals(binaryOp.getOperatorToken().getText(), "-");
assertEquals(((Identifier) binaryOp.getRight()).getName(), "k");
}

@Test @Disabled
public void testMultipleComparisonOperators() throws ParserException {
Tokenizer tokenizer = new Tokenizer();
Parser testParser = new Parser(tokenizer.tokenize(
Expand Down

0 comments on commit 432ddbe

Please sign in to comment.