Пример #1
0
        private Tokens GetBlockCallTokens()
        {
            Tokens tokens = new Tokens();
            tokens.AddTokens ( new string[] { "x", "=", "{", "print", "$", "[", "0", "]", "}", "x", "(", "'yay'", ")" } );

            return tokens;
        }
Пример #2
0
        public void ParseBlock( Block block, Tokens tokens )
        {
            bool enclosed = false;
            if ( tokens.PeekToken () == BeginBlock )
            {
                enclosed = true;
                tokens.RemoveNextToken ( BeginBlock );
            }

            while ( !tokens.AtEnd () )
            {
                if ( enclosed && tokens.PeekToken () == EndBlock )
                {
                    tokens.RemoveNextToken ( EndBlock );
                    break;
                }

                bool parsed = false;
                foreach ( IStatementParser parser in _parsers )
                {
                    IStatement nextStatement;
                    if ( parser.TryParse( tokens, block.Scope, out nextStatement ) )
                    {
                        block.Add ( nextStatement );
                        parsed = true;
                        break;
                    }
                }

                if (!parsed)
                    throw new Exception("Unable to parse token " + tokens.PeekToken() );
            }
        }
Пример #3
0
        public Block GetBlock( IScope scope, Tokens tokens )
        {
            Block newBlock = new Block ();
            newBlock.Scope = scope;
            ParseBlock ( newBlock, tokens );

            return newBlock;
        }
Пример #4
0
        public void ParseNestedBlockTest()
        {
            Tokens tokens = new Tokens ();
            tokens.AddTokens ( new string[] { "x", "=", "{", "if", "x", "==", "5", "{", "print", "x", "}", "}", "print", "x" } );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            Block block = parser.Parse ( tokens );

            Assert.AreEqual ( 2, block.Count, "Should be two statements, one assign, one print");
        }
Пример #5
0
        public void ParseBlockAssignment()
        {
            Tokens tokens = new Tokens ();
            tokens.AddTokens ( new string[] { "x", "=", "{", "print", "'bong'", "}" } );
            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );

            Assert.IsInstanceOfType ( typeof ( Assign ), parser.MainBlock[0] );
            Assign ass = parser.MainBlock[0] as Assign;
            Assert.IsInstanceOfType ( typeof ( Block ), ass.Expr );
        }
Пример #6
0
        public void ParseBlockCall()
        {
            Tokens tokens = new Tokens ();
            tokens.AddTokens ( new string[] { "x", "=", "{", "print", "'bong'", "}", "x" } );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );

            Assert.IsInstanceOfType ( typeof ( Assign ), parser.MainBlock[0] );
            Assert.IsInstanceOfType ( typeof ( Variable ), parser.MainBlock[1] );
            Assert.AreEqual ( "x", (parser.MainBlock[1] as Variable).Ident );
        }
Пример #7
0
        public void ParsePrintAddingStringsTest()
        {
            Tokens tokens = new Tokens ();
            tokens.Add ( new Token ( "print" ) );
            tokens.Add ( new Token ( "'ongle'" ) );
            tokens.Add ( new Token ( "+" ) );
            tokens.Add ( new Token ( "'ooog'" ) );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );

            Assert.IsInstanceOfType ( typeof ( ArithExpr ), ( (Print)parser.MainBlock[0] ).Expr );
        }
Пример #8
0
        /// <summary>
        /// Start parsing the main block. 
        /// </summary>
        /// <param name="tokens">
        /// A <see cref="Tokens"/>
        /// </param>
        /// <returns>
        /// A <see cref="Block"/>
        /// </returns>
        public Block Parse( Tokens tokens )
        {
            IBlockParser blockParser = _kernel.Get<IBlockParser> ();

            blockParser.Parsers.Add ( _kernel.Get<PrintParser> () );
            blockParser.Parsers.Add ( _kernel.Get<IfParser> () );
            blockParser.Parsers.Add ( _kernel.Get<AssignmentParser> () );
            blockParser.Parsers.Add ( _kernel.Get<CallParser> () );

            blockParser.ParseBlock ( MainBlock, tokens );

            return MainBlock;
        }
