예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
 public LiteralExpression(NodeTask task, string val)
     : base(task)
 {
     this.val = new Value(val);
 }
예제 #7
0
 public IDExpression(NodeTask task, string id)
     : base(task)
 {
     this.id = id;
 }
예제 #8
0
        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);
        }
예제 #9
0
 public CmpExpression(NodeTask task, NodeExpression a0, NodeExpression a1)
     : base(task, a0, a1)
 {
 }
예제 #10
0
 public CollectionExpression(NodeTask task, List <NodeExpression> expressions)
     : base(task)
 {
     this.expressions = expressions;
 }
예제 #11
0
 public ExprAnd(NodeTask task, NodeExpression a0, NodeExpression a1)
     : base(task, a0, a1)
 {
 }
예제 #12
0
 public BinExpresssion(NodeTask task, NodeExpression left, NodeExpression right)
     : base(task)
 {
     this.left  = left;
     this.right = right;
 }
예제 #13
0
 public FcallExpression(NodeTask task, string id, List <NodeExpression> parms)
     : base(task)
 {
     this.id    = id;
     this.parms = parms;
 }
예제 #14
0
 public NodeExpression(NodeTask task)
 {
     this.task = task;
 }
예제 #15
0
 public LiteralExpression(NodeTask task, Value val)
     : base(task)
 {
     this.val = val;
 }
예제 #16
0
 public NegExpression(NodeTask task, NodeExpression e)
     : base(task)
 {
     this.e = e;
 }
예제 #17
0
        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);
        }
예제 #18
0
 public NodeTask(NodeTask parent)
 {
     this.parent = parent;
 }
예제 #19
0
 public AssocReadExpression(NodeTask task, NodeExpression a0, NodeExpression a1)
     : base(task, a0, a1)
 {
 }
예제 #20
0
 public void AddTask(NodeTask task)
 {
     subTasks.Add(task);
     task.parent = this;
 }
예제 #21
0
 public ArithBinExpression(NodeTask task, NodeExpression a0, NodeExpression a1)
     : base(task, a0, a1)
 {
 }
예제 #22
0
 public NodeDefinition(NodeTask task, string name, NodeExpression expression)
 {
     this.task       = task;
     this.name       = name;
     this.expression = expression;
 }