Пример #1
0
        public void TestCallHasParameter()
        {
            Parser parser = new Parser ( TestModule.GetTestKernel() );
            parser.Parse ( GetBlockCallTokens () );

            Variable variable = parser.MainBlock[1] as Variable;
            Assert.IsInstanceOfType( typeof(ArrayExpr), variable.Parameters );
        }
Пример #2
0
        static Block ParseIf()
        {
            var tokens = GetIf ();

            IKernel kernel = TestModule.GetTestKernel ();
            Block block = new Parser ( kernel ).Parse ( tokens );
            return block;
        }
Пример #3
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");
        }
Пример #4
0
        public void TestCallEvaluatesParameters()
        {
            Parser parser = new Parser ( TestModule.GetTestKernel() );
            parser.Parse ( GetBlockCallTokens () );

            Variable variable = parser.MainBlock[1] as Variable;
            Dynamic parameters = variable.Parameters.Evaluate();

            Assert.AreEqual ( DynamicType.arrayType, parameters.Type );
        }
Пример #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 TestCallPassesParameters()
        {
            IKernel kernel = TestModule.GetTestKernel();
            Parser parser = new Parser ( kernel );
            parser.Parse ( GetBlockCallTokens () );

            new Executor ( parser.MainBlock );

            // It should give give us the correct value in standard out
            StandardOutDummy output = kernel.Get<IStandardOut> () as StandardOutDummy;
            Assert.AreEqual ( "yay", output.Text );
        }
Пример #7
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 );
        }
Пример #8
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] );
        }
Пример #9
0
        public void Run( Stream program )
        {
            Scanner scanner = new Scanner ( program );

            try
            {
                Parser parser = new Parser ( _kernel );
                Block block = parser.Parse( scanner.Tokens );
                block.Execute ();
            }
            catch ( ParseException ex )
            {
                Console.WriteLine ( ex.Message );
            }
        }
Пример #10
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");
        }
Пример #11
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 );
        }
Пример #12
0
        public void ParseBlockAssignment()
        {
            List<Token> tokens = new List<Token> ();
            tokens.Add ( new Token ( "x" ) );
            tokens.Add ( new Token ( "=" ) );
            tokens.Add ( new Token ( "{" ) );
            tokens.Add ( new Token ( ">" ) );
            tokens.Add ( new Token ( "'bong'" ) );
            tokens.Add ( new Token ( "}" ) );

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

            Assert.IsInstanceOfType ( parser.Statements[0], typeof ( Assign ) );
            Assign ass = parser.Statements[0] as Assign;
            Assert.IsInstanceOfType ( ass.Expr, typeof ( Block ) );
        }
Пример #13
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" );
        }
Пример #14
0
        public void ParsePrintAddingStringsTest()
        {
            List<Token> tokens = new List<Token> ();
            tokens.Add ( new Token ( ">" ) );
            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 ( ( (Print)parser.Statements[0] ).Expr, typeof ( ArithExpr ) );
        }
Пример #15
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 );
        }
Пример #16
0
        public void ParseSimplePrintTest()
        {
            List<Token> tokens = new List<Token>();
            tokens.Add(new Token(">"));
            tokens.Add(new Token("'ongle'"));

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

            Assert.AreEqual(1, parser.Statements.Count);
            Assert.IsInstanceOfType ( parser.Statements[0], typeof ( Print ) );
        }
Пример #17
0
        public void StringLiteralPrintTest()
        {
            List<Token> tokens = new List<Token> ();
            tokens.Add ( new Token ( ">" ) );
            tokens.Add ( new Token ( "'ongle'" ) );

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

            Assert.IsInstanceOfType ( parser.Statements[0], typeof ( Print ) );
            Print print = (Print)parser.Statements[0];
            Assert.IsInstanceOfType ( print.Expr, typeof ( StringLiteral ) );
            Assert.AreEqual ( ((StringLiteral)print.Expr).Value, "ongle" );
        }
Пример #18
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;
        }
Пример #19
0
        public void VariableAssignmentTest()
        {
            List<Token> tokens = new List<Token> ();
            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.Statements.Count );
            Assert.IsInstanceOfType ( parser.Statements[0], typeof ( Assign ) );
            Assert.AreEqual ( ((Assign)parser.Statements[0]).Ident, "ong" );
        }
Пример #20
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] );
        }
Пример #21
0
        public void ParseBlockCall()
        {
            List<Token> tokens = new List<Token> ();
            tokens.Add ( new Token ( "x" ) );
            tokens.Add ( new Token ( "=" ) );
            tokens.Add ( new Token ( "{" ) );
            tokens.Add ( new Token ( ">" ) );
            tokens.Add ( new Token ( "'bong'" ) );
            tokens.Add ( new Token ( "}" ) );
            tokens.Add ( new Token ( "x" ) );

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

            Assert.IsInstanceOfType ( parser.Statements[1], typeof ( Variable ) );
            Assert.AreEqual ( "x", (parser.Statements[1] as Variable).Ident );
        }
Пример #22
0
        //[TestMethod ()]
        //public void ParseSimpleIfHasCorrectExpressionTest ()
        //{
        //    Parser parser = ParseSimpleIf ();
        //    Assert.AreEqual ( 1, parser.Statements.Count );
        //    Assert.IsInstanceOfType ( (parser.Statements[0] as If).Test, typeof ( Comparison ) );
        //}
        private static Parser ParseSimpleIf()
        {
            List<Token> tokens = new List<Token> ();
            tokens.Add ( new Token ( "?" ) );
            tokens.Add ( new Token ( "x" ) );
            tokens.Add ( new Token ( "==" ) );
            tokens.Add ( new Token ( "5" ) );
            tokens.Add ( new Token ( ">" ) );
            tokens.Add ( new Token ( "'yay'" ) );
            tokens.Add ( new Token ( "." ) );

            Parser parser = new Parser ( TestModule.GetTestKernel () );
            parser.Parse ( tokens );
            return parser;
        }
Пример #23
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" );
        }
Пример #24
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] );
        }