gombaila/src/main/java/ch/fritteli/gombaila/domain/common/TokenType.java

59 lines
1.1 KiB
Java

package ch.fritteli.gombaila.domain.common;
import io.vavr.control.Option;
import org.jetbrains.annotations.NotNull;
public enum TokenType {
// Keywords
LET,
EXIT,
PRINT,
// special characters
SEMI,
OPEN_PAREN,
CLOSE_PAREN,
EQUALS,
PLUS,
MINUS,
MULT,
DIV,
MOD,
EXP,
// literals, identifiers
INT_LIT,
IDENTIFIER,
// the rest
WHITESPACE;
public boolean isBinaryOperator() {
return switch (this) {
case PLUS, MINUS, MULT, DIV, MOD, EXP -> true;
default -> false;
};
}
@NotNull
public Option<Integer> precedence() {
return Option.of(switch (this) {
case PLUS, MINUS -> 1;
case MULT, DIV, MOD -> 2;
case EXP -> 3;
default -> null;
});
}
@NotNull
public Option<Associativity> associativity() {
return Option.of(switch (this) {
case PLUS, MINUS, MULT, DIV, MOD -> Associativity.LEFT;
case EXP -> Associativity.RIGHT;
default -> null;
});
}
public enum Associativity {
LEFT,
RIGHT
}
}