Example #1
0
        public static void Main(string[] args)
        {
            PythonSharp.Machine machine = new PythonSharp.Machine();

            if (args != null && args.Length > 0)
                if (ProcessFiles(args, machine))
                    return;

            PrintIntro();

            Parser parser = new Parser(System.Console.In);

            while (true)
            {
                try
                {
                    ICommand command = parser.CompileCommand();

                    if (command == null)
                        break;

                    if (command is ExpressionCommand)
                    {
                        IExpression expr = ((ExpressionCommand)command).Expression;
                        var value = expr.Evaluate(machine.Environment);

                        if (value != null)
                            Console.WriteLine(ValueUtilities.AsPrintString(value));
                    }
                    else
                        command.Execute(machine.Environment);
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
            }
        }
Example #2
0
        public void CompileSimpleForMultilineCommand()
        {
            Parser parser = new Parser("for a in [1,2,3]:\r\n  print(a)");

            var command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ForCommand));

            var forcmd = (ForCommand)command;
            Assert.IsNotNull(forcmd.Command);
            Assert.IsInstanceOfType(forcmd.Command, typeof(ExpressionCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #3
0
        public void CompileImportFromCommand()
        {
            Parser parser = new Parser("from module import a, b");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ImportFromCommand));

            ImportFromCommand impcmd = (ImportFromCommand)command;

            Assert.AreEqual("module", impcmd.ModuleName);

            Assert.IsNotNull(impcmd.Names);
            Assert.AreEqual(2, impcmd.Names.Count);
            Assert.AreEqual("a", impcmd.Names.First());
            Assert.AreEqual("b", impcmd.Names.Skip(1).First());
        }
Example #4
0
        public void CompileImportCommandWithDottedName()
        {
            Parser parser = new Parser("import PythonSharp.Language");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ImportCommand));

            ImportCommand impcmd = (ImportCommand)command;

            Assert.AreEqual("PythonSharp.Language", impcmd.ModuleName);
        }
Example #5
0
        public void CompileIfCommandWithSingleThenCommandSameLine()
        {
            Parser parser = new Parser("if a: print(a)");

            ICommand cmd = parser.CompileCommand();

            Assert.IsNotNull(cmd);
            Assert.IsInstanceOfType(cmd, typeof(IfCommand));

            IfCommand ifcmd = (IfCommand)cmd;

            Assert.IsNotNull(ifcmd.Condition);
            Assert.IsInstanceOfType(ifcmd.Condition, typeof(NameExpression));
            Assert.IsNotNull(ifcmd.ThenCommand);
            Assert.IsInstanceOfType(ifcmd.ThenCommand, typeof(ExpressionCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #6
0
        public void CompileExpressionCommandWithName()
        {
            Parser parser = new Parser("a+2");
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("a", 1);

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ExpressionCommand));
            command.Execute(environment);

            ExpressionCommand exprcommand = (ExpressionCommand)command;
            Assert.IsNotNull(exprcommand.Expression);
            Assert.AreEqual(3, exprcommand.Expression.Evaluate(environment));
        }
Example #7
0
        public void CompileExpressionCommand()
        {
            Parser parser = new Parser("1+2");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ExpressionCommand));
            command.Execute(null);

            ExpressionCommand exprcommand = (ExpressionCommand)command;
            Assert.IsNotNull(exprcommand.Expression);
            Assert.AreEqual(3, exprcommand.Expression.Evaluate(null));
        }
Example #8
0
        public void CompileEmptyPrintFunction()
        {
            Parser parser = new Parser("print()");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ExpressionCommand));

            var exprcmd = (ExpressionCommand)command;

            Assert.IsNotNull(exprcmd.Expression);
            Assert.IsInstanceOfType(exprcmd.Expression, typeof(CallExpression));

            var callexpr = (CallExpression)exprcmd.Expression;

            Assert.IsNotNull(callexpr.TargetExpression);
            Assert.IsInstanceOfType(callexpr.TargetExpression, typeof(NameExpression));
            Assert.IsNull(callexpr.ArgumentExpressions);
        }
Example #9
0
 public void RaiseIfCompileImportCommandWithoutModuleName()
 {
     Parser parser = new Parser("import");
     parser.CompileCommand();
 }
Example #10
0
 public void RaiseIfCompileIfHasNoCommand()
 {
     Parser parser = new Parser("if 0:");
     parser.CompileCommand();
 }
