Beispiel #1
0
        public bool TryReadModule(ref IodineModule module)
        {
            if (!ReadHeader())
            {
                return(false);
            }

            string name = binaryReader.ReadString();

            ModuleBuilder builder = new ModuleBuilder(name, fileName);

            binaryReader.ReadByte();

            ReadCodeObject(builder.Initializer);

            int constantCount = binaryReader.ReadInt32();

            for (int i = 0; i < constantCount; i++)
            {
                builder.DefineConstant(ReadConstant());
            }

            module = builder;
            return(true);
        }
        private void CacheModule(IodineModule module)
        {
            try {
                string cacheDir = System.IO.Path.Combine(
                    System.IO.Path.GetDirectoryName(Path),
                    ".iodine_cache"
                    );

                if (!Directory.Exists(cacheDir))
                {
                    Directory.CreateDirectory(cacheDir);
                }

                string filePath = System.IO.Path.Combine(
                    cacheDir,
                    System.IO.Path.GetFileNameWithoutExtension(Path) + ".bytecode"
                    );

                using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
                    BytecodeFile testFile = new BytecodeFile(fs, Path);
                    testFile.WriteModule(module as ModuleBuilder);
                }
            } catch (UnauthorizedAccessException) {
            }
        }
Beispiel #3
0
        private static void LaunchRepl(IodineOptions options, IodineModule module = null)
        {
            string interpreterDir = Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().Location
                );

            if (module != null)
            {
                foreach (KeyValuePair <string, IodineObject> kv in module.Attributes)
                {
                    context.Globals [kv.Key] = kv.Value;
                }
            }

            string iosh = Path.Combine(interpreterDir, "tools", "iosh.id");

            if (File.Exists(iosh) && !options.FallBackFlag)
            {
                EvalSourceUnit(options, SourceUnit.CreateFromFile(iosh));
            }
            else
            {
                ReplShell shell = new ReplShell(context);
                shell.Run();
            }
        }
        /// <summary>
        /// Registers an assembly, allowing all classes in this assembly to be
        /// used from Iodine.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        public void RegisterAssembly(Assembly assembly)
        {
            var classes = assembly.GetExportedTypes().Where(p => p.IsClass || p.IsValueType || p.IsInterface);

            foreach (Type type in classes)
            {
                if (type.Namespace != "")
                {
                    string moduleName = type.Namespace.Contains(".") ?
                                        type.Namespace.Substring(type.Namespace.LastIndexOf(".") + 1) :
                                        type.Namespace;
                    IodineModule module = null;
                    if (!modules.ContainsKey(type.Namespace))
                    {
                        #warning This needs fixed
                        //module = new IodineModule (moduleName);
                        modules [type.Namespace] = module;
                    }
                    else
                    {
                        module = modules [type.Namespace];
                    }
                    ClassWrapper wrapper = ClassWrapper.CreateFromType(typeRegistry, type,
                                                                       type.Name);
                    module.SetAttribute(type.Name, wrapper);
                    typeRegistry.AddTypeMapping(type, wrapper, null);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Loads an Iodine module.
        /// </summary>
        /// <returns>A compiled Iodine module.</returns>
        /// <param name="name">The module's name.</param>
        public IodineModule LoadModule(string name)
        {
            if (moduleCache.ContainsKey(name))
            {
                return(moduleCache [name]);
            }

            if (_resolveModule != null)
            {
                foreach (Delegate del in _resolveModule.GetInvocationList())
                {
                    ModuleResolveHandler handler = del as ModuleResolveHandler;
                    IodineModule         result  = handler(name);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            IodineModule module = LoadIodineModule(name);

            if (module == null)
            {
                module = LoadExtensionModule(name);
            }

            if (module != null)
            {
                moduleCache [name] = module;
            }

            return(module);
        }
        private bool LoadCachedModule(ref IodineModule module)
        {
            string cacheDir = System.IO.Path.Combine(
                System.IO.Path.GetDirectoryName(Path),
                ".iodine_cache"
                );

            if (!Directory.Exists(cacheDir))
            {
                return(false);
            }

            string filePath = System.IO.Path.Combine(
                cacheDir,
                System.IO.Path.GetFileNameWithoutExtension(Path) + ".bytecode"
                );

            if (!File.Exists(filePath))
            {
                return(false);
            }

            using (FileStream fs = new FileStream(filePath, FileMode.Open)) {
                BytecodeFile testFile = new BytecodeFile(fs, Path);
                return(testFile.TryReadModule(ref module));
            }
        }
        /// <summary>
        /// Executes and loads an Iodine source file
        /// </summary>
        /// <returns>Last object evaluated during the execution of the file</returns>
        /// <param name="file">File path.</param>
        public dynamic DoFile(string file)
        {
            SourceUnit line = SourceUnit.CreateFromFile(file);

            module = line.Compile(Context);
            Context.Invoke(module, new IodineObject[] { });
            return(null);
        }
Beispiel #8
0
 /// <summary>
 /// Exposes an iodine module by name to this context's module resolver
 /// </summary>
 /// <param name="moduleName">Module name.</param>
 /// <param name="module">Module.</param>
 public void ExposeModule(string moduleName, IodineModule module)
 {
     ResolveModule += (name) => {
         if (name == moduleName)
         {
             return(module);
         }
         return(null);
     };
 }
        public IodineModule Compile(IodineContext context)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            context.ErrorLog.Clear();

            string moduleName = Path == null ? "__anonymous__"
                : System.IO.Path.GetFileNameWithoutExtension(Path);

            if (HasPath)
            {
                string wd      = System.IO.Path.GetDirectoryName(Path);
                string depPath = System.IO.Path.Combine(wd, ".deps");

                if (!context.SearchPath.Contains(wd))
                {
                    context.SearchPath.Add(wd);
                }

                if (!context.SearchPath.Contains(depPath))
                {
                    context.SearchPath.Add(depPath);
                }

                IodineModule cachedModule = null;

                if (LoadCachedModule(ref cachedModule))
                {
                    return(cachedModule);
                }
            }

            Parser parser = Parser.CreateParser(context, this);

            CompilationUnit root = parser.Parse();

            IodineCompiler compiler = IodineCompiler.CreateCompiler(context, root);

            IodineModule module = compiler.Compile(moduleName, Path);

            if (Path == null)
            {
                foreach (KeyValuePair <string, IodineObject> kv in module.Attributes)
                {
                    context.InteractiveLocals [kv.Key] = kv.Value;
                }
                module.Attributes = context.InteractiveLocals;
            }
            else if (context.ShouldCache)
            {
                CacheModule(module);
            }

            return(module);
        }
Beispiel #10
0
        public IodineModule Compile(string moduleName)
        {
            IodineModule module = new IodineModule(moduleName);

            ModuleCompiler compiler = new ModuleCompiler(symbolTable, module);

            root.Visit(compiler);
            module.Initializer.FinalizeLabels();
            if (context.ShouldOptimize)
            {
                OptimizeObject(module);
            }
            return(module);
        }
Beispiel #11
0
        private static void EvalSourceUnit(IodineOptions options, SourceUnit unit)
        {
            try {
                IodineModule module = unit.Compile(context);

                if (context.Debug)
                {
                    context.VirtualMachine.SetTrace(WaitForDebugger);
                }

                do
                {
                    context.Invoke(module, new IodineObject[] { });

                    if (module.HasAttribute("main"))
                    {
                        context.Invoke(module.GetAttribute("main"), new IodineObject[] {
                            options.IodineArguments
                        });
                    }
                } while (options.LoopFlag);

                if (options.ReplFlag)
                {
                    LaunchRepl(options, module);
                }
            } catch (UnhandledIodineExceptionException ex) {
                HandleIodineException(ex);
            } catch (SyntaxException ex) {
                DisplayErrors(ex.ErrorLog);
                Panic("Compilation failed with {0} errors!", ex.ErrorLog.ErrorCount);
            } catch (ModuleNotFoundException ex) {
                Console.Error.WriteLine(ex.ToString());
                Panic("Program terminated.");
            } catch (Exception e) {
                Console.Error.WriteLine("Fatal exception has occured!");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine("Stack trace: \n{0}", e.StackTrace);
                Console.Error.WriteLine(
                    "\nIodine stack trace \n{0}",
                    context.VirtualMachine.GetStackTrace()
                    );
                Panic("Program terminated.");
            }
        }
Beispiel #12
0
        public bool TryReadModule(ref IodineModule module)
        {
            if (!ReadHeader())
            {
                return(false);
            }

            var name = binaryReader.ReadString();

            var builder = new ModuleBuilder(name, fileName);

            binaryReader.ReadByte();

            ReadCodeObject(builder.Initializer);

            module = builder;
            return(true);
        }
Beispiel #13
0
 /// <summary>
 /// Invoke an Iodine module.
 /// </summary>
 /// <returns>The result of the invocation.</returns>
 /// <param name="module">Module.</param>
 public IodineObject InvokeModule(IodineModule module)
 {
     // Invoke the module
     return(Context.Invoke(module, new IodineObject [0]));
 }
Beispiel #14
0
 public ModuleCompiler(SymbolTable symbolTable, IodineModule module)
 {
     this.symbolTable = symbolTable;
     this.module      = module;
     functionCompiler = new FunctionCompiler(symbolTable, module.Initializer);
 }
Beispiel #15
0
		public void AddModule (IodineModule module)
		{
			this.SetAttribute (module.Name, module);
		}
Beispiel #16
0
 public void AddModule(IodineModule module)
 {
     this.SetAttribute(module.Name, module);
 }