Exemplo n.º 1
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"));
        }
Exemplo n.º 2
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"));
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
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"));
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
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;
        }
Exemplo n.º 7
0
        public void CompileEmptyCommandList()
        {
            Parser parser = new Parser(string.Empty);

            ICommand command = parser.CompileCommandList();

            Assert.IsNull(command);
        }
Exemplo n.º 8
0
        public void CompileCompositeCommandUsingSemicolonAndSpaces()
        {
            Parser parser = new Parser("spam = \"bar\";   one = 1");

            ICommand command = parser.CompileCommandList();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(CompositeCommand));
        }
Exemplo n.º 9
0
        public void CompileCompositeCommand()
        {
            Parser parser = new Parser("spam = \"bar\"\r\none = 1");

            ICommand command = parser.CompileCommandList();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(CompositeCommand));
        }
Exemplo n.º 10
0
        public void CompileHttpServer()
        {
            Parser parser = new Parser(new StreamReader("httpserver.py"));

            ICommand command = parser.CompileCommandList();
        }
Exemplo n.º 11
0
        public void CompileDefIf()
        {
            Parser parser = new Parser(new StreamReader("defif.py"));

            ICommand command = parser.CompileCommandList();
        }
Exemplo n.º 12
0
 private void ExecuteFile(string filename)
 {
     Parser parser = new Parser(new StreamReader(filename));
     ICommand command = parser.CompileCommandList();
     command.Execute(this.machine.Environment);
 }
Exemplo n.º 13
0
 private void Execute(string text)
 {
     Parser parser = new Parser(text);
     ICommand command = parser.CompileCommandList();
     command.Execute(this.machine.Environment);
 }