-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f1a306
commit 68d4244
Showing
7 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/fi/helsinki/compiler/exceptions/ParserException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package fi.helsinki.compiler.exceptions; | ||
|
||
public class ParserException extends Exception { | ||
|
||
public ParserException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,27 @@ | ||
package fi.helsinki.compiler.parser; | ||
|
||
import fi.helsinki.compiler.tokenizer.Token; | ||
|
||
public class BinaryOp implements Expression { | ||
private Expression left; | ||
private Token operatorToken; | ||
private Expression right; | ||
|
||
public BinaryOp(Expression left, Token operatorToken, Expression right) { | ||
this.left = left; | ||
this.operatorToken = operatorToken; | ||
this.right = right; | ||
} | ||
|
||
public Expression getLeft() { | ||
return left; | ||
} | ||
|
||
public Token getOperatorToken() { | ||
return operatorToken; | ||
} | ||
|
||
public Expression getRight() { | ||
return right; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
package fi.helsinki.compiler.parser; | ||
|
||
public class Identifier { | ||
public class Identifier implements Expression { | ||
|
||
private String name; | ||
|
||
public Identifier(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package fi.helsinki.compiler.parser; | ||
|
||
public class Literal implements Expression { | ||
private | ||
|
||
private Integer value; | ||
|
||
public Literal(Integer value) { | ||
this.value = value; | ||
} | ||
|
||
public Integer getValue() { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,78 @@ | ||
package fi.helsinki.compiler.parser; | ||
|
||
import fi.helsinki.compiler.exceptions.ParserException; | ||
import fi.helsinki.compiler.tokenizer.Token; | ||
import fi.helsinki.compiler.tokenizer.TokenType; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class Parser { | ||
|
||
public Expression parse(List<Token> tokens) { | ||
return null; | ||
private List<Token> tokens; | ||
private int tokenPosition; | ||
|
||
public Parser(List<Token> tokens) { | ||
this.tokens = tokens; | ||
this.tokenPosition = 0; | ||
} | ||
|
||
private Token peek() { | ||
if (tokenPosition == tokens.size()) { | ||
return new Token("", TokenType.END, tokens.getLast().getTokenLocation()); | ||
} | ||
return tokens.get(tokenPosition); | ||
} | ||
|
||
private Token consume(String... expected) throws ParserException { | ||
Token token = peek(); | ||
if (!Arrays.stream(expected).anyMatch(token.getText()::equals)) { | ||
String commaSeparated = Arrays.stream(expected).map(Objects::toString) | ||
.collect(Collectors.joining(", ")).toString(); | ||
throw new ParserException(token.getTokenLocation() + ": expected one of: " + commaSeparated); | ||
} | ||
tokenPosition += 1; | ||
return token; | ||
} | ||
|
||
public Literal parseIntegerLiteral() throws ParserException { | ||
if (peek().getTokenType() != TokenType.INTEGER_LITERAL) { | ||
throw new ParserException(peek().getTokenLocation() + ": expected an integer literal"); | ||
} | ||
Token token = consume(); | ||
return new Literal(Integer.valueOf(token.getText())); | ||
} | ||
|
||
public Identifier parseIdentifier() throws ParserException { | ||
if (peek().getTokenType() != TokenType.INTEGER_LITERAL) { | ||
throw new ParserException(peek().getTokenLocation() + ": expected an integer literal"); | ||
} | ||
Token token = consume(); | ||
return new Identifier(token.getText()); | ||
} | ||
|
||
public Expression parseTerm() throws ParserException { | ||
if (peek().getTokenType() == TokenType.INTEGER_LITERAL) { | ||
return parseIntegerLiteral(); | ||
} | ||
if (peek().getTokenType() == TokenType.IDENTIFIER) { | ||
return parseIdentifier(); | ||
} | ||
throw new ParserException(peek().getTokenLocation() + ": expected an integer literal or an identifier"); | ||
} | ||
|
||
public BinaryOp parseExpression() throws ParserException { | ||
Expression left = parseTerm(); | ||
while (Arrays.asList("+", "-").contains(peek().getText())) { | ||
Token operatorToken = consume(); | ||
Expression right = parseTerm(); | ||
left = new BinaryOp(left, operatorToken, right); | ||
} | ||
Token operatorToken = consume("+", "-"); | ||
Expression right = parseTerm(); | ||
return new BinaryOp(left, operatorToken, right); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ public enum TokenType { | |
IDENTIFIER, | ||
STRING_LITERAL, | ||
BOOLEAN_LITERAL, | ||
KEYWORD | ||
KEYWORD, | ||
END | ||
} |