Пример #9
0
        static Tokens GetIf()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken ("if");
            tokens.AddToken ("x");
            tokens.AddToken ("<");
            tokens.AddToken ("10");
            tokens.AddToken ("{");
            tokens.AddToken ("a");
            tokens.AddToken ("}");

            return tokens;
        }
Пример #10
0
        public void TestBlockPassesAsParameter()
        {
            Tokens tokens = new Tokens();
            tokens.AddTokens(new string[] {"x", "(", "{", "print", "'yay'","}",")"});

            IKernel kernel = TestModule.GetTestKernel();
            Parser parser = new Parser ( kernel );
            parser.Parse ( tokens );

            Variable variable = parser.MainBlock[0] as Variable;
            ArrayExpr array = variable.Parameters;

            Assert.AreEqual ( 1, array.Elements.Count, "Should only be one parameter" );
            Assert.IsInstanceOfType ( typeof(Block), array.Elements[0] );
        }
Пример #11
0
        public void TestSetRollbackPoint()
        {
            Tokens tokens = new Tokens();
            tokens.AddTokens ( new string[] { "1", "2", "3", "4", "5" } );

            Assert.AreEqual ( "1", tokens.PeekToken () );

            tokens.SetMark ();
            Assert.AreEqual ( "1", tokens.PullToken () );
            Assert.AreEqual ( "2", tokens.PullToken () );
            Assert.AreEqual ( "3", tokens.PullToken () );

            tokens.RollbackToMark ();
            Assert.AreEqual ( "1", tokens.PeekToken () );
        }
Пример #12
0
        public bool TryParse(Tokens tokens, IScope scope, out IStatement statement)
        {
            if ( tokens.PeekToken () == IfTag )
            {
                tokens.RemoveNextToken ( IfTag );
                If iff = new If ( _executorFactory.GetIfExecutor ());
                iff.Test = _expressionParser.ParseExpression ( scope, tokens );
                iff.Body = _blockParser.GetBlock ( scope, tokens );

                statement = iff;
                return true;
            }

            statement = null;
            return false;
        }
Пример #13
0
        public Expression ParseArray(IScope scope, Tokens tokens)
        {
            var result = new ArrayExpr( _executorFactory.GetArrayExecutor() );

            tokens.RemoveNextToken(BeginArray);

            while (tokens.PeekToken() != EndArray)
            {
                result.Elements.Add(this.ParseValue(scope, tokens));
                tokens.RemoveNextToken(ArraySeparator);
            }

            tokens.RemoveNextToken(EndArray);

            return result;
        }
Пример #14
0
        public bool TryParse(Tokens tokens, IScope scope, out IStatement statement)
        {
            if ( tokens.PeekToken () == PrintTag )
            {
                // Print statement
                tokens.RemoveNextToken ( PrintTag );
                Print print = new Print( _executorFactory.GetPrintExecutor ());
                print.Expr = _expressionParser.ParseExpression ( scope, tokens );

                statement = print;
                return true;
            }

            statement = null;
            return false;
        }
Пример #15
0
        public void TestParseArrayIndex()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken("x");
            tokens.AddToken("[");
            tokens.AddToken("0");
            tokens.AddToken("]");

            Parser parser = new Parser ( TestModule.GetTestKernel() );
            parser.Parse ( tokens );

            Variable variable = parser.MainBlock[0] as Variable;
            Dynamic indexer = variable.Indexer.Evaluate ();

            Assert.AreEqual(0, indexer.NumberValue, "Indexer should evaluate to 0");
        }
Пример #16
0
        public void ExpressionAssignmentTest()
        {
            Tokens tokens = new Tokens ();
            tokens.Add ( new Token ( "ong" ) );
            tokens.Add ( new Token ( "=" ) );
            tokens.Add ( new Token ( "bong" ) );
            tokens.Add ( new Token ( "+" ) );
            tokens.Add ( new Token ( "10" ) );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );

            ArithExpr expression = (ArithExpr)( (Assign)parser.MainBlock[0] ).Expr;
            Assert.IsInstanceOfType ( typeof ( Variable ), expression.Left);
            Assert.AreEqual ( ArithOp.Add, expression.Op );
            Assert.AreEqual ( 10, ((NumberLiteral) expression.Right).Value );
        }
