Example #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 );
        }
Example #2
0
        static Block ParseIf()
        {
            var tokens = GetIf ();

            IKernel kernel = TestModule.GetTestKernel ();
            Block block = new Parser ( kernel ).Parse ( tokens );
            return block;
        }
Example #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");
        }
Example #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 );
        }
Example #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 );
        }
Example #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 );
        }
Example #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 );
        }
Example #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] );
        }
Example #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 );
            }
        }
Example #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");
        }
Example #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 );
        }
Example #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 ) );
        }
Example #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" );
        }
Example #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 ) );
        }
Example #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 );
        }
Example #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 ) );
        }
Example #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" );
        }
Example #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;
        }
Example #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" );
        }
Example #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] );
        }
Example #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 );
        }
Example #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;
        }
Example #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" );
        }
Example #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] );
        }