Example #11
0
        public void CompileWhileCommandWithSingleCommandSameLine()
        {
            Parser parser = new Parser("while a: print(a)");

            ICommand cmd = parser.CompileCommand();

            Assert.IsNotNull(cmd);
            Assert.IsInstanceOfType(cmd, typeof(WhileCommand));

            WhileCommand whilecmd = (WhileCommand)cmd;

            Assert.IsNotNull(whilecmd.Condition);
            Assert.IsInstanceOfType(whilecmd.Condition, typeof(NameExpression));
            Assert.IsNotNull(whilecmd.Command);
            Assert.IsInstanceOfType(whilecmd.Command, typeof(ExpressionCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #12
0
        public void CompileWhileCommandWithCompositeCommand()
        {
            Parser parser = new Parser("while a:\r\n  print(a)\r\n  print(b)");

            ICommand cmd = parser.CompileCommand();

            Assert.IsNotNull(cmd);
            Assert.IsInstanceOfType(cmd, typeof(WhileCommand));

            WhileCommand whilecmd = (WhileCommand)cmd;

            Assert.IsNotNull(whilecmd.Condition);
            Assert.IsInstanceOfType(whilecmd.Condition, typeof(NameExpression));
            Assert.IsNotNull(whilecmd.Command);
            Assert.IsInstanceOfType(whilecmd.Command, typeof(CompositeCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #13
0
        public void CompileTwoSimpleDefFunction()
        {
            Parser parser = new Parser("def foo(a):\r\n    print(a)\r\ndef bar(b):\r\n    print(b)");

            var command = parser.CompileCommand();
            Assert.IsInstanceOfType(command, typeof(DefCommand));
            command = parser.CompileCommand();
            Assert.IsInstanceOfType(command, typeof(DefCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #14
0
        public void CompileSimpleTryWithFinally()
        {
            Parser parser = new Parser("try:\r\n  pass\r\nfinally:\r\n  pass");

            ICommand cmd = parser.CompileCommand();

            Assert.IsNotNull(cmd);
            Assert.IsInstanceOfType(cmd, typeof(TryCommand));

            TryCommand trycmd = (TryCommand)cmd;

            Assert.IsNotNull(trycmd.Command);
            Assert.IsInstanceOfType(trycmd.Command, typeof(PassCommand));

            Assert.IsNotNull(trycmd.Finally);
            Assert.IsInstanceOfType(trycmd.Command, typeof(PassCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #15
0
        public void CompileSimpleSetVariable()
        {
            Parser parser = new Parser("a=1");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(SetCommand));

            var scommand = (SetCommand)command;
            Assert.AreEqual("a", scommand.Target);
            Assert.IsInstanceOfType(scommand.Expression, typeof(ConstantExpression));
            Assert.AreEqual(1, ((ConstantExpression)scommand.Expression).Value);
        }
Example #16
0
        public void CompileCallWithNamedArgument()
        {
            Parser parser = new Parser("foo(a=1+2)");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ExpressionCommand));

            var exprcmd = (ExpressionCommand)command;

            Assert.IsNotNull(exprcmd.Expression);
            Assert.IsInstanceOfType(exprcmd.Expression, typeof(CallExpression));

            var callexpr = (CallExpression)exprcmd.Expression;

            Assert.IsNotNull(callexpr.TargetExpression);
            Assert.IsInstanceOfType(callexpr.TargetExpression, typeof(NameExpression));
            Assert.IsNotNull(callexpr.ArgumentExpressions);
            Assert.AreEqual(1, callexpr.ArgumentExpressions.Count);
            Assert.IsInstanceOfType(callexpr.ArgumentExpressions[0], typeof(NamedArgumentExpression));

            var nexpr = (NamedArgumentExpression)callexpr.ArgumentExpressions[0];
            Assert.AreEqual("a", nexpr.Name);
            Assert.IsNotNull(nexpr.Expression);
        }
Example #17
0
        public void CompileContinueCommand()
        {
            Parser parser = new Parser("continue");

            var result = parser.CompileCommand();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ContinueCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #18
0
 public void RaiseIfCompileImportFromCommandWithNoImportWord()
 {
     Parser parser = new Parser("from module imports a, b");
     parser.CompileCommand();
 }
Example #19
0
        public void CompileEmptyReturn()
        {
            Parser parser = new Parser("return");
            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ReturnCommand));

            var retcommand = (ReturnCommand)command;

            Assert.IsNull(retcommand.Expression);
        }
Example #20
0
        public void RaiseIfInvalidSpacesAtStartOfLine()
        {
            Parser parser = new Parser("  a=1");

            try
            {
                parser.CompileCommand();
                Assert.Fail("Expected Exception");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("unexpected indent", ex.Message);
            }
        }
Example #21
0
        public void CompileExpressionCommandWithList()
        {
            Parser parser = new Parser("1, 2");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ExpressionCommand));
            command.Execute(null);

            ExpressionCommand exprcommand = (ExpressionCommand)command;
            Assert.IsNotNull(exprcommand.Expression);
            Assert.IsInstanceOfType(exprcommand.Expression, typeof(ListExpression));
            Assert.IsTrue(((ListExpression)exprcommand.Expression).IsReadOnly);
        }
Example #22
0
        public void RaiseIsCommandIsUnknown()
        {
            Parser parser = new Parser("spam bar");

            ICommand command = parser.CompileCommand();
        }
Example #23
0
        public void CompileIfCommandWithElifCommand()
        {
            Parser parser = new Parser("if a:\r\n  print(a)\r\nelif b:\r\n  print(a)\r\n  print(b)");

            ICommand cmd = parser.CompileCommand();

            Assert.IsNotNull(cmd);
            Assert.IsInstanceOfType(cmd, typeof(IfCommand));

            IfCommand ifcmd = (IfCommand)cmd;

            Assert.IsNotNull(ifcmd.Condition);
            Assert.IsInstanceOfType(ifcmd.Condition, typeof(NameExpression));
            Assert.IsNotNull(ifcmd.ThenCommand);
            Assert.IsNotNull(ifcmd.ElseCommand);
            Assert.IsInstanceOfType(ifcmd.ElseCommand, typeof(IfCommand));

            IfCommand elifcmd = (IfCommand)ifcmd.ElseCommand;

            Assert.IsNotNull(elifcmd.ThenCommand);
            Assert.IsInstanceOfType(elifcmd.ThenCommand, typeof(CompositeCommand));
            Assert.IsNull(elifcmd.ElseCommand);

            Assert.IsNull(parser.CompileCommand());
        }
Example #24
0
        public void RaiseWhenTwoFinallyCommands()
        {
            Parser parser = new Parser("try:\r\n  pass\r\nfinally:\r\n  pass\r\nfinally:\r\n  pass");

            try
            {
                parser.CompileCommand();
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("invalid syntax", ex.Message);
            }
        }
Example #25
0
        public void CompileImportCommand()
        {
            Parser parser = new Parser("import module");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ImportCommand));

            ImportCommand impcmd = (ImportCommand)command;

            Assert.AreEqual("module", impcmd.ModuleName);
        }
Example #26
0
        public void RaiseWhenTwoListArguments()
        {
            Parser parser = new Parser("def foo(*a, *b):");

            try
            {
                parser.CompileCommand();
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(SyntaxError));
                Assert.AreEqual("invalid syntax", ex.Message);
            }
        }
Example #27
0
        public void CompileImportCompositeName()
        {
            Parser parser = new Parser("from System.IO import *");

            ICommand command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ImportFromCommand));

            ImportFromCommand impcmd = (ImportFromCommand)command;

            Assert.AreEqual("System.IO", impcmd.ModuleName);

            Assert.IsNull(impcmd.Names);
        }
Example #28
0
        public void CompileAndExecuteIfCommandWithCompositeThenCommandWithAnotherCommand()
        {
            Parser parser = new Parser("if 0:\r\n  one=1\r\n  two=2\r\nthree=3");
            Machine machine = new Machine();
            ICommand cmd = parser.CompileCommand();

            cmd.Execute(machine.Environment);

            Assert.IsNull(machine.Environment.GetValue("one"));
            Assert.IsNull(machine.Environment.GetValue("two"));

            cmd = parser.CompileCommand();

            Assert.IsNotNull(cmd);

            cmd.Execute(machine.Environment);

            Assert.AreEqual(3, machine.Environment.GetValue("three"));

            Assert.IsNull(parser.CompileCommand());
        }
Example #29
0
        public void CompileBreakCommand()
        {
            Parser parser = new Parser("break");

            var result = parser.CompileCommand();

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(BreakCommand));

            Assert.IsNull(parser.CompileCommand());
        }
Example #30
0
        public void CompileSimpleForCommand()
        {
            Parser parser = new Parser("for a in [1,2,3]: b = b +a");

            var command = parser.CompileCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ForCommand));

            var forcmd = (ForCommand)command;
            Assert.IsNotNull(forcmd.Command);
            Assert.IsInstanceOfType(forcmd.Command, typeof(SetCommand));

            Assert.IsNull(parser.CompileCommand());
        }