Пример #17
0
        public Expression ParseExpression( IScope scope, Tokens tokens )
        {
            Expression leftExpression = null;

            // Get the left value
            leftExpression = ParseValue ( scope, tokens );

            if ( leftExpression == null )
            {
                throw new Exception ( "Expecting a value" );
            }

            // Check if there is an operator which will continue the expression
            if ( !tokens.AtEnd () )
            {
                ArithOp op = ParseOperator ( tokens );
                if ( op != ArithOp.none )
                {
                    Expression rightExpression = ParseExpression ( scope, tokens );

                    if ( rightExpression != null )
                    {
                        ArithExpr arithExpression = new ArithExpr
                        {
                            Scope = scope,
                            Left = leftExpression,
                            Op = op,
                            Right = rightExpression
                        };

                        return arithExpression;
                    }
                }
            }

            return leftExpression;
        }
Пример #18
0
        public bool TryParse(Tokens tokens, IScope scope, out IStatement statement)
        {
            tokens.SetMark ();

            Expression value = _expressionParser.ParseValue ( scope, tokens );

            // Look ahead to see if this is an assignment
            if ( value is Variable && tokens.PullTokenIfEqual ( "=" ) )
            {
                Assign assign = new Assign ( _executorFactory.GetAssignExecutor() );
                assign.Ident = value as Variable;

                Expression expression = _expressionParser.ParseExpression ( scope, tokens );
                assign.Expr = expression;

                statement = assign;
                return true;
            }

            // Failed to parse as assignment, rollback.
            tokens.RollbackToMark ();
            statement = null;
            return false;
        }
Пример #19
0
        public bool TryParse(Tokens tokens, IScope scope, out IStatement statement)
        {
            // This is a block call
            Variable variable = new Variable ( _executorFactory.GetVariableExecutor() );
            variable.Scope = scope;
            variable.Ident = tokens.PullToken ();

            // Check if there is an indexer into the variable
            if (tokens.PeekToken () == "[")
            {
                tokens.PullToken ();
                variable.Indexer = _expressionParser.ParseExpression ( scope, tokens );
                tokens.RemoveNextToken ("]");
            }

            if ( tokens.NextTokenIs("(") )
            {
                // Parse the parameters..
                variable.Parameters = _expressionParser.ParseArray( scope, tokens ) as ArrayExpr;
            }

            statement = variable;
            return true;
        }
Пример #20
0
        static Tokens CreateSimpleArray()
        {
            Tokens tokens = new Tokens ();
            tokens.Add ( new Token ( "(" ) );
            tokens.Add ( new Token ( "'a'" ) );
            tokens.Add ( new Token ( "," ) );
            tokens.Add ( new Token ( "'b'" ) );
            tokens.Add ( new Token ( "," ) );
            tokens.Add ( new Token ( "3" ) );
            tokens.Add ( new Token ( ")" ) );

            return tokens;
        }
Пример #21
0
        public void TestParseVariableWithArrayIndexExpression()
        {
            Tokens tokens = new Tokens();
            tokens.AddToken("x");
            tokens.AddToken("[");
            tokens.AddToken("0");
            tokens.AddToken("+");
            tokens.AddToken("4");
            tokens.AddToken("]");

            Parser parser = new Parser ( TestModule.GetTestKernel() );
            parser.Parse ( tokens );

            Assert.AreEqual(1, parser.MainBlock.Count);
            Assert.IsInstanceOfType ( typeof ( Variable ), parser.MainBlock[0] );
        }
Пример #22
0
        public void ParseSimplePrintTest()
        {
            Tokens tokens = new Tokens();
            tokens.Add(new Token("print"));
            tokens.Add(new Token("'ongle'"));

            Parser parser = new Parser ( TestModule.GetTestKernel() );
            parser.Parse ( tokens );

            Assert.AreEqual(1, parser.MainBlock.Count);
            Assert.IsInstanceOfType ( typeof ( Print ), parser.MainBlock[0] );
        }
