Permalink
Browse files

Simple webdsl sample

  • Loading branch information...
zefhemel committed Mar 25, 2009
1 parent 8a76f7c commit 35a3dededf4d69b5c871b95f4f8dbc9876430042
Showing with 95 additions and 37 deletions.
  1. +1 −1 TODO
  2. +40 −21 core.pil
  3. +2 −1 gelly.pil
  4. +2 −1 primitives.pil
  5. +9 −3 rewrite.pil
  6. +8 −8 sugar.g
  7. +3 −2 test.g
  8. +23 −0 webdsl.g
  9. +7 −0 webdsltest.g
View
2 TODO
@@ -1,2 +1,2 @@
-* scope blending
+* is-string, is-symbol, is-int primitives
* Try some code generation
View
@@ -11,6 +11,7 @@ Term gelly::rewrite::importPattern = replaceMatchVar(new Parser("import `fn").ac
Term gelly::rewrite::seqPattern = replaceMatchVar(new Parser("`f1; `f2").acceptExp());
Term gelly::rewrite::leftChoicePattern = replaceMatchVar(new Parser("`f1 <+ `f2").acceptExp());
Term gelly::rewrite::strategyDefPattern = replaceMatchVar(new Parser("strategy `name (`strparam)[`valparam] { `steps }").acceptExp());
+Term gelly::rewrite::unScopedStrategyDefPattern = replaceMatchVar(new Parser("unscoped strategy `name (`strparam)[`valparam] { `steps }").acceptExp());
Term gelly::rewrite::strategyCallPattern = replaceMatchVar(new Parser("`name(`strparam)[`valparam]").acceptExp());
Term gelly::rewrite::strategyCallNoStrParamPattern = replaceMatchVar(new Parser("`name[`valparam]").acceptExp());
Term gelly::rewrite::strategyCallNoValParamPattern = replaceMatchVar(new Parser("`name(`strparam)").acceptExp());
@@ -43,35 +44,42 @@ Bool gelly::rewrite::prim::build(Term t, Env env) {
env.currentTerm = replaceMatchVar(m["t"]).construct(env.bindings);
return true;
} else {
+ env.constructCounter = env.constructCounter + 1;
return false;
}
}
Bool gelly::rewrite::prim::strategyDef(Term t, Env env) {
var m = new Map<String, Term>();
+ var scoped = true;
if(strategyDefPattern.match(t, m)) {
- if(!(m["name"] instanceof SymbolTerm)) {
- return false;
- }
- String name = m["name"].as<SymbolTerm>.symbol;
- String strArgName = null;
- String valArgName = null;
- Bool valArgPassByValue = true;
- if(!(m["strparam"] instanceof EmptyTerm)) {
- strArgName = m["strparam"].as<SymbolTerm>.symbol;
- }
- if(!(m["valparam"] instanceof EmptyTerm)) {
- if(m["valparam"] instanceof SymbolTerm) {
- valArgName = m["valparam"].as<SymbolTerm>.symbol;
- } else {
- valArgName = m["valparam"].as<OpTerm>.right.as<SymbolTerm>.symbol;
- valArgPassByValue = false;
- }
+ scoped = true;
+ } else if(unScopedStrategyDefPattern.match(t, m)) {
+ scoped = false;
+ } else {
+ env.constructCounter = env.constructCounter + 1;
+ return false;
+ }
+ if(!(m["name"] instanceof SymbolTerm)) {
+ return false;
+ }
+ String name = m["name"].as<SymbolTerm>.symbol;
+ String strArgName = null;
+ String valArgName = null;
+ Bool valArgPassByValue = true;
+ if(!(m["strparam"] instanceof EmptyTerm)) {
+ strArgName = m["strparam"].as<SymbolTerm>.symbol;
+ }
+ if(!(m["valparam"] instanceof EmptyTerm)) {
+ if(m["valparam"] instanceof SymbolTerm) {
+ valArgName = m["valparam"].as<SymbolTerm>.symbol;
+ } else {
+ valArgName = m["valparam"].as<OpTerm>.right.as<SymbolTerm>.symbol;
+ valArgPassByValue = false;
}
- env.strategies[name] = new StrategyDef(env, strArgName, valArgName, valArgPassByValue, m["steps"]);
- return true;
}
- return false;
+ env.strategies[name] = new StrategyDef(env, scoped, strArgName, valArgName, valArgPassByValue, m["steps"]);
+ return true;
}
Bool gelly::rewrite::prim::seq(Term t, Env env) {
@@ -88,6 +96,7 @@ Bool gelly::rewrite::prim::seq(Term t, Env env) {
if(passedOnce) {
return env.eval(t);
} else {
+ env.constructCounter = env.constructCounter + 1;
return false;
}
}
@@ -100,6 +109,8 @@ Bool gelly::rewrite::prim::leftChoice(Term t, Env env) {
} else {
return env.eval(m["f2"]);
}
+ } else {
+ env.constructCounter = env.constructCounter + 1;
}
return false;
}
@@ -130,6 +141,7 @@ Bool gelly::rewrite::prim::strategyCall(Term t, Env env) {
} else if(strategyCallPattern.match(t, m)) {
// Nuthin'
} else {
+ env.constructCounter = env.constructCounter + 1;
return false;
}
var strategyName = m["name"].as<SymbolTerm>.symbol;
@@ -139,7 +151,10 @@ Bool gelly::rewrite::prim::strategyCall(Term t, Env env) {
if(env.strategies.contains(strategyName)) {
var str = env.strategies[strategyName];
- var newEnv = new Env(str.env.strategies.clone(), str.env.closureStrategies.clone(), str.env.bindings.clone()); //);new Map<String, Term>()); //env.bindings.clone()); //new Map<String, Term>());
+ var newEnv = env;
+ if(str.scoped) {
+ newEnv = new Env(str.env.strategies.clone(), str.env.closureStrategies.clone(), str.env.bindings.clone()); //);new Map<String, Term>()); //env.bindings.clone()); //new Map<String, Term>());
+ }
newEnv.currentTerm = env.currentTerm;
if(!(m["strparam"] instanceof EmptyTerm)) {
newEnv.closureStrategies[str.strArgName] = new ClosureStrategyDef(env, m["strparam"]);
@@ -183,6 +198,8 @@ Bool gelly::rewrite::prim::match(Term t, Env env) {
if(matchPattern.match(t, m)) {
var t2 = replaceMatchVar(m["t"]);
return t2.match(env.currentTerm, env.bindings);
+ } else {
+ env.constructCounter = env.constructCounter + 1;
}
return false;
}
@@ -198,6 +215,8 @@ Bool gelly::rewrite::prim::import(Term t, Env env) {
//} catch(Exception e) {
//println("Could not read file.");
//}
+ } else {
+ env.constructCounter = env.constructCounter + 1;
}
return false;
}
View
@@ -4,6 +4,7 @@ import "parser.pil"
import "rewrite.pil"
import "core.pil"
import "primitives.pil"
+import "pretty.pil"
import gel
import gelly::rewrite
@@ -20,7 +21,7 @@ void main(Array<String> args) {
}
if(args.length > 0) {
if(env.evalString("import \"" + args[0] + "\"")) {
- println(env.currentTerm.toIndentedString(0));
+ println(pretty(env.currentTerm, 0));
} else {
println("Rewriting failed.");
}
View
@@ -85,8 +85,9 @@ Bool gelly::rewrite::primitives::all(Map<String, Term> bindings, Env env) {
}
} else if(env.currentTerm instanceof GroupTerm) {
var oldT = env.currentTerm;
+ var stratT = bindings["strparam"];
env.currentTerm = env.currentTerm.as<GroupTerm>.group;
- if(gelly::rewrite::primitives::all(bindings, env)) {
+ if(gelly::rewrite::prim::strategyCall(stratT, env)) {
env.currentTerm = new GroupTerm(oldT.as<GroupTerm>.type, env.currentTerm);
return true;
} else {
View
@@ -39,19 +39,21 @@ class gelly::rewrite::CouldNotInterpretException extends Exception {
}
as<String> {
- return "Could not interpret: " + 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, String strArgName, String valArgName, Bool valArgPassByValue, Term t) {
+ 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;
@@ -74,6 +76,7 @@ class gelly::rewrite::Env {
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;
@@ -87,12 +90,15 @@ class gelly::rewrite::Env {
} else if(t instanceof EmptyTerm) {
return true;
}
+ constructCounter = 0;
for(Function2<Bool, Term, Env> f : primitiveConstructs) {
if(f(t, this)) {
return true;
}
}
- //throw new CouldNotInterpretException(t);
+ if(constructCounter == primitiveConstructs.length) {
+ throw new CouldNotInterpretException(t);
+ }
return false;
}
View
16 sugar.g
@@ -26,8 +26,8 @@ strategy gelly-sugar-strategy-simple()[] {
};
strategy gelly-sugar-strategy-application()[] {
- ?[ <`str> `t ];
- ![ `t; `str ];
+ ?[ <`str> `t2 ];
+ ![ `t2; `str ];
};
strategy gelly-sugar-strategy-application2()[] {
@@ -40,9 +40,9 @@ strategy gelly-sugar-binding()[] {
![ `rhs; ?[ `lhs ] ];
};
-strategy main()[:p] {
- ?[ `t ];
- ![ `p ];
+unscoped strategy main()[:main-p] {
+ ?[ `main-t ];
+ ![ `main-p ];
innermost(
gelly-sugar-rule-where-def
<+ gelly-sugar-rule-def
@@ -51,7 +51,7 @@ strategy main()[:p] {
<+ gelly-sugar-strategy-application
<+ gelly-sugar-strategy-application2
);
- ?[ `p2 ];
- ![ `t ];
- eval[ ![ `p2 ] ];
+ ?[ `main-p2 ];
+ ![ `main-t ];
+ eval[ ![ `main-p2 ] ];
};
View
5 test.g
@@ -1,11 +1,12 @@
// Simple test script
main[
//rule bla-rule : [ 1 ] -> [ 2 ];
- rule bla2rule : [ `x ] -> [ `y + 1 ] where { `y := ![ `x ]; };
+ //rule bla2rule : [ `x ] -> [ `y + 1 ] where { `y := ![ `x ]; };
//strategy whatever_id {
//id; debug
//};
//<(bla-rule; whatever_id)> ![ 1 ];
![ 7 * 8 ];
- bla2rule
+ where(debug);
+ //bla2rule
]
View
@@ -0,0 +1,23 @@
+main[
+ rule addone : [ `x ] -> [ `x + 1 ];
+ strategy elem-to-code {
+ rule text-to-code : [ text(`txt) `next ] -> [ echo `txt; `next-code ]
+ where { `next-code := <elem-to-code> ![ `next ] };
+
+ rule hr-to-code : [ hr() `next ] -> [ echo "<hr/>"; `next-code ]
+ where { `next-code := <elem-to-code> ![ `next ] };
+
+ strategy single-to-code {
+ ?[ `s(`a) ];
+ ![ `s(`a) ingore ];
+ elem-to-code;
+ };
+
+ text-to-code <+ hr-to-code <+ single-to-code <+ ![ ]
+ };
+
+];
+strategy webdsl()[:webdsl-p] {
+ ![ `webdsl-p ];
+ elem-to-code
+};
View
@@ -0,0 +1,7 @@
+import "webdsl.g";
+
+webdsl[
+ text("Fritsie!")
+ text("Fratsie")
+ hr()
+]

0 comments on commit 35a3ded

Please sign in to comment.