示例#1
0
        private IodineObject compileModule(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            IodineString source = args [0] as IodineString;
            SourceUnit   unit   = SourceUnit.CreateFromSource(source.Value);

            return(unit.Compile(vm.Context));
        }
示例#2
0
        private IodineObject eval(VirtualMachine host, string source, IodineHashMap dict)
        {
            VirtualMachine vm = host;

            if (dict != null)
            {
                vm = new VirtualMachine(host.Context, new Dictionary <string, IodineObject> ());

                foreach (string glob in host.Globals.Keys)
                {
                    vm.Globals [glob] = host.Globals [glob];
                }

                foreach (IodineObject key in dict.Keys.Values)
                {
                    vm.Globals [key.ToString()] = dict.Dict [key.GetHashCode()];
                }
            }
            IodineContext context = new IodineContext();
            SourceUnit    code    = SourceUnit.CreateFromSource(source);
            IodineModule  module  = null;

            try {
                module = code.Compile(context);
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(null);
            }
            return(vm.InvokeMethod(module.Initializer, null, new IodineObject[] { }));
        }
        private IodineObject Eval(VirtualMachine host, string source, IodineDictionary dict)
        {
            VirtualMachine vm = host;

            IodineContext context = host.Context;

            if (dict != null)
            {
                context = new IodineContext();
                context.Globals.Clear();

                vm = new VirtualMachine(host.Context);

                foreach (IodineObject key in dict.Keys)
                {
                    context.Globals [key.ToString()] = dict.Get(key);
                }
            }

            SourceUnit   code   = SourceUnit.CreateFromSource(source);
            IodineModule module = null;

            try {
                module = code.Compile(context);
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(IodineNull.Instance);
            }
            return(module.Invoke(vm, new IodineObject[] { }));
        }
示例#4
0
        /// <summary>
        /// Executes a string of Iodine source code
        /// </summary>
        /// <returns>The last object evaluated during the execution of the source.</returns>
        /// <param name="source">A string containing valid Iodine code..</param>
        public dynamic DoString(string source)
        {
            SourceUnit line = SourceUnit.CreateFromSource(source);

            module = line.Compile(Context);
            Context.Invoke(module, new IodineObject[] { });
            return(null);
        }
示例#5
0
        private static void ExecuteOptions(IodineOptions options)
        {
            if (options.DebugFlag)
            {
                RunDebugServer();
            }

            if (options.WarningFlag)
            {
                context.WarningFilter = WarningType.SyntaxWarning;
            }

            if (options.SupressWarningFlag)
            {
                context.WarningFilter = WarningType.None;
            }

            switch (options.InterpreterAction)
            {
            case InterpreterAction.Check:
                CheckIfFileExists(options.FileName);
                CheckSourceUnit(options, SourceUnit.CreateFromFile(options.FileName));
                break;

            case InterpreterAction.ShowVersion:
                DisplayInfo();
                break;

            case InterpreterAction.ShowHelp:
                DisplayUsage();
                break;

            case InterpreterAction.EvaluateFile:
                CheckIfFileExists(options.FileName);
                EvalSourceUnit(options, SourceUnit.CreateFromFile(options.FileName));
                break;

            case InterpreterAction.EvaluateArgument:
                EvalSourceUnit(options, SourceUnit.CreateFromSource(options.FileName));
                break;

            case InterpreterAction.Repl:
                LaunchRepl(options);
                break;
            }
        }
        private IodineObject Compile(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }
            IodineString source = args [0] as IodineString;
            SourceUnit   unit   = SourceUnit.CreateFromSource(source.Value);

            try {
                return(unit.Compile(vm.Context));
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(IodineNull.Instance);
            }
        }
示例#7
0
        public void Run()
        {
            var version = typeof(IodineContext).Assembly.GetName().Version;

            Console.WriteLine("Iodine v{0}-alpha", version.ToString(3));
            Console.WriteLine("Fallback REPL. Enter expressions to have them be evaluated");

            IodineContext context = new IodineContext();

            context.ShouldOptimize = false;

            context.Globals ["quit"] = new QuitObject();

            while (true)
            {
                Console.Write(">>> ");
                var source = Console.ReadLine().Trim();
                try {
                    if (source.Length > 0)
                    {
                        SourceUnit   unit   = SourceUnit.CreateFromSource(source);
                        var          result = unit.Compile(context);
                        IodineObject ret    = context.Invoke(result, new IodineObject[] { });
                        if (!(ret is IodineNull))
                        {
                            Console.WriteLine(ret.Represent(context.VirtualMachine));
                        }
                    }
                } catch (UnhandledIodineExceptionException ex) {
                    Console.Error.WriteLine("*** {0}", ex.OriginalException.GetAttribute("message"));
                    ex.PrintStack();
                    context.VirtualMachine.Top = null;
                    Console.Error.WriteLine();
                } catch (ModuleNotFoundException ex) {
                    Console.Error.WriteLine(ex.ToString());
                } catch (SyntaxException syntaxException) {
                    DisplayErrors(syntaxException.ErrorLog);
                } catch (Exception ex) {
                    Console.Error.WriteLine("Fatal exception has occured!");
                    Console.Error.WriteLine(ex.Message);
                    Console.Error.WriteLine("Stack trace: \n{0}", ex.StackTrace);
                    //Console.Error.WriteLine ("\nIodine stack trace \n{0}", engine.VirtualMachine.GetStackTrace ());
                }
            }
        }