Пример #23
0
        /// <summary>
        /// Value is a constant string, number or variable name.
        /// </summary>
        /// <param name="tokens"></param>
        /// <returns></returns>
        public Expression ParseValue( IScope scope, Tokens tokens )
        {
            Expression value = null;

            if ( tokens.NextTokenIs ( BeginBlock ) )
            {
                value = _blockParser.GetBlock ( scope, tokens );
            }
            else if ( tokens.PeekToken().StartsWith ( "'" ) && tokens.PeekToken().EndsWith ( "'" ) )
            {
                string literal = tokens.PullToken().Trim ( '\'' );
                value = new StringLiteral
                {
                    Value = literal
                };
            }
            else if ( tokens.PeekToken().IsNumber () )
            {
                value = new NumberLiteral
                {
                    Value = double.Parse ( tokens.PullToken() )
                };
            }
            else if ( tokens.NextTokenIs(BeginArray) )
            {
                return this.ParseArray ( scope, tokens );
            }
            else
            {
                var variable = new Variable ( _executorFactory.GetVariableExecutor () );
                variable.Scope = scope;
                variable.Ident = tokens.PullToken();

                // Check if there is an indexer into the variable
                if (tokens.PeekToken () == "[")
                {
                    tokens.PullToken ();
                    variable.Indexer = ParseExpression ( scope, tokens );
                    tokens.RemoveNextToken ("]");
                }

                value = variable;
            }

            return value;
        }
Пример #24
0
        public void StringLiteralPrintTest()
        {
            Tokens tokens = new Tokens ();
            tokens.Add ( new Token ( "print" ) );
            tokens.Add ( new Token ( "'ongle'" ) );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );

            Assert.IsInstanceOfType ( typeof ( Print ), parser.MainBlock[0] );
            Print print = (Print)parser.MainBlock[0];
            Assert.IsInstanceOfType ( typeof ( StringLiteral ), print.Expr );
            Assert.AreEqual ( ((StringLiteral)print.Expr).Value, "ongle" );
        }
Пример #25
0
        private ArithOp ParseOperator( Tokens tokens )
        {
            if ( tokens.PullTokenIfEqual ( "+" ) )
            {
                return ArithOp.Add;
            }
            else if ( tokens.PullTokenIfEqual ( "-" ) )
            {
                return ArithOp.Sub;
            }
            else if ( tokens.PullTokenIfEqual ( "*" ) )
            {
                return ArithOp.Mul;
            }
            else if ( tokens.PullTokenIfEqual ( "/" ) )
            {
                return ArithOp.Div;
            }
            else if ( tokens.PullTokenIfEqual ( "==" ) )
            {
                return ArithOp.Equality;
            }
            else if ( tokens.PullTokenIfEqual ( "<" ) )
            {
                return ArithOp.LessThan;
            }
            else if ( tokens.PullTokenIfEqual ( ">" ) )
            {
                return ArithOp.GreaterThan;
            }

            return ArithOp.none;
        }
Пример #26
0
        public void VariableAssignmentTest()
        {
            Tokens tokens = new Tokens ();
            tokens.Add ( new Token ( "ong" ) );
            tokens.Add ( new Token ( "=" ) );
            tokens.Add ( new Token ( "10" ) );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );

            Assert.AreEqual ( 1, parser.MainBlock.Count );
            Assert.IsInstanceOfType ( typeof ( Assign ), parser.MainBlock[0] );

            Assign assign = parser.MainBlock[0] as Assign;
            Assert.IsInstanceOfType ( typeof ( Variable ), assign.Ident );

            Assert.AreEqual ( ((Variable)assign.Ident).Ident, "ong" );
        }
Пример #27
0
        private static Parser ParseSimpleIf()
        {
            Tokens tokens = new Tokens ();
            tokens.Add ( new Token ( "if" ) );
            tokens.Add ( new Token ( "x" ) );
            tokens.Add ( new Token ( "==" ) );
            tokens.Add ( new Token ( "5" ) );
            tokens.Add ( new Token ( "{" ) );
            tokens.Add ( new Token ( "print" ) );
            tokens.Add ( new Token ( "'yay'" ) );
            tokens.Add ( new Token ( "}" ) );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );
            return parser;
        }