private NodeExpression ParseExpressionE(NodeTask t) { // this is the hard one NodeExpression o0 = ParseExpressionT(t); Token next = tn.Peek(); while (next.type == Token.Type.Keyword && (next.val == "+" || next.val == "-" || next.val == "%")) { Token op = tn.Next(); NodeExpression o1 = ParseExpressionT(t); if (op.val == "+") { o0 = new ExprAdd(t, o0, o1); } else if (op.val == "-") { o0 = new ExprSub(t, o0, o1); } else if (op.val == "%") { o0 = new ExprMod(t, o0, o1); } next = tn.Peek(); } return(o0); }
private NodeExpression ParseExpressionT(NodeTask t) { // this is the hard one NodeExpression o0 = ParseExpressionP(t); Token next = tn.Peek(); while (next.type == Token.Type.Keyword && (next.val == "*" || next.val == "/")) { Token op = tn.Next(); NodeExpression o1 = ParseExpressionP(t); if (op.val == "*") { o0 = new ExprMul(t, o0, o1); } else if (op.val == "/") { o0 = new ExprDiv(t, o0, o1); } next = tn.Peek(); } return(o0); }
private NodeExpression ParseExpression(NodeTask t) { // this is the hard one NodeExpression e = ParseExpressionBOr(t); Token next = tn.Peek(); if (next.type == Token.Type.Keyword && next.val == ";") { // all fine } else { } return(e); }
public bool BindSymbols() { bool ok = true; // Traverse the tree and connect ID expression to their expression foreach (NodeDefinition d in definitions) { NodeExpression e = d.GetExpression(); ok &= e.BindSymbols(); } foreach (NodeTask t in subTasks) { ok &= t.BindSymbols(); } return(ok); }
public virtual Value GetValueOfId(string def) { int dotIndex = def.IndexOf('.'); if (dotIndex != -1) { // dot in name, search in named chilren tasks for it String cname = def.Substring(0, dotIndex); String cdef = def.Substring(dotIndex + 1); foreach (NodeTask t in subTasks) { if (t.name != null && t.name == cname) { Value v = t.GetValueOfId(cdef); return(v); } } } else { foreach (NodeDefinition d in definitions) { if (d.IsNamed(def)) { NodeExpression e = d.GetExpression(); Value val = e.GetValue(); // PPather.WriteLine(" def: " + def + " got e " + e + " and val " + val); return(val); } } if (parent != null) { Value e = parent.GetValueOfId(def); return(e); } } Error("No definition of idenfifier " + def); return(null); }
private NodeExpression ParseExpressionC(NodeTask t) { NodeExpression o0 = ParseExpressionE(t); Token next = tn.Peek(); while (next.type == Token.Type.Keyword && (next.val == "<" || next.val == "<=" || next.val == "==" || next.val == ">=" || next.val == ">" || next.val == "!=")) { Token op = tn.Next(); NodeExpression o1 = ParseExpressionE(t); if (op.val == "<") { o0 = new ExprCmpLt(t, o0, o1); } else if (op.val == "<=") { o0 = new ExprCmpLe(t, o0, o1); } else if (op.val == "==") { o0 = new ExprCmpEq(t, o0, o1); } else if (op.val == ">=") { o0 = new ExprCmpGe(t, o0, o1); } else if (op.val == ">") { o0 = new ExprCmpGt(t, o0, o1); } else if (op.val == "!=") { o0 = new ExprCmpNe(t, o0, o1); } next = tn.Peek(); } return(o0); }
private NodeExpression ParseExpressionBOr(NodeTask t) { NodeExpression o0 = ParseExpressionBAnd(t); Token next = tn.Peek(); while (next.type == Token.Type.Keyword && (next.val == "||")) { Token op = tn.Next(); NodeExpression o1 = ParseExpressionBAnd(t); if (op.val == "||") { o0 = new ExprOr(t, o0, o1); } next = tn.Peek(); } return(o0); }
public virtual NodeExpression GetExpressionOfId(string def) { /*int dotIndex = def.IndexOf('.'); * * if (dotIndex != -1) * { * // dot in name, search in named chilren tasks for it * String cname = def.Substring(0, dotIndex); * String cdef = def.Substring(dotIndex + 1); * foreach ( * NodeTask t in subTasks) * { * if (t.name != null && t.name == cname) * return t.GetExpressionOfId(def); * } * } * else*/ { foreach (NodeDefinition d in definitions) { if (d.IsNamed(def)) { return(d.GetExpression()); } } } if (parent != null) { NodeExpression e = parent.GetExpressionOfId(def); return(e); } Error("No definition of idenfifier " + def); return(null); }
public AssocReadExpression(NodeTask task, NodeExpression a0, NodeExpression a1) : base(task, a0, a1) { }
private NodeExpression ParseExpressionP(NodeTask t) { Token next = tn.Peek(); if (next.type == Token.Type.Literal) { // literal expression tn.Next(); Token lpar = tn.Peek(); if (lpar.type == Token.Type.Keyword && lpar.val == "(") { //fcall List <NodeExpression> exprs = new List <NodeExpression>(); Token comma = null; do { tn.Next(); // eat ( or , NodeExpression n = ParseExpression(t); exprs.Add(n); comma = tn.Peek(); } while (comma.type == Token.Type.Keyword && comma.val == ","); Expect(Token.Type.Keyword, ")"); NodeExpression e = new FcallExpression(t, next.val, exprs); return(e); } else { NodeExpression e = new LiteralExpression(t, next.val); return(e); } } else if (next.type == Token.Type.ID) { // ID expression tn.Next(); Token lpar = tn.Peek(); // assoc lookup if (lpar.type == Token.Type.Keyword && lpar.val == "{") { tn.Next(); // eat { NodeExpression id = new IDExpression(t, next.val); NodeExpression key = ParseExpression(t); Expect(Token.Type.Keyword, "}"); NodeExpression e = new AssocReadExpression(t, id, key); return(e); } else { NodeExpression e = new IDExpression(t, next.val); return(e); } } else if (next.type == Token.Type.Keyword && next.val == "(") { // par expressions tn.Next(); NodeExpression n = ParseExpression(t); Expect(Token.Type.Keyword, ")"); return(n); } else if (next.type == Token.Type.Keyword && next.val == "[") { // collection expressions List <NodeExpression> exprs = new List <NodeExpression>(); Token comma = null; do { tn.Next(); Token p = tn.Peek(); if (p.type == Token.Type.Keyword && p.val == "]") { break; // empty collection } NodeExpression n = ParseExpression(t); exprs.Add(n); comma = tn.Peek(); } while (comma.type == Token.Type.Keyword && comma.val == ","); Expect(Token.Type.Keyword, "]"); CollectionExpression ce = new CollectionExpression(t, exprs); return(ce); } else if (next.type == Token.Type.Keyword && next.val == "-") { // unary neg Token neg = tn.Next(); NodeExpression P = ParseExpressionP(t); NodeExpression e = new NegExpression(t, P); return(e); } else { Error("Currupt expression"); } return(null); }
public NodeTask ParseTask(NodeTask parent) { NodeTask t = new NodeTask(parent); Token r = tn.Peek(); if (r.type == Token.Type.EOF) { return(null); } String s_name = null; String s_type = null; // Type (or name) Token t1 = tn.Next(); if (t1.type != Token.Type.Literal) // !!! { Error("Task must have a type or name"); return(null); } Token colon = tn.Peek(); if (colon.type == Token.Type.Keyword && colon.val == ":") { tn.Next(); // eat colon Token t2 = tn.Next(); // type if (t2.type != Token.Type.Literal) // !!! { Error("Expected task type after : in task definition"); return(null); } s_name = t1.val; s_type = t2.val; } else { s_name = null; s_type = t1.val; } t.type = s_type; t.name = s_name; // { if (!Expect(Token.Type.Keyword, "{")) { return(null); } // definitions Token next = tn.Peek(); while (next.type == Token.Type.ID) { Token ID = tn.Next(); // chomp ID if (!Expect(Token.Type.Keyword, "=")) { return(null); } NodeExpression expr = ParseExpression(t); if (!Expect(Token.Type.Keyword, ";")) { return(null); } t.AddDefinition(new NodeDefinition(t, ID.val, expr)); next = tn.Peek(); } // child tasks next = tn.Peek(); while (next.type == Token.Type.Literal) { NodeTask child = ParseTask(t); t.AddTask(child); next = tn.Peek(); } // } if (!Expect(Token.Type.Keyword, "}")) { return(null); } return(t); }
public BinExpresssion(NodeTask task, NodeExpression left, NodeExpression right) : base(task) { this.left = left; this.right = right; }
public NegExpression(NodeTask task, NodeExpression e) : base(task) { this.e = e; }
public CmpExpression(NodeTask task, NodeExpression a0, NodeExpression a1) : base(task, a0, a1) { }
public ExprAnd(NodeTask task, NodeExpression a0, NodeExpression a1) : base(task, a0, a1) { }
public override bool BindSymbols() { expression = task.GetExpressionOfId(id); return(expression != null); }
public ArithBinExpression(NodeTask task, NodeExpression a0, NodeExpression a1) : base(task, a0, a1) { }
public NodeDefinition(NodeTask task, string name, NodeExpression expression) { this.task = task; this.name = name; this.expression = expression; }