Beispiel #1
0
        Ast.IExpression ParseAddOp(ParseTreeNode node)
        {
            if (node.Term.Name == "AddOp")
            {
                ParseTreeNode   left  = node.ChildNodes[0];
                Ast.IExpression lexpr = ParseMulOp(left);
                if (node.ChildNodes[1].ChildNodes.Count == 0)
                {
                    return(lexpr);
                }
                string       opstring = node.ChildNodes[1].ChildNodes[0].ChildNodes[0].Token.ValueString;
                Ast.BinaryOp op;
                switch (opstring)
                {
                case "+":
                    op = Ast.BinaryOp.Addition; break;

                case "-":
                    op = Ast.BinaryOp.Subtraction; break;

                default:
                    throw new Exception("Invalid operator");
                }

                ParseTreeNode   right = node.ChildNodes[1].ChildNodes[1];
                Ast.IExpression rexpr = ParseMulOp(right);

                return(new Ast.BinaryExpression()
                {
                    Left = lexpr, Right = rexpr, Operation = op
                });
            }
            throw new Exception("Invalid AddOp node");
        }
Beispiel #2
0
        Ast.FunctionCall ParseFunctionCall(ParseTreeNode node)
        {
            if (node.Term.Name == "FunctionCall")
            {
                Ast.IExpression  expr = ParsePrefix(node.ChildNodes[0]);
                Ast.FunctionCall call = new Ast.FunctionCall();
                call.Arguments = new List <Ast.IExpression>();
                call.Function  = expr;

                var root = node.ChildNodes[1].ChildNodes[0];
                if (root.ChildNodes.Count != 0)
                {
                    root = root.ChildNodes[0];
                    while (true)
                    {
                        call.Arguments.Add(ParseExpression(root.ChildNodes[0]));
                        if (root.ChildNodes.Count == 1)
                        {
                            break;
                        }
                        else
                        {
                            root = root.ChildNodes[1];
                        }
                    }
                }
                return(call);
            }
            throw new Exception("Invalid FunctionCall node");
        }
Beispiel #3
0
        Ast.UnaryExpression ParseUnaryExpr(ParseTreeNode node)
        {
            if (node.Term.Name == "UnaryExpr")
            {
                Ast.IExpression expr   = ParseExpression(node.ChildNodes[1]);
                var             opNode = node.ChildNodes[0].ChildNodes[0];
                Ast.UnaryOp     op     = Ast.UnaryOp.Invert;
                switch (opNode.Token.ValueString)
                {
                case "not":
                    op = Ast.UnaryOp.Negate; break;

                case "-":
                    op = Ast.UnaryOp.Invert; break;

                case "#":
                    op = Ast.UnaryOp.Length; break;
                }
                return(new Ast.UnaryExpression()
                {
                    Expression = expr,
                    Operation = op
                });
            }
            throw new Exception("Invalid UnaryExpr node");
        }
Beispiel #4
0
 Ast.IAssignable ParseVariable(ParseTreeNode node)
 {
     if (node.Term != null)
     {
         if (node.ChildNodes.Count == 1)
         {
             string name = node.ChildNodes[0].Token.ValueString;
             return(new Ast.Variable()
             {
                 Name = name
             });
         }
         else
         {
             Ast.IExpression prefix = ParsePrefix(node.ChildNodes[0]);
             if (node.ChildNodes[1].Term.Name == "Expression")
             {
                 Ast.IExpression index = ParseExpression(node.ChildNodes[1]);
                 return(new Ast.TableAccess()
                 {
                     Expression = prefix, Index = index
                 });
             }
             else
             {
                 string name = node.ChildNodes[1].Token.ValueString;
                 return(new Ast.Variable()
                 {
                     Name = name, Prefix = prefix
                 });
             }
         }
     }
     throw new Exception("Invalid Variable node");
 }
Beispiel #5
0
        Ast.FunctionCall ParseOopCall(ParseTreeNode node)
        {
            if (node.Term.Name == "OopCall")
            {
                Ast.IExpression  expr = ParsePrefix(node.ChildNodes[0]);
                string           name = node.ChildNodes[1].Token.ValueString;
                Ast.FunctionCall call = new Ast.FunctionCall();
                call.Arguments.Add(expr);
                call.Function = new Ast.Variable()
                {
                    Name = name, Prefix = expr
                };

                var root = node.ChildNodes[2].ChildNodes[0];
                if (root.ChildNodes.Count != 0)
                {
                    root = root.ChildNodes[0];
                    while (true)
                    {
                        call.Arguments.Add(ParseExpression(root.ChildNodes[0]));
                        if (root.ChildNodes.Count == 1)
                        {
                            break;
                        }
                        else
                        {
                            root = root.ChildNodes[1];
                        }
                    }
                }
                return(call);
            }
            throw new Exception("Invalid OopCall node");
        }
        private Ast.IExpression ParseCallExpression(Ast.IExpression function)
        {
            var exp = new Ast.CallExpression
            {
                Token     = _curToken,
                Function  = function,
                Arguments = ParseExpressionList(TokenType.Rparen)
            };

            return(exp);
        }
