Skip to content

Commit

Permalink
Updates, have to work on keyword expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zef Hemel committed Mar 18, 2009
1 parent ea3b71d commit 54b1cba
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 31 deletions.
5 changes: 5 additions & 0 deletions ast.pil
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class gel::ConsTerm extends gel::Term {
var s = new MutableString();
s.append(spaces(depth));
s.append("(");
if(type == 1) {
s.append("prefix:");
} else if(type == 2){
s.append("postfix:");
}
s.append(constructor.as<String>);
var sawCons = false;
for(gel::Term t : children) {
Expand Down
6 changes: 4 additions & 2 deletions gelly.pil
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ void main(Array<String> args) {
interp.handle(input);
}
}
//println(new Parser("3 * 8").acceptExp().toIndentedString(0));
//println(new Parser("rule : [ 10 ] -> [ 20 ]").acceptExp().toIndentedString(0));
/*
println(new Parser("3 * 8").acceptExp().toIndentedString(0));
println(new Parser("rule : [ 10 ] -> [ 20 ]").acceptExp().toIndentedString(0));
*/
/*
var m = new Map<String, Term>();
println(t.match(matchTerm, m));
Expand Down
106 changes: 77 additions & 29 deletions parser.pil
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,40 @@ Term gel::util::valueOrEmpty(Term t) {
class gel::Parser {
String text = "";
Int i = 0;
Int termOperatorIdx = 1;

Array<Array<String>> operators = new Array<Array<String>>(
new Array<String>("."),
new Array<String>("*", "/", "%"),
new Array<String>("+", "->", "-"),
new Array<String>("<", ">"),
new Array<String>("+>", "+", "->", "-"),
new Array<String>("<+", "<", ">"),
new Array<String>("!=", "!", "==", "="),
new Array<String>("&&", "&"),
new Array<String>("||", "|"),
new Array<String>("?", ":", "$"),
new Array<String>("::", "?", ":", "$"),
new Array<String>(","),
new Array<String>("keyword"),
new Array<String>(";")
);

Array<String> prefixOps = new Array<String>("++", "+", "--", "*", "<", ">");
List<String> noWhitespaceOps = new List<String>(";", ",", ".");
Array<String> prefixOps = new Array<String>("++", "+", "--", "-", "*", "<", ">");


new(String text) {
this.text = text;
}

Term followFactor(Term t) {
/*
if(!acceptWhiteSpace()) {
return t;
}
*/
var oldI = i;
acceptWhiteSpace();
var exp = acceptGeneric(termOperatorIdx);
if(!acceptWhiteSpace()) { // Postfix
var op = acceptOps(prefixOps);
if(op != null) {
t = new ConsTerm(2, new SymbolTerm(op), new Array<Term>(t));
}
acceptWhiteSpace();
oldI = i;
}
var exp = acceptFactor(); //acceptGeneric(termOperatorIdx);
if(exp == null) {
i = oldI;
return t;
Expand All @@ -58,7 +61,7 @@ class gel::Parser {
oldI = i;
acceptWhiteSpace();
t = new ConsTerm(0, new SymbolTerm("_"), new Array<Term>(t, exp));
exp = acceptGeneric(termOperatorIdx);
exp = acceptFactor(); //acceptGeneric(termOperatorIdx);
if(exp == null) {
i = oldI;
return t;
Expand All @@ -68,46 +71,57 @@ class gel::Parser {
}

Term acceptFactor() {
var t = acceptFactorNoFollow();
if(t == null) {
return null;
} else {
return followFactor(t);
}
}

Term acceptFactorNoFollow() {
var in = acceptInt();
if(in != -1) {
return followFactor(new IntTerm(in));
return new IntTerm(in);
}
var idn = acceptIdn();
if(idn != null) {
return followFactor(new SymbolTerm(idn));
if(idn != null && !lookAhead(":")) {
return new SymbolTerm(idn);
} else if(idn != null && lookAhead(":")) {
i = i - idn.length;
}
var s = acceptString();
if(s != null) {
return followFactor(new StringTerm(s));
return new StringTerm(s);
}
var mv = acceptMatchVar();
if(mv != null) {
return followFactor(new MatchVarTerm(mv));
return new MatchVarTerm(mv);
}
var pe = acceptPrefixExp();
if(pe != null) {
return followFactor(pe);
return pe;
}
if(accept('(')) {
acceptWhiteSpace();
var exp = acceptExp();
acceptWhiteSpace();
expect(')');
return followFactor(new ConsTerm(0, new SymbolTerm("()"), new Array<Term>(valueOrEmpty(exp))));
return new ConsTerm(0, new SymbolTerm("()"), new Array<Term>(valueOrEmpty(exp)));
}
if(accept('[')) {
acceptWhiteSpace();
var exp = acceptExp();
acceptWhiteSpace();
expect(']');
return followFactor(new ConsTerm(0, new SymbolTerm("[]"), new Array<Term>(valueOrEmpty(exp))));
return new ConsTerm(0, new SymbolTerm("[]"), new Array<Term>(valueOrEmpty(exp)));
}
if(accept('{')) {
acceptWhiteSpace();
var exp = acceptExp();
acceptWhiteSpace();
expect('}');
return followFactor(new ConsTerm(0, new SymbolTerm("{}"), new Array<Term>(valueOrEmpty(exp))));
return new ConsTerm(0, new SymbolTerm("{}"), new Array<Term>(valueOrEmpty(exp)));
}
return null;
}
Expand All @@ -117,18 +131,30 @@ class gel::Parser {
if(op == null) {
return null;
}
var t = acceptGeneric(1);
var t = acceptFactorNoFollow(); //acceptGeneric(0);
if(t != null) {
return new ConsTerm(1, new SymbolTerm(op), new Array<Term>(valueOrEmpty(t)));
return new ConsTerm(1, new SymbolTerm(op), new Array<Term>(t));
} else {
return null;
}
}

Term acceptGeneric(Int level) {
Term f = null;
var inKeyworldLevel = false;
if(level == 0) {
f = acceptFactor();
} else if(operators[level][0] == "keyword") {
var veryOldI = i;
inKeyworldLevel = true;
var kw = acceptKeyword();
if(kw != null) {
i = veryOldI;
printRest();
f = new EmptyTerm();
} else {
f = acceptGeneric(level-1);
}
} else {
f = acceptGeneric(level-1);
}
Expand All @@ -137,12 +163,14 @@ class gel::Parser {
}
var t = f;
var oldI = i;
if(!acceptWhiteSpace()) {
var didAcceptWhitespace = acceptWhiteSpace();
var op = acceptOps(operators[level]);
if(!didAcceptWhitespace && op != null && !noWhitespaceOps.contains(op) && !inKeyworldLevel) {
i = oldI;
return t;
}
var op = acceptOps(operators[level]);
while(op != null) {
if(!acceptWhiteSpace()) {
if(!acceptWhiteSpace() && !noWhitespaceOps.contains(op)) {
i = oldI;
return t;
}
Expand All @@ -154,7 +182,7 @@ class gel::Parser {
}
t = new ConsTerm(0, new SymbolTerm(op), new Array<Term>(t, valueOrEmpty(t2)));
oldI = i;
if(!acceptWhiteSpace()) {
if(!acceptWhiteSpace() && !noWhitespaceOps.contains(op) && !inKeyworldLevel) {
i = oldI;
return t;
}
Expand Down Expand Up @@ -201,14 +229,33 @@ class gel::Parser {

String acceptOps(Array<String> ops) {
for(String op : ops) {
if(lookAhead(op)) {
if(op == "keyword") {
var kw = acceptKeyword();
if(kw != null) {
return kw;
}
} else if(lookAhead(op)) {
i = i + op.length;
return op;
}
}
return null;
}

String acceptKeyword() {
var oldI = i;
var idn = acceptIdn();
if(idn == null) {
return null;
}
if(accept(':')) {
return idn + ":";
} else {
i = oldI;
return null;
}
}


Int acceptInt() {
if(i == text.length) {
Expand Down Expand Up @@ -287,6 +334,7 @@ class gel::Parser {
if(i == text.length) {
return null;
}
var oldI = i;
var sym = new MutableString();
if((text[i] >= 'a' && text[i] <= 'z') ||
(text[i] >= 'A' && text[i] <= 'Z') ||
Expand Down

0 comments on commit 54b1cba

Please sign in to comment.