Commit 903f952b authored by Alexander Weigl's avatar Alexander Weigl

starting the boring work on an AST

parent dbf48761
......@@ -5,6 +5,8 @@ import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.ast.ProofScript;
import org.antlr.v4.runtime.*;
import java.util.List;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
......@@ -15,4 +17,9 @@ public class Facade {
return slp.start();
}
public List<ProofScript> asAST(CharStream stream) {
TransformAst astt = new TransformAst();
parseStream(stream).accept(astt);
return astt.getScripts();
}
}
package edu.kit.formatl.proofscriptparser;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formal.proofscriptparser.ScriptLanguageVisitor;
import edu.kit.formatl.proofscriptparser.ast.*;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.*;
import java.util.*;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
*/
public class TransformAst implements ScriptLanguageVisitor<Object> {
private List<ProofScript> scripts = new ArrayList<>(10);
public List<ProofScript> getScripts() {
return scripts;
}
@Override public ProofScript visitStart(ScriptLanguageParser.StartContext ctx) {
ProofScript s = new ProofScript();
s.setName(ctx.name.getText());
s.setRuleContext(ctx);
s.setParameters((Map<String, String>) ctx.paramters.accept(this));
s.setBody((Statements) ctx.body.accept(this));
scripts.add(s);
return s;
}
@Override public Map<String, String> visitArgList(ScriptLanguageParser.ArgListContext ctx) {
Map<String, String> signature = new LinkedHashMap<>();
for (ScriptLanguageParser.VarDeclContext decl : ctx.varDecl()) {
signature.put(decl.ID().getText(), decl.type().getText());
}
return signature;
}
@Override public Object visitVarDecl(ScriptLanguageParser.VarDeclContext ctx) {
return null;
}
@Override public Object visitType(ScriptLanguageParser.TypeContext ctx) {
return null;
}
@Override public Statements visitStmtList(ScriptLanguageParser.StmtListContext ctx) {
Statements statements = new Statements();
for (ScriptLanguageParser.StatementContext stmt : ctx.statement()) {
statements.add((Statement) stmt.accept(this));
}
return statements;
}
@Override public Object visitStatement(ScriptLanguageParser.StatementContext ctx) {
return ctx.getChild(0).accept(this);
}
@Override public Object visitAssignment(ScriptLanguageParser.AssignmentContext ctx) {
AssignmentStatement assign = new AssignmentStatement();
assign.setRuleContext(ctx);
assign.setRhs(ctx.variable.getText());
assign.setLhs((Expression) ctx.expression().accept(this));
return assign;
}
@Override public BinaryExpression visitExprMultiplication(ScriptLanguageParser.ExprMultiplicationContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.MULTIPLY);
}
private BinaryExpression createBinaryExpression(ParserRuleContext ctx,
List<ScriptLanguageParser.ExpressionContext> expression, Operator op) {
BinaryExpression be = new BinaryExpression();
be.setLeft((Expression) expression.get(0).accept(this));
be.setRight((Expression) expression.get(1).accept(this));
be.setOperator(op);
return be;
}
@Override public Object visitExprMinus(ScriptLanguageParser.ExprMinusContext ctx) {
return null;
}
@Override public Object visitExprNegate(ScriptLanguageParser.ExprNegateContext ctx) {
return null;
}
@Override public Object visitExprComparison(ScriptLanguageParser.ExprComparisonContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), findOperator(ctx.op.getText()));
}
private Operator findOperator(String n) {
for (Operator op : Operator.values()) {
if (op.symbol().equals(n))
return op;
}
return null;
}
@Override public Object visitExprEquality(ScriptLanguageParser.ExprEqualityContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), findOperator(ctx.op.getText()));
}
@Override public Object visitExprMatch(ScriptLanguageParser.ExprMatchContext ctx) {
return ctx.matchPattern().accept(this);
}
@Override public Object visitExprIMP(ScriptLanguageParser.ExprIMPContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.IMPLICATION);
}
@Override public Object visitExprParen(ScriptLanguageParser.ExprParenContext ctx) {
return ctx.expression().accept(this);
}
@Override public Object visitExprOr(ScriptLanguageParser.ExprOrContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.OR);
}
@Override public Object visitExprLineOperators(ScriptLanguageParser.ExprLineOperatorsContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), findOperator(ctx.op.getText()));
}
@Override public Object visitExprAnd(ScriptLanguageParser.ExprAndContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.AND);
}
@Override public Object visitExprLiterals(ScriptLanguageParser.ExprLiteralsContext ctx) {
return ctx.literals().accept(this);
}
@Override public Object visitExprDivision(ScriptLanguageParser.ExprDivisionContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.DIVISION);
}
@Override public Object visitLiteralID(ScriptLanguageParser.LiteralIDContext ctx) {
return null;
}
@Override public Object visitLiteralDigits(ScriptLanguageParser.LiteralDigitsContext ctx) {
return null;
}
@Override public Object visitLiteralTerm(ScriptLanguageParser.LiteralTermContext ctx) {
return new TermLiteral(ctx.getText());
}
@Override public Object visitLiteralString(ScriptLanguageParser.LiteralStringContext ctx) {
return new StringLiteral(ctx.getText());
}
@Override public Object visitLiteralTrue(ScriptLanguageParser.LiteralTrueContext ctx) {
return new BooleanLiteral(false, ctx.TRUE().getSymbol());
}
@Override public Object visitLiteralFalse(ScriptLanguageParser.LiteralFalseContext ctx) {
return new BooleanLiteral(false, ctx.FALSE().getSymbol());
}
@Override public Object visitMatchPattern(ScriptLanguageParser.MatchPatternContext ctx) {
MatchExpression match = new MatchExpression();
match.setRuleContext(ctx);
match.setSignature((Map<String, String>) ctx.argList().accept(this));
if (ctx.TERM_LITERAL() != null) {
match.setTerm(new TermLiteral(ctx.TERM_LITERAL().getText()));
}
else {
match.setVariable(ctx.ID().getText());
}
return match;
}
@Override public Object visitScriptVar(ScriptLanguageParser.ScriptVarContext ctx) {
return null;
}
@Override public Object visitRepeatStmt(ScriptLanguageParser.RepeatStmtContext ctx) {
return null;
}
@Override public Object visitCasesStmt(ScriptLanguageParser.CasesStmtContext ctx) {
return null;
}
@Override public Object visitCasesList(ScriptLanguageParser.CasesListContext ctx) {
return null;
}
@Override public Object visitForEachStmt(ScriptLanguageParser.ForEachStmtContext ctx) {
return null;
}
@Override public Object visitTheOnlyStmt(ScriptLanguageParser.TheOnlyStmtContext ctx) {
return null;
}
@Override public Object visitScriptCommand(ScriptLanguageParser.ScriptCommandContext ctx) {
return null;
}
@Override public Object visitCallStmt(ScriptLanguageParser.CallStmtContext ctx) {
return null;
}
@Override public Object visit(ParseTree parseTree) {
return null;
}
@Override public Object visitChildren(RuleNode ruleNode) {
return null;
}
@Override public Object visitTerminal(TerminalNode terminalNode) {
return null;
}
@Override public Object visitErrorNode(ErrorNode errorNode) {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
import java.util.Optional;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
*/
public abstract class ASTNode<T extends ParserRuleContext> implements Visitable, Cloneable {
private Optional<T> ruleContext;
private Position startPosition = new Position();
private Position endPosition = new Position();
public void setRuleContext(T c) {
startPosition = Position.from(c.getStart());
endPosition = Position.from(c.getStop());
ruleContext = Optional.of(c);
}
public Optional<T> getRuleContext() {
return ruleContext;
}
public Position getStartPosition() {
return startPosition;
}
public Position getEndPosition() {
return endPosition;
}
/**
* <p>getNodeName.</p>
*
* @return a {@link java.lang.String} object.
*/
public String getNodeName() {
return getClass().getName();
}
/**
* {@inheritDoc}
*/
public abstract <T> T accept(Visitor<T> visitor);
@Override public abstract ASTNode<T> clone();
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class AssignmentStatement extends Statement<ScriptLanguageParser.AssignmentContext> {
private String rhs;
private Expression lhs;
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.AssignmentContext> clone() {
return null;
}
public void setRhs(String rhs) {
this.rhs = rhs;
}
public String getRhs() {
return rhs;
}
public void setLhs(Expression lhs) {
this.lhs = lhs;
}
public Expression getLhs() {
return lhs;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class BinaryExpression extends Expression<ParserRuleContext> {
private Expression left,right;
private Operator operator;
public BinaryExpression setLeft(Expression left) {
this.left = left;
return this;
}
public BinaryExpression setRight(Expression right) {
this.right = right;
return this;
}
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ParserRuleContext> clone() {
return null;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public Operator getOperator() {
return operator;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import java.util.Optional;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class BooleanLiteral extends Literal {
public static final BooleanLiteral FALSE = new BooleanLiteral(false);
public static final BooleanLiteral TRUE = new BooleanLiteral(true);
public BooleanLiteral(boolean value, Token token) {
if (token != null)
this.token = Optional.of(token);
else
this.token = Optional.empty();
}
BooleanLiteral(boolean b) {
this(b, null);
}
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ParserRuleContext> clone() {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public abstract class Expression<T extends ParserRuleContext> extends ASTNode<T> {
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.graalvm.compiler.nodes.calc.IntegerDivRemNode;
import java.util.Optional;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public abstract class Literal extends ASTNode<ParserRuleContext> {
protected Optional<Token> token;
public Optional<Token> getToken() {
return token;
}
public Literal setToken(Token token) {
this.token = Optional.of(token);
return this;
}
@Override public Optional<ParserRuleContext> getRuleContext() {
return Optional.empty();
}
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import org.graalvm.compiler.core.match.MatchPattern;
import java.util.Map;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class MatchExpression extends Expression<ScriptLanguageParser.MatchPatternContext> {
private Map<String, String> signature;
private TermLiteral term;
private String variable;
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.MatchPatternContext> clone() {
return null;
}
public void setSignature(Map<String, String> signature) {
this.signature = signature;
}
public Map<String, String> getSignature() {
return signature;
}
public void setVariable(String variable) {
this.variable = variable;
}
public void setTerm(TermLiteral term) {
this.term = term;
}
public TermLiteral getTerm() {
return term;
}
}
package edu.kit.formatl.proofscriptparser.ast;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public enum Operator {
MULTIPLY("*"), DIVISION("/"), AND("&&"), OR("||"), IMPLICATION("==>");
private final String symbol;
Operator(String symbol) {
this.symbol = symbol;
}
public String symbol() {
return symbol;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.Token;
public class Position implements Cloneable {
final int lineNumber;
final int charInLine;
public Position(int lineNumber, int charInLine) {
this.lineNumber = lineNumber;
this.charInLine = charInLine;
}
public Position() {
this(-1, -1);
}
public int getLineNumber() {
return lineNumber;
}
public int getCharInLine() {
return charInLine;
}
@Override public Position clone() {
return new Position(lineNumber, charInLine);
}
@Override public String toString() {
return "@" + lineNumber + ":" + charInLine;
}
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Position position = (Position) o;
if (lineNumber != position.lineNumber)
return false;
return charInLine == position.charInLine;
}
@Override public int hashCode() {
int result = lineNumber;
result = 31 * result + charInLine;
return result;
}
public static Position from(Token token) {
return new Position(token.getLine(), token.getCharPositionInLine());
}
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import java.util.Map;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
*/
public class ProofScript extends ASTNode<ScriptLanguageParser.StartContext> {
private String name;
private Statements body;
private Map<String, String> parameters;
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.StartContext> clone() {
return null;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;