Example #1
0
        public void CompileAndEvaluateComplexListExpression()
        {
            Parser parser = new Parser("[1, 2, [a, b], 'spam']");
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("a", 1);
            environment.SetValue("b", 2);

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));
            Assert.IsFalse(((ListExpression)expression).IsReadOnly);

            object result = expression.Evaluate(environment);

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

            IList list = (IList)result;

            Assert.AreEqual(4, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.IsNotNull(list[2]);
            Assert.IsInstanceOfType(list[2], typeof(IList));
            Assert.AreEqual("spam", list[3]);

            IList list2 = (IList)list[2];

            Assert.IsNotNull(list2);
            Assert.AreEqual(2, list2.Count);
            Assert.AreEqual(1, list2[0]);
            Assert.AreEqual(2, list2[1]);
        }
Example #2
0
        public void CompileAndEvaluateAddExpression()
        {
            Parser parser = new Parser("1+2");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BinaryOperatorExpression));
            Assert.AreEqual(3, expression.Evaluate(new BindingEnvironment()));
        }
Example #3
0
        public void EvaluateSetVar()
        {
            Parser parser = new Parser(new StreamReader("setvar.py"));

            ICommand command = parser.CompileCommandList();

            Machine machine = new Machine();

            command.Execute(machine.Environment);

            Assert.AreEqual(1, machine.Environment.GetValue("a"));
        }
Example #4
0
        public void CompileAndEvaluateComplexExpressionWithParenthesis()
        {
            Parser parser = new Parser("(6+3)/(1+2)");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BinaryOperatorExpression));
            Assert.AreEqual(3, expression.Evaluate(new BindingEnvironment()));

            Assert.IsNull(parser.CompileExpression());
        }
Example #5
0
        public void EvaluateImportFrom()
        {
            Parser parser = new Parser(new StreamReader("importfrom.py"));

            ICommand command = parser.CompileCommandList();

            Machine machine = new Machine();

            command.Execute(machine.Environment);

            Assert.AreEqual(1, machine.Environment.GetValue("one"));
            Assert.AreEqual(2, machine.Environment.GetValue("two"));
        }
Example #6
0
        private static bool ProcessFiles(string[] args, Machine machine)
        {
            bool hasfiles = false;

            foreach (var arg in args)
            {
                if (!arg.EndsWith(".py"))
                    continue;

                hasfiles = true;
                Parser parser = new Parser(new StreamReader(arg));
                ICommand command = parser.CompileCommandList();
                command.Execute(machine.Environment);
            }

            return hasfiles;
        }
Example #7
0
        public void EvaluateImport()
        {
            Parser parser = new Parser(new StreamReader("import.py"));

            ICommand command = parser.CompileCommandList();

            Machine machine = new Machine();

            command.Execute(machine.Environment);

            object mod = machine.Environment.GetValue("setvar");

            Assert.IsNotNull(mod);
            Assert.IsInstanceOfType(mod, typeof(IValues));

            IValues modenv = (IValues)mod;

            Assert.AreEqual(1, modenv.GetValue("a"));
        }
Example #8
0
        public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
        {
            int nargs = arguments == null ? 0 : arguments.Count;

            if (nargs == 0)
                throw new TypeError("eval expected at least 1 arguments, got 0");

            // TODO implement bytes or code object
            if (!(arguments[0] is string))
                throw new TypeError("eval() arg 1 must be a string, bytes or code object");

            Parser parser = new Parser((string)arguments[0]);

            IExpression expression = parser.CompileExpression();

            if (expression == null)
                return null;

            return expression.Evaluate(context);
        }
Example #9
0
        public void CompileAndEvaluateArrayAsListExpression()
        {
            Parser parser = new Parser("[1,2,'spam']");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));
            Assert.IsFalse(((ListExpression)expression).IsReadOnly);

            object result = expression.Evaluate(new BindingEnvironment());

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

            IList list = (IList)result;

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual("spam", list[2]);
        }
Example #10
0
        public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
        {
            int nargs = arguments == null ? 0 : arguments.Count;

            if (nargs == 0)
                throw new TypeError("exec expected at least 1 arguments, got 0");

            // TODO implement bytes or code object
            if (!(arguments[0] is string))
                throw new TypeError("exec() arg 1 must be a string, bytes or code object");

            Parser parser = new Parser((string)arguments[0]);

            ICommand command = parser.CompileCommandList();

            if (command == null)
                return null;

            command.Execute(context);

            return null;
        }
Example #11
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 #12
0
        public static Module LoadModule(string name, IContext context)
        {
            Module module = null;

            if (TypeUtilities.IsNamespace(name))
            {
                var types = TypeUtilities.GetTypesByNamespace(name);

                module = new Module(context.GlobalContext);

                foreach (var type in types)
                    module.SetValue(type.Name, type);
            }
            else
            {
                string filename = ModuleUtilities.ModuleFileName(name);

                if (filename == null)
                    throw new ImportError(string.Format("No module named {0}", name));

                if (modules.ContainsKey(filename) && modules[filename].GlobalContext == context.GlobalContext)
                    return modules[filename];

                Parser parser = new Parser(new StreamReader(filename));
                ICommand command = parser.CompileCommandList();

                module = new Module(context.GlobalContext);
                string doc = CommandUtilities.GetDocString(command);

                command.Execute(module);
                module.SetValue("__doc__", doc);

                modules[filename] = module;
            }

            return module;
        }
Example #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
0
        public void CompileExpressionList()
        {
            Parser parser = new Parser("a, b");

            IList<IExpression> expressions = parser.CompileExpressionList();

            Assert.IsNotNull(expressions);
            Assert.AreEqual(2, expressions.Count);
            Assert.IsInstanceOfType(expressions[0], typeof(NameExpression));
            Assert.IsInstanceOfType(expressions[1], typeof(NameExpression));
        }
Example #20
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 #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 CompileEmptyTupleExpression()
        {
            Parser parser = new Parser("()");

            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ListExpression));

            var lexpr = (ListExpression)expression;

            Assert.IsTrue(lexpr.IsReadOnly);
            Assert.AreEqual(0, lexpr.Expressions.Count);
        }
Example #23
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 #24
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 #25
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 #26
0
        public void CompileFalseAsBooleanConstantExpression()
        {
            Parser parser = new Parser("False");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(ConstantExpression));

            ConstantExpression cexpr = (ConstantExpression)expression;

            Assert.AreEqual(false, cexpr.Value);
        }
Example #27
0
        public void CompileIndexedExpression()
        {
            Parser parser = new Parser("'spam'[1]");
            IExpression expression = parser.CompileExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(IndexedExpression));

            IndexedExpression iexpr = (IndexedExpression)expression;

            Assert.AreEqual("spam", iexpr.TargetExpression.Evaluate(null));
            Assert.AreEqual(1, iexpr.IndexExpression.Evaluate(null));
        }
Example #28
0
        public void CompileCompositeCommandUsingSemicolonAndSpaces()
        {
            Parser parser = new Parser("spam = \"bar\";   one = 1");

            ICommand command = parser.CompileCommandList();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(CompositeCommand));
        }
Example #29
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 #30
0
        public void CompileEmptyCommandList()
        {
            Parser parser = new Parser(string.Empty);

            ICommand command = parser.CompileCommandList();

            Assert.IsNull(command);
        }