Beispiel #7
0
        Ast.RepeatStat ParseRepeat(ParseTreeNode node)
        {
            if (node.Term.Name == "Repeat")
            {
                Ast.Block       block     = ParseBlock(node.ChildNodes[1]);
                Ast.IExpression condition = ParseExpression(node.ChildNodes[3]);

                return(new Ast.RepeatStat()
                {
                    Block = block,
                    Condition = condition
                });
            }
            throw new Exception("Invalid Repeat node");
        }
        private Ast.IExpression ParseIndexExpression(Ast.IExpression left)
        {
            var exp = new Ast.IndexExpression {
                Token = _curToken, Left = left
            };

            NextToken();
            exp.Index = ParseExpression(Precedence.Lowest);

            if (!ExpectPeek(TokenType.Rbracket))
            {
                return(null);
            }

            return(exp);
        }
        private Ast.IExpression ParseInfixExpression(Ast.IExpression left)
        {
            var expression = new Ast.InfixExpression
            {
                Token    = _curToken,
                Operator = _curToken.Literal,
                Left     = left
            };

            var precedence = CurPrecedence();

            NextToken();
            expression.Right = ParseExpression(precedence);

            return(expression);
        }
Beispiel #10
0
        Ast.IExpression ParseRelOp(ParseTreeNode node)
        {
            if (node.Term.Name == "RelOp")
            {
                ParseTreeNode   left  = node.ChildNodes[0];
                Ast.IExpression lexpr = ParseConcatOp(left);
                if (node.ChildNodes[1].ChildNodes.Count == 0)
                {
                    return(lexpr);
                }
                string       opstring = node.ChildNodes[1].ChildNodes[0].ChildNodes[0].Token.ValueString;
                Ast.BinaryOp op;
                switch (opstring)
                {
                case ">":
                    op = Ast.BinaryOp.GreaterThan; break;

                case ">=":
                    op = Ast.BinaryOp.GreaterOrEqual; break;

                case "<":
                    op = Ast.BinaryOp.LessThan; break;

                case "<=":
                    op = Ast.BinaryOp.LessOrEqual; break;

                case "==":
                    op = Ast.BinaryOp.Equal; break;

                case "~=":
                    op = Ast.BinaryOp.Different; break;

                default:
                    throw new Exception("Invalid operator");
                }

                ParseTreeNode   right = node.ChildNodes[1].ChildNodes[1];
                Ast.IExpression rexpr = ParseConcatOp(right);

                return(new Ast.BinaryExpression()
                {
                    Left = lexpr, Right = rexpr, Operation = op
                });
            }
            throw new Exception("Invalid RelOp node");
        }
Beispiel #11
0
        Ast.IExpression ParseAndOp(ParseTreeNode node)
        {
            if (node.Term.Name == "AndOp")
            {
                ParseTreeNode   left  = node.ChildNodes[0];
                Ast.IExpression lexpr = ParseRelOp(left);
                if (node.ChildNodes[1].ChildNodes.Count == 0)
                {
                    return(lexpr);
                }
                ParseTreeNode   right = node.ChildNodes[1].ChildNodes[1];
                Ast.IExpression rexpr = ParseRelOp(right);

                return(new Ast.BinaryExpression()
                {
                    Left = lexpr, Right = rexpr, Operation = Ast.BinaryOp.And
                });
            }
            throw new Exception("Invalid AndOp node");
        }
        private Ast.AssignExpression ParseAssignExpression(Ast.IExpression left)
        {
            if (left.GetType() != typeof(Ast.Identifier))
            {
                var msg = $"{_peekToken.TokenPosition()} expected identifier on left but got {left}";
                Errors.Add(msg);
                return(null);
            }

            var expression = new Ast.AssignExpression
            {
                Token = _curToken,
                Name  = (Ast.Identifier)left
            };

            NextToken();

            expression.Value = ParseExpression(Precedence.Lowest);

            return(expression);
        }
Beispiel #13
0
        Ast.IExpression ParsePowerOp(ParseTreeNode node)
        {
            if (node.Term.Name == "PowerOp")
            {
                ParseTreeNode   left  = node.ChildNodes[0];
                Ast.IExpression lexpr = ParseExpression(left);
                if (node.ChildNodes[1].ChildNodes.Count == 0)
                {
                    return(lexpr);
                }

                ParseTreeNode   right = node.ChildNodes[1].ChildNodes[1];
                Ast.IExpression rexpr = ParseExpression(right);

                return(new Ast.BinaryExpression()
                {
                    Span = node.Span, Left = lexpr, Right = rexpr, Operation = Ast.BinaryOp.Power
                });
            }
            throw new Exception("Invalid PowerOp node");
        }
