Commit cbd872ae authored by Kevin Wu's avatar Kevin Wu

Add some nodes

parent 166556f6
package io.kwu.kythera.parser;
public enum Operator {
EQUALS("=", 1),
PLUS_EQUALS("+=", 1),
MINUS_EQUALS("-=", 1),
TIMES_EQUALS("*=", 1),
DIV_EQUALS("/=", 1),
MOD_EQUALS("%=", 1),
OR_LOGICAL("||", 3),
AND_LOGICAL("&&", 4),
// TODO bitwise boolean operators
EQUIVALENT("==", 8),
NOT_EQUIV("!=", 8),
LESS_THAN("<", 9),
GREATER_THAN(">", 9),
LESS_EQUALS("<=", 9),
GREATER_EQUALS(">=", 9),
// TODO bit shift operators
PLUS("+", 11),
MINUS("-", 11),
TIMES("*", 12),
DIVIDE("/", 12),
MODULUS("%", 12),
NOT("!", 14);
public final String symbol;
public final int precedence; // higher precedence evaluated first
Operator(String symbol, int precedence) {
this.symbol = symbol;
this.precedence = precedence;
}
}
package io.kwu.kythera.parser;
public class ParserException extends Exception {
public ParserException(String message) {
System.err.println("Error: " + message);
}
public ParserException(String message, int line, int col) {
System.err.println("Error: " + message + " at " + line + "," + col);
}
}
package io.kwu.kythera.parser;
// placeholder until type system can be decided on
public enum Type {
}
package io.kwu.kythera.parser.node;
public class Assignment {
}
package io.kwu.kythera.parser.node;
import io.kwu.kythera.parser.Operator;
public class BinaryNode extends ExpressionNode {
public final Operator op;
public final ExpressionNode left;
public final ExpressionNode right;
public BinaryNode(Operator op, ExpressionNode left, ExpressionNode right) {
super(NodeKind.BINARY, null);
this.op = op;
this.left = left;
this.right = right;
// TODO type check left and right?
this.type = left.type;
}
}
package io.kwu.kythera.parser.node;
import io.kwu.kythera.parser.ParserException;
import io.kwu.kythera.parser.Type;
// ExpressionNodes are statements that evaluate to a value and therefore also have a type.
public abstract class ExpressionNode extends StatementNode {
public Type type; // may need to be set after ExpressionNode is instantiated
ExpressionNode(NodeKind kind, Type type) throws ParserException {
super(kind);
this.type = type;
}
}
package io.kwu.kythera.parser.node;
public enum NodeKind {
UNARY,
BINARY,
ASSIGN,
LITERAL,
TYPE,
IDENTIFIER,
TYPEOF,
NEW,
LET,
IF,
WHILE,
RETURN,
AS,
CALL,
ACCESS,
THIS,
}
package io.kwu.kythera.parser.node;
import io.kwu.kythera.parser.ParserException;
// All nodes extend StatementNode. Statements have no type, so neither do StatementNodes.
// Very few StatementNodes are /not/ ParseNodes. Examples include "let" and "return".
// TODO this could be a good place to send line/col numbers for debugging
public abstract class StatementNode {
public final NodeKind kind;
public StatementNode(NodeKind kind) throws ParserException {
this.kind = kind;
}
}
package io.kwu.kythera.parser.node;
import io.kwu.kythera.parser.Operator;
import io.kwu.kythera.parser.ParserException;
public class UnaryNode extends ExpressionNode {
public final Operator operator;
public final ExpressionNode target;
public UnaryNode(Operator op, ExpressionNode target) throws ParserException {
super(NodeKind.UNARY, target.type);
if(op != Operator.NOT) {
throw new ParserException("Invalid operator: " + op.symbol);
}
this.operator = op;
this.target = target;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment