Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (102 sloc) 2.74 KB
import "ast.pil"
import "parser.pil"
import "utils.pil"
import "/Domain/tudelft.net/Users/zhemel/svn/pil/src/lib/io.pil"
import "core.pil"
import "pretty.pil"
import gel
import gelly
import gelly::utils
import gelly::rewrite
Term gelly::rewrite::replaceMatchVar(Term t) {
if(t instanceof OpTerm && t.as<OpTerm>.op == "`" && t.as<OpTerm>.isPrefix()) {
return new MatchVarTerm(t.as<OpTerm>.right.as<SymbolTerm>.symbol);
} else if(t instanceof OpTerm) {
var t2 = t.as<OpTerm>;
var ct = t2.clone();
if(!ct.isPrefix()) {
ct.left = replaceMatchVar(ct.left);
}
if(!ct.isPostfix()) {
ct.right = replaceMatchVar(ct.right);
}
return ct;
} else if(t instanceof GroupTerm) {
var t2 = t.as<GroupTerm>;
var ct = t2.clone();
ct.group = replaceMatchVar(ct.group);
return ct;
}
return t;
}
class gelly::rewrite::CouldNotInterpretException extends Exception {
Term t = null;
new(Term t) {
this.t = t;
}
as<String> {
return "Could not interpret: " + pretty(t, 0);
}
}
class gelly::rewrite::StrategyDef {
Env env = null;
Bool scoped = true;
Term t = null;
String strArgName = null;
String valArgName = null;
Bool valArgPassByValue = true;
new(Env env, Bool scoped, String strArgName, String valArgName, Bool valArgPassByValue, Term t) {
this.env = env;
this.scoped = scoped;
this.strArgName = strArgName;
this.valArgName = valArgName;
this.valArgPassByValue = valArgPassByValue;
this.t = t;
}
}
class gelly::rewrite::ClosureStrategyDef {
Term t = null;
Env parent = null;
new(Env parent, Term t) {
this.parent = parent;
this.t = t;
}
}
class gelly::rewrite::Env {
Term currentTerm = new EmptyTerm();
Map<String, Term> bindings = null;
Map<String, StrategyDef> strategies = null;
Map<String, ClosureStrategyDef> closureStrategies = null;
Int constructCounter = 0;
new(Map<String, StrategyDef> strategies, Map<String, ClosureStrategyDef> closureStrategies, Map<String, Term> bindings) {
this.strategies = strategies;
this.closureStrategies = closureStrategies;
this.bindings = bindings;
}
Bool eval(Term t) {
if(t instanceof GroupTerm && t.as<GroupTerm>.type == '(') {
t = t.as<GroupTerm>.group;
} else if(t instanceof EmptyTerm) {
return true;
}
constructCounter = 0;
for(Function2<Bool, Term, Env> f : primitiveConstructs) {
if(f(t, this)) {
return true;
}
}
if(constructCounter == primitiveConstructs.length) {
throw new CouldNotInterpretException(t);
}
return false;
}
Bool evalString(String input) {
var p = new Parser(input);
var t = p.acceptExp();
//println(pretty(t, 0));
if(t != null) {
return eval(t);
}
return false;
}
}