Commit 3c196318 authored by Alexander Weigl's avatar Alexander Weigl

work on ast

parent 3f132b86
grammar ScriptLanguage;
start
: (SCRIPT name=ID '(' paramters=argList? ')' INDENT body=stmtList DEDENT)*
: (SCRIPT name=ID '(' signature=argList? ')' INDENT body=stmtList DEDENT)*
;
argList
......@@ -9,13 +9,7 @@ argList
;
varDecl
: ID ':' type
;
type
: INT
| BOOL
| TERMTYPE
: name=ID ':' type=ID
;
stmtList
......@@ -29,11 +23,11 @@ statement
| forEachStmt
| theOnlyStmt
| scriptCommand
| callStmt
// | callStmt
;
assignment
: variable=ID (COLON type)? ASSIGN expression SEMICOLON
: variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
;
expression
......@@ -99,16 +93,17 @@ theOnlyStmt
;
scriptCommand
: cmd=ID parameter* SEMICOLON
: cmd=ID parameters? SEMICOLON
;
parameters: parameter+;
parameter : ((pname=ID '=')? expr=expression);
parameter : ((pname=ID '=')? expr=expression)
;
/*
callStmt
: CALL scriptCommand SEMICOLON
;
*/
//LEXER Rules
WS : [ \t\n\r]+ -> skip ;
......@@ -125,9 +120,9 @@ TRUE : 'true' ;
FALSE : 'false' ;
CALL : 'call' ;
REPEAT : 'repeat' ;
INT : 'int' ;
/*INT : 'int' ;
BOOL: 'bool' ;
TERMTYPE : 'term' ;
TERMTYPE : 'term' ;*/
FOREACH : 'foreach' ;
THEONLY : 'theonly' ;
ID : [a-zA-Z] [_a-zA-Z0-9]* ;
......
package edu.kit.formatl.proofscriptparser;
import edu.kit.formatl.proofscriptparser.ast.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
/**
* {@link ASTChanger} provides a visitor with for replacing or substiting nodes (in situ).
*
* @author Alexander Weigl
* @version 1 (29.04.17)
*/
public class ASTChanger extends DefaultASTVisitor<ASTNode> {
@Override public ProofScript visit(ProofScript proofScript) {
proofScript.setBody((Statements) proofScript.getBody().accept(this));
return proofScript;
}
@Override public AssignmentStatement visit(AssignmentStatement assign) {
assign.setRhs((Variable) assign.getRhs().accept(this));
assign.setLhs((Expression) assign.getLhs().accept(this));
return assign;
}
@Override public Expression visit(BinaryExpression e) {
e.setLeft((Expression) e.getLeft().accept(this));
e.setRight((Expression) e.getRight().accept(this));
return e;
}
@Override public MatchExpression visit(MatchExpression match) {
if (match.getTerm() != null)
match.setTerm((TermLiteral) match.getTerm().accept(this));
return match;
}
@Override public TermLiteral visit(TermLiteral term) {
return term;
}
@Override public StringLiteral visit(StringLiteral string) {
return string;
}
@Override public Variable visit(Variable variable) {
return variable;
}
@Override public BooleanLiteral visit(BooleanLiteral bool) {
return bool;
}
@Override public Statements visit(Statements statements) {
ArrayList copy = new ArrayList<>(statements.size());
for (Statement statement : statements) {
copy.add(statement.accept(this));
}
statements.clear();
statements.addAll(copy);
return statements;
}
@Override public IntegerLiteral visit(IntegerLiteral integer) {
return integer;
}
@Override public CasesStatement visit(CasesStatement casesStatement) {
for (CaseStatement c : casesStatement.getCases()) {
c.accept(this);
}
return casesStatement;
}
@Override public CaseStatement visit(CaseStatement caseStatement) {
caseStatement.getGuard().accept(this);
caseStatement.getBody().accept(this);
return caseStatement;
}
@Override public ScriptCallStatement visit(ScriptCallStatement call) {
Map<String, Expression> p = call.getParameters();
Set<Map.Entry<String, Expression>> entries = call.getParameters().entrySet();
for (Map.Entry<String, Expression> e : entries) {
p.put(e.getKey(), (Expression) e.getValue().accept(this));
}
return call;
}
}
package edu.kit.formatl.proofscriptparser;
import edu.kit.formatl.proofscriptparser.ast.*;
/**
* {@link ASTTraversal} provides a visitor with a a default traversal of the given AST.
*
* @author Alexander Weigl
* @version 1 (29.04.17)
*/
public class ASTTraversal<T> implements Visitor<T> {
@Override public T visit(ProofScript proofScript) {
proofScript.getBody().accept(this);
return null;
}
@Override public T visit(AssignmentStatement assign) {
assign.getLhs().accept(this);
return null;
}
@Override public T visit(BinaryExpression e) {
e.getLeft().accept(this);
e.getRight().accept(this);
return null;
}
@Override public T visit(MatchExpression match) {
if (match.getTerm() != null)
match.getTerm().accept(this);
return null;
}
@Override public T visit(TermLiteral term) {
term.accept(this);
return null;
}
@Override public T visit(StringLiteral string) {
return null;
}
@Override public T visit(Variable variable) {
return null;
}
@Override public T visit(BooleanLiteral bool) {
return null;
}
@Override public T visit(Statements statements) {
for (Statement statement : statements) {
statement.accept(this);
}
return null;
}
@Override public T visit(IntegerLiteral integer) {
return null;
}
@Override public T visit(CasesStatement casesStatement) {
for (CaseStatement c : casesStatement.getCases()) {
c.accept(this);
}
return null;
}
@Override public T visit(CaseStatement caseStatement) {
caseStatement.getGuard().accept(this);
caseStatement.getBody().accept(this);
return null;
}
@Override public T visit(ScriptCallStatement call) {
for (Expression e : call.getParameters().values()) {
e.accept(this);
}
return null;
}
@Override public T visit(TheOnlyStatement theOnly) {
return null;
}
@Override public T visit(ForeachStatement foreach) {
return null;
}
@Override public T visit(RepeatStatement repeatStatement) {
return null;
}
@Override public T visit(Signature signature) {
return null;
}
@Override public T visit(Parameters parameters) {
return null;
}
}
......@@ -21,7 +21,7 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> {
s.append("script");
s.append(proofScript.getName());
s.append(" (");
printArglist(proofScript.getParameters());
proofScript.getSignature().accept(this);
s.append(") {");
proofScript.getBody().accept(this);
nl();
......@@ -29,14 +29,16 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> {
return null;
}
private void printArglist(Map<String, String> parameters) {
Iterator<Map.Entry<String, String>> iter = parameters.entrySet().iterator();
@Override public Void visit(Signature sig) {
Iterator<Map.Entry<Variable, String>> iter = sig.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> next = iter.next();
s.append(next.getKey()).append(" : ").append(next.getValue());
Map.Entry<Variable, String> next = iter.next();
next.getKey().accept(this);
s.append(" : ").append(next.getValue());
if (iter.hasNext())
s.append(", ");
}
return null;
}
@Override public Void visit(AssignmentStatement assign) {
......
......@@ -23,17 +23,17 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
ProofScript s = new ProofScript();
s.setName(ctx.name.getText());
s.setRuleContext(ctx);
if (ctx.paramters != null)
s.setParameters((Map<String, String>) ctx.paramters.accept(this));
if (ctx.signature != null)
s.setSignature((Signature) ctx.signature.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<>();
@Override public Signature visitArgList(ScriptLanguageParser.ArgListContext ctx) {
Signature signature = new Signature();
for (ScriptLanguageParser.VarDeclContext decl : ctx.varDecl()) {
signature.put(decl.ID().getText(), decl.type().getText());
signature.put(new Variable(decl.name), decl.type.getText());
}
return signature;
}
......@@ -42,10 +42,6 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
throw new IllegalStateException("not implemented");
}
@Override public Object visitType(ScriptLanguageParser.TypeContext ctx) {
throw new IllegalStateException("not implemented");
}
@Override public Statements visitStmtList(ScriptLanguageParser.StmtListContext ctx) {
Statements statements = new Statements();
for (ScriptLanguageParser.StatementContext stmt : ctx.statement()) {
......@@ -201,11 +197,17 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
@Override public Object visitForEachStmt(ScriptLanguageParser.ForEachStmtContext ctx) {
throw new IllegalStateException("not implemented");
ForeachStatement f = new ForeachStatement();
f.setRuleContext(ctx);
f.setBody((Statements) ctx.stmtList().accept(this));
return f;
}
@Override public Object visitTheOnlyStmt(ScriptLanguageParser.TheOnlyStmtContext ctx) {
throw new IllegalStateException("not implemented");
TheOnlyStatement f = new TheOnlyStatement();
f.setRuleContext(ctx);
f.setBody((Statements) ctx.stmtList().accept(this));
return f;
}
@Override public Object visitScriptCommand(ScriptLanguageParser.ScriptCommandContext ctx) {
......@@ -213,22 +215,25 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
scs.setRuleContext(ctx);
scs.setCommand(ctx.cmd.getText());
int i = 1;
if (ctx.parameter() != null) {
for (ScriptLanguageParser.ParameterContext p : ctx.parameter()) {
Expression expr = (Expression) p.expr.accept(this);
String key = p.ID() != null ? p.ID().getText() : "#" + (i++);
scs.getParameters().put(key, expr);
}
if (ctx.parameters() != null) {
ctx.parameters().accept(this);
}
return scs;
}
@Override public Object visitParameter(ScriptLanguageParser.ParameterContext ctx) {
return null;
@Override public Object visitParameters(ScriptLanguageParser.ParametersContext ctx) {
Parameters params = new Parameters();
int i = 1;
for (ScriptLanguageParser.ParameterContext pc : ctx.parameter()) {
Expression expr = (Expression) pc.expr.accept(this);
Variable key = pc.pname != null ? new Variable(pc.pname) : new Variable("#" + (i++));
params.put(key, expr);
}
return params;
}
@Override public Object visitCallStmt(ScriptLanguageParser.CallStmtContext ctx) {
throw new IllegalStateException("not implemented");
@Override public Object visitParameter(ScriptLanguageParser.ParameterContext ctx) {
return null;
}
@Override public Object visit(ParseTree parseTree) {
......
package edu.kit.formatl.proofscriptparser.ast;
package edu.kit.formatl.proofscriptparser;
/**
* @author Alexander Weigl
......
package edu.kit.formatl.proofscriptparser.ast;
package edu.kit.formatl.proofscriptparser;
import edu.kit.formatl.proofscriptparser.ast.*;
/**
* @author Alexander Weigl
......@@ -25,9 +27,19 @@ public interface Visitor<T> {
T visit(IntegerLiteral integer);
T visit(CasesStatement casesStatement);
T visit(CasesStatement cases);
T visit(CaseStatement caseStatement);
T visit(CaseStatement case_);
T visit(ScriptCallStatement call);
T visit(TheOnlyStatement theOnly);
T visit(ForeachStatement foreach);
T visit(RepeatStatement repeatStatement);
T visit(Signature signature);
T visit(Parameters parameters);
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formatl.proofscriptparser.Visitable;
import edu.kit.formatl.proofscriptparser.Visitor;
import org.antlr.v4.runtime.ParserRuleContext;
import java.util.Optional;
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.Visitor;
/**
* @author Alexander Weigl
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formatl.proofscriptparser.Visitor;
import org.antlr.v4.runtime.ParserRuleContext;
/**
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formatl.proofscriptparser.Visitor;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.Visitor;
/**
* @author Alexander Weigl
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.Visitor;
import java.util.ArrayList;
import java.util.List;
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formatl.proofscriptparser.Visitor;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
......@@ -56,4 +58,24 @@ public class DefaultASTVisitor<T> implements Visitor<T> {
@Override public T visit(ScriptCallStatement call) {
return null;
}
@Override public T visit(TheOnlyStatement theOnly) {
return null;
}
@Override public T visit(ForeachStatement foreach) {
return null;
}
@Override public T visit(RepeatStatement repeatStatement) {
return null;
}
@Override public T visit(Signature signature) {
return null;
}
@Override public T visit(Parameters parameters) {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.Visitor;
/**
* @author Alexander Weigl
* @version 1 (29.04.17)
*/
public class ForeachStatement extends GoalSelector<ScriptLanguageParser.ForEachStmtContext> {
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.ForEachStmtContext> clone() {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
/**
* @author Alexander Weigl
* @version 1 (29.04.17)
*/
public abstract class GoalSelector<T extends ParserRuleContext> extends ASTNode<T> {
private Statements body;
public Statements getBody() {
return body;
}
public GoalSelector<T> setBody(Statements body) {
this.body = body;
return this;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formatl.proofscriptparser.Visitor;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.math.BigInteger;
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.Visitor;
import java.util.Map;
......
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.Visitor;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* @author Alexander Weigl
* @version 1 (29.04.17)
*/
public class Parameters extends ASTNode<ScriptLanguageParser.ParametersContext> {
private final Map<Variable, Expression> parameters = new LinkedHashMap<>();
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.ParametersContext> clone() {
return null;
}
public int size() {
return parameters.size();
}
public boolean isEmpty() {
return parameters.isEmpty();
}
public boolean containsKey(Object key) {
return parameters.containsKey(key);
}
public boolean containsValue(Object value) {
return parameters.containsValue(value);
}