Permalink
Browse files

Added pretty printer

  • Loading branch information...
1 parent 7b1d829 commit cb780685f6e89ef5ecf906e879aa9b4fd4300f5e @zefhemel committed Mar 24, 2009
Showing with 137 additions and 186 deletions.
  1. +20 −20 primitives.pil → core.pil
  2. +0 −91 evaluator.pil
  3. +7 −7 gelly.pil
  4. +48 −0 pretty.pil
  5. +9 −6 rewrite.pil
  6. +10 −19 simple.g
  7. +34 −34 sugar.g
  8. +1 −1 test.g
  9. +8 −8 test2.g
@@ -15,17 +15,17 @@ Term gelly::rewrite::strategyCallNoStrParamPattern = replaceMatchVar(new Parser(
Term gelly::rewrite::strategyCallNoValParamPattern = replaceMatchVar(new Parser("`name(`strparam)").acceptExp());
Term gelly::rewrite::strategyCallNoStrParamNoValParamPattern = replaceMatchVar(new Parser("`name").acceptExp());
-Array<Function2<Bool, Term, Strategy>> gelly::rewrite::primitiveConstructs = new Array<Function2<Bool, Term, Strategy>>(
- Bool(Term t, Strategy i) { gelly::rewrite::prim::strategyCall(t, i) },
- Bool(Term t, Strategy i) { gelly::rewrite::prim::import(t, i) },
- Bool(Term t, Strategy i) { gelly::rewrite::prim::strategyDef(t, i) },
- Bool(Term t, Strategy i) { gelly::rewrite::prim::seq(t, i) },
- Bool(Term t, Strategy i) { gelly::rewrite::prim::leftChoice(t, i) },
- Bool(Term t, Strategy i) { gelly::rewrite::prim::build(t, i) },
- Bool(Term t, Strategy i) { gelly::rewrite::prim::match(t, i) }
+Array<Function2<Bool, Term, Env>> gelly::rewrite::primitiveConstructs = new Array<Function2<Bool, Term, Env>>(
+ Bool(Term t, Env env) { gelly::rewrite::prim::strategyCall(t, env) },
+ Bool(Term t, Env env) { gelly::rewrite::prim::import(t, env) },
+ Bool(Term t, Env env) { gelly::rewrite::prim::strategyDef(t, env) },
+ Bool(Term t, Env env) { gelly::rewrite::prim::seq(t, env) },
+ Bool(Term t, Env env) { gelly::rewrite::prim::leftChoice(t, env) },
+ Bool(Term t, Env env) { gelly::rewrite::prim::build(t, env) },
+ Bool(Term t, Env env) { gelly::rewrite::prim::match(t, env) }
);
-Bool gelly::rewrite::prim::build(Term t, Strategy env) {
+Bool gelly::rewrite::prim::build(Term t, Env env) {
var m = new Map<String, Term>();
if(buildPattern.match(t, m)) {
env.currentTerm = replaceMatchVar(m["t"]).construct(env.bindings);
@@ -35,7 +35,7 @@ Bool gelly::rewrite::prim::build(Term t, Strategy env) {
}
}
-Bool gelly::rewrite::prim::strategyDef(Term t, Strategy env) {
+Bool gelly::rewrite::prim::strategyDef(Term t, Env env) {
var m = new Map<String, Term>();
if(strategyDefPattern.match(t, m)) {
if(!(m["name"] instanceof SymbolTerm)) {
@@ -62,7 +62,7 @@ Bool gelly::rewrite::prim::strategyDef(Term t, Strategy env) {
return false;
}
-Bool gelly::rewrite::prim::seq(Term t, Strategy env) {
+Bool gelly::rewrite::prim::seq(Term t, Env env) {
var passedOnce = false;
while(t instanceof OpTerm && t.as<OpTerm>.op == ";") {
var t2 = t.as<OpTerm>;
@@ -80,7 +80,7 @@ Bool gelly::rewrite::prim::seq(Term t, Strategy env) {
}
}
-Bool gelly::rewrite::prim::leftChoice(Term t, Strategy env) {
+Bool gelly::rewrite::prim::leftChoice(Term t, Env env) {
var m = new Map<String, Term>();
if(leftChoicePattern.match(t, m)) {
if(env.eval(m["f1"])) {
@@ -94,15 +94,15 @@ Bool gelly::rewrite::prim::leftChoice(Term t, Strategy env) {
class gelly::rewrite::StrArgStrategy {
Term t = null;
- Strategy e = null;
+ Env e = null;
- new(Strategy e, Term t) {
+ new(Env e, Term t) {
this.e = e;
this.t = t;
}
}
-Bool gelly::rewrite::prim::strategyCall(Term t, Strategy env) {
+Bool gelly::rewrite::prim::strategyCall(Term t, Env env) {
var m = new Map<String, Term>();
if(strategyCallNoStrParamPattern.match(t, m)) {
m["strparam"] = new EmptyTerm();
@@ -132,7 +132,7 @@ Bool gelly::rewrite::prim::strategyCall(Term t, Strategy env) {
} else if(strategyName == "fail") {
return false;
} else if(strategyName == "eval") {
- var newStr = new Strategy(env.strategies.clone(), env.closureStrategies.clone(), env.bindings.clone());
+ var newStr = new Env(env.strategies.clone(), env.closureStrategies.clone(), env.bindings.clone());
newStr.currentTerm = env.currentTerm;
if(!newStr.eval(m["valparam"])) {
return false;
@@ -218,7 +218,7 @@ Bool gelly::rewrite::prim::strategyCall(Term t, Strategy env) {
} else {
if(env.strategies.contains(strategyName)) {
var str = env.strategies[strategyName];
- var newStr = new Strategy(env.strategies.clone(), env.closureStrategies.clone(), new Map<String, Term>()); //env.bindings.clone()); //new Map<String, Term>());
+ var newStr = new Env(env.strategies.clone(), env.closureStrategies.clone(), new Map<String, Term>()); //env.bindings.clone()); //new Map<String, Term>());
newStr.currentTerm = env.currentTerm;
if(!(m["strparam"] instanceof EmptyTerm)) {
newStr.closureStrategies[str.strArgName] = new ClosureStrategyDef(env, m["strparam"]);
@@ -243,7 +243,7 @@ Bool gelly::rewrite::prim::strategyCall(Term t, Strategy env) {
}
} else if(env.closureStrategies.contains(strategyName)) {
ClosureStrategyDef str = env.closureStrategies[strategyName];
- var newStr = new Strategy(str.parent.strategies, str.parent.closureStrategies, str.parent.bindings);
+ var newStr = new Env(str.parent.strategies, str.parent.closureStrategies, str.parent.bindings);
newStr.currentTerm = env.currentTerm;
if(newStr.eval(str.t)) {
env.currentTerm = newStr.currentTerm;
@@ -258,7 +258,7 @@ Bool gelly::rewrite::prim::strategyCall(Term t, Strategy env) {
return false;
}
-Bool gelly::rewrite::prim::match(Term t, Strategy env) {
+Bool gelly::rewrite::prim::match(Term t, Env env) {
var m = new Map<String, Term>();
if(matchPattern.match(t, m)) {
var t2 = replaceMatchVar(m["t"]);
@@ -267,7 +267,7 @@ Bool gelly::rewrite::prim::match(Term t, Strategy env) {
return false;
}
-Bool gelly::rewrite::prim::import(Term t, Strategy env) {
+Bool gelly::rewrite::prim::import(Term t, Env env) {
var m = new Map<String, Term>();
if(importPattern.match(t, m)) {
//try {
View
@@ -1,91 +0,0 @@
-import "ast.pil"
-
-import zisp
-
-class zisp::EvalException extends Exception {
- new(String message) extends super(message) {
- }
-}
-
-
-class zisp::Evaluator {
- Term eval(Term t) {
- return t;
- }
-}
-
-Map<String, Evaluator> zisp::functions = new Map<String, Evaluator>();
-Map<String, Term> zisp::globalDefs = new Map<String, Term>();
-
-Term zisp::evaluate(Term t) {
- //println("Now evaling: " + t.toString());
- if(t instanceof ConsTerm) {
- var term = t.as<ConsTerm>;
- return functions[term.constructor.as<SymbolTerm>.symbol].eval(t);
- }
- if(t instanceof SymbolTerm) {
- return globalDefs[getSymbol(t)];
- }
- return t;
-}
-
-class zisp::eval::Plus extends Evaluator {
- Term eval(Term t) {
- var value = 0;
- var term = t.as<ConsTerm>;
- for(Term v : term.children) {
- var val = evaluate(v);
- value = value + getInt(val);
- }
- return new IntTerm(value);
- }
-}
-
-class zisp::eval::Multiply extends Evaluator {
- Term eval(Term t) {
- var value = 1;
- var term = t.as<ConsTerm>;
- for(Term v : term.children) {
- var val = evaluate(v);
- value = value * getInt(val);
- }
- return new IntTerm(value);
- }
-}
-
-class zisp::eval::Def extends Evaluator {
- Term eval(Term t) {
- var value = 1;
- var term = t.as<ConsTerm>;
- var result = evaluate(term.children[1]);
- globalDefs[getSymbol(term.children[0])] = result;
- return result;
- }
-}
-
-String zisp::getString(Term t) {
- if(!(t instanceof StringTerm)) {
- throw new EvalException("Expected String, but got: " + t.as<String>);
- }
- return t.as<StringTerm>.value;
-}
-
-String zisp::getSymbol(Term t) {
- if(!(t instanceof SymbolTerm)) {
- throw new EvalException("Expected Symbol, but got: " + t.as<String>);
- }
- return t.as<SymbolTerm>.symbol;
-}
-
-Int zisp::getInt(Term t) {
- if(!(t instanceof IntTerm)) {
- throw new EvalException("Expected Int, but got: " + t.as<String>);
- }
- return t.as<IntTerm>.value;
-}
-
-void zisp::evalInit() {
- functions["+"] = new zisp::eval::Plus();
- functions["*"] = new zisp::eval::Multiply();
- functions["def"] = new zisp::eval::Def();
-}
View
@@ -2,7 +2,7 @@ import "ast.pil"
import "parser.pil"
//import "evaluator.pil"
import "rewrite.pil"
-import "primitives.pil"
+import "core.pil"
import gel
import gelly::rewrite
@@ -13,13 +13,13 @@ void main(Array<String> args) {
String input = null;
//println(replaceMatchVar(new Parser("![ `exp ]").acceptExp()).toIndentedString(0));
//println(new Parser("<bla> ha").acceptExp().toIndentedString(0));
- var s = new Strategy(new Map<String, StrategyDef>(), new Map<String, ClosureStrategyDef>(), new Map<String, Term>());
- if(!s.evalString("import \"sugar.g\"")) {
+ var env = new Env(new Map<String, StrategyDef>(), new Map<String, ClosureStrategyDef>(), new Map<String, Term>());
+ if(!env.evalString("import \"sugar.g\"")) {
println("Couldn't load sugar.g");
}
if(args.length > 0) {
- if(s.evalString("import \"" + args[0] + "\"")) {
- println(s.currentTerm.toIndentedString(0));
+ if(env.evalString("import \"" + args[0] + "\"")) {
+ println(env.currentTerm.toIndentedString(0));
} else {
println("Rewriting failed.");
}
@@ -29,8 +29,8 @@ void main(Array<String> args) {
print("> ");
input = readln();
if(input != ":q") {
- if(s.evalString(input)) {
- println(s.currentTerm.toIndentedString(0));
+ if(env.evalString(input)) {
+ println(env.currentTerm.toIndentedString(0));
} else {
println("Rewriting failed.");
}
View
@@ -0,0 +1,48 @@
+import "ast.pil"
+
+import gel
+import gel::util
+import gelly
+
+String stripEndSpaces(String str, Int n) {
+ var m = new MutableString(str);
+ for(Int i = str.length-1; i > 0 && n > 0; i--) {
+ if(str[i] != ' ') {
+ return m.as<String>;
+ }
+ m.remove(i);
+ n--;
+ }
+ return m.as<String>;
+}
+
+String gelly::pretty(Term t, Int indentLevel) {
+ if(t instanceof GroupTerm) {
+ var gt = t.as<GroupTerm>;
+ if(gt.type == '(') {
+ return "(" + pretty(gt.group, indentLevel+1) + ")";
+ } else if(gt.type == '[') {
+ return "[" + pretty(gt.group, indentLevel+1) + "]";
+ } else if(gt.type == '{') {
+ return "{\n" + spaces(indentLevel+1) + stripEndSpaces(pretty(gt.group, indentLevel+1), 2) + "}";
+ }
+ } else if(t instanceof OpTerm) {
+ var ot = t.as<OpTerm>;
+ if(ot.op == ";") {
+ return pretty(ot.left, indentLevel) + ";\n" + spaces(indentLevel) + pretty(ot.right, indentLevel);
+ }
+ if(ot.op == "_") {
+ return pretty(ot.left, indentLevel) + " " + pretty(ot.right, indentLevel);
+ }
+ if(ot.isPrefix()) {
+ return ot.op + pretty(ot.right, indentLevel);
+ }
+ if(ot.isPostfix()) {
+ return pretty(ot.left, indentLevel) + ot.op;
+ }
+ return pretty(ot.left, indentLevel) + " " + ot.op + " " + pretty(ot.right, indentLevel);
+ } else if(t instanceof EmptyTerm) {
+ return "";
+ }
+ return t.as<String>;
+}
View
@@ -1,10 +1,12 @@
import "ast.pil"
import "parser.pil"
import "utils.pil"
-import "/Users/zef/svn/pil/src/lib/io.pil"
-import "primitives.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
@@ -57,15 +59,15 @@ class gelly::rewrite::StrategyDef {
class gelly::rewrite::ClosureStrategyDef {
Term t = null;
- Strategy parent = null;
+ Env parent = null;
- new(Strategy parent, Term t) {
+ new(Env parent, Term t) {
this.parent = parent;
this.t = t;
}
}
-class gelly::rewrite::Strategy {
+class gelly::rewrite::Env {
Term currentTerm = new EmptyTerm();
Map<String, Term> bindings = null;
Map<String, StrategyDef> strategies = null;
@@ -83,7 +85,7 @@ class gelly::rewrite::Strategy {
} else if(t instanceof EmptyTerm) {
return true;
}
- for(Function2<Bool, Term, Strategy> f : primitiveConstructs) {
+ for(Function2<Bool, Term, Env> f : primitiveConstructs) {
if(f(t, this)) {
return true;
}
@@ -95,6 +97,7 @@ class gelly::rewrite::Strategy {
Bool evalString(String input) {
var p = new Parser(input);
var t = p.acceptExp();
+ //println(pretty(t, 0));
if(t != null) {
return eval(t);
}
Oops, something went wrong.

0 comments on commit cb78068

Please sign in to comment.