Beispiel #14
0
        Ast.IfStat ParseIf(ParseTreeNode node)
        {
            if (node.Term.Name == "If")
            {
                Ast.IExpression condition = ParseExpression(node.ChildNodes[1]);
                Ast.Block       block     = ParseBlock(node.ChildNodes[3]);

                Ast.IfStat If = new Ast.IfStat();
                If.Block     = block;
                If.Condition = condition;
                If.ElseIfs   = new List <Ast.IfStat>();
                If.Span      = node.Span;

                ParseTreeNode ElseifNode = node.ChildNodes[4];
                ParseTreeNode ElseNode   = node.ChildNodes[5];

                while (ElseifNode.ChildNodes.Count != 0)
                {
                    var        childnode = ElseifNode.ChildNodes[0];
                    Ast.IfStat elseif    = new Ast.IfStat();
                    elseif.Span      = childnode.Span;
                    elseif.Condition = ParseExpression(childnode.ChildNodes[1]);
                    elseif.Block     = ParseBlock(childnode.ChildNodes[3]);

                    If.ElseIfs.Add(elseif);

                    ElseifNode = childnode.ChildNodes[4];
                }

                if (ElseNode.ChildNodes.Count != 0)
                {
                    If.ElseBlock = ParseBlock(ElseNode.ChildNodes[0].ChildNodes[1]);
                }

                return(If);
            }
            throw new Exception("Invalid If node");
        }
Beispiel #15
0
        Ast.IExpression ParseConcatOp(ParseTreeNode node)
        {
            if (node.Term.Name == "ConcatOp")
            {
                ParseTreeNode   left  = node.ChildNodes[0];
                Ast.IExpression lexpr = ParseAddOp(left);
                if (node.ChildNodes[1].ChildNodes.Count == 0)
                {
                    return(lexpr);
                }
                string       opstring = node.ChildNodes[1].ChildNodes[0].Token.ValueString;
                Ast.BinaryOp op       = Ast.BinaryOp.Concat;

                ParseTreeNode   right = node.ChildNodes[1].ChildNodes[1];
                Ast.IExpression rexpr = ParseAddOp(right);

                return(new Ast.BinaryExpression()
                {
                    Left = lexpr, Right = rexpr, Operation = op
                });
            }
            throw new Exception("Invalid ConcatOp node");
        }
Beispiel #16
0
        Ast.IExpression ParseMulOp(ParseTreeNode node)
        {
            if (node.Term.Name == "MulOp")
            {
                ParseTreeNode   left  = node.ChildNodes[0];
                Ast.IExpression lexpr = ParsePowerOp(left);
                if (node.ChildNodes[1].ChildNodes.Count == 0)
                {
                    return(lexpr);
                }
                string       opstring = node.ChildNodes[1].ChildNodes[0].ChildNodes[0].Token.ValueString;
                Ast.BinaryOp op;
                switch (opstring)
                {
                case "*":
                    op = Ast.BinaryOp.Multiplication; break;

                case "/":
                    op = Ast.BinaryOp.Division; break;

                case "%":
                    op = Ast.BinaryOp.Modulo; break;

                default:
                    throw new Exception("Invalid operator");
                }

                ParseTreeNode   right = node.ChildNodes[1].ChildNodes[1];
                Ast.IExpression rexpr = ParsePowerOp(right);

                return(new Ast.BinaryExpression()
                {
                    Span = node.Span, Left = lexpr, Right = rexpr, Operation = op
                });
            }
            throw new Exception("Invalid MulOp node");
        }
Beispiel #17
0
        Ast.TableConstructor ParseTableConstruct(ParseTreeNode node)
        {
            if (node.Term.Name == "TableConstruct")
            {
                if (node.ChildNodes.Count == 0)
                {
                    return(new Ast.TableConstructor()
                    {
                        Values = new Dictionary <Ast.IExpression, Ast.IExpression>()
                    });
                }
                else
                {
                    var child = node.ChildNodes[0];
                    Ast.TableConstructor t = new Ast.TableConstructor();
                    t.Values = new Dictionary <Ast.IExpression, Ast.IExpression>();

                    int i = 1;
                    while (true)
                    {
                        if (child.ChildNodes.Count == 0)
                        {
                            break;
                        }

                        var value = child.ChildNodes[0];

                        if (value.ChildNodes.Count == 1)
                        {
                            t.Values.Add(new Ast.NumberLiteral()
                            {
                                Value = i
                            }, ParseExpression(value.ChildNodes[0]));
                            i++;
                        }
                        else
                        {
                            var prefix = value.ChildNodes[0];

                            Ast.IExpression key;
                            if (prefix.ChildNodes[0].Term.Name == "identifier")
                            {
                                key = new Ast.StringLiteral()
                                {
                                    Value = prefix.ChildNodes[0].Token.ValueString
                                }
                            }
                            ;
                            else
                            {
                                key = ParseExpression(prefix.ChildNodes[0]);
                            }

                            var             expr = value.ChildNodes[1];
                            Ast.IExpression val  = ParseExpression(expr);

                            t.Values.Add(key, val);
                        }

                        //child = child.ChildNodes[1].ChildNodes[0];
                        child = child.ChildNodes[1];
                        if (child.ChildNodes.Count == 0)
                        {
                            break;
                        }
                        child = child.ChildNodes[0];
                    }
                    return(t);
                }
            }
            throw new Exception("Invalid TableConstruct node");
        }