예제 #1
0
        static BuiltInModules()
        {
            var modules = Assembly.GetExecutingAssembly().GetTypes()
                          .Where(p => p.IsSubclassOf(typeof(IodineModule)));

            foreach (Type type in modules)
            {
                if (type.IsDefined(typeof(IodineBuiltinModule), false))
                {
                    IodineBuiltinModule attr = (IodineBuiltinModule)type.GetCustomAttributes(
                        typeof(IodineBuiltinModule),
                        false
                        ).First();
                    IodineModule module = (IodineModule)Activator.CreateInstance(type);

                    BuiltinDocString docstr = type.GetCustomAttribute <BuiltinDocString> ();

                    if (docstr != null)
                    {
                        module.SetAttribute("__doc__", new IodineString(docstr.DocumentationString));
                    }
                    Modules.Add(attr.Name, module);
                }
            }
        }
예제 #2
0
        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[] { }));
        }
예제 #3
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[] { }));
        }
예제 #4
0
 public ModuleCompiler(ErrorLog errorLog, SymbolTable symbolTable, IodineModule module)
 {
     this.errorLog = errorLog;
     this.symbolTable = symbolTable;
     this.module = module;
     functionCompiler = new FunctionCompiler (errorLog, symbolTable, module.Initializer);
 }
예제 #5
0
 public IodineModule CompileAst(IodineModule module, AstRoot ast)
 {
     ModuleCompiler compiler = new ModuleCompiler (errorLog, symbolTable, module);
     ast.Visit (compiler);
     module.Initializer.FinalizeLabels ();
     optimizeObject (module);
     return module;
 }
예제 #6
0
        private IodineObject loadModule(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            IodineString pathStr = args [0] as IodineString;
            IodineModule module  = vm.Context.LoadModule(pathStr.Value);

            module.Initializer.Invoke(vm, new IodineObject[] { });
            return(module);
        }
예제 #7
0
 public IodineMethod(IodineModule module, string name, bool isInstance, int parameterCount,
                     int localCount) : base(MethodTypeDef)
 {
     Name           = name;
     ParameterCount = parameterCount;
     Module         = module;
     LocalCount     = localCount;
     InstanceMethod = isInstance;
     Parameters     = new Dictionary <string, int> ();
 }
예제 #8
0
        public IodineModule LoadModule(string name, bool useCache = true)
        {
            IodineModule module = Context.LoadModule(name, useCache);

            if (module == null)
            {
                throw new ModuleNotFoundException(name, Context.SearchPath);
            }
            return(module);
        }
예제 #9
0
 public StackFrame(
     IodineModule module,
     IodineMethod method,
     IodineObject[] arguments,
     StackFrame parent,
     IodineObject self,
     AttributeDictionary locals) : this(module, method, arguments, parent, self)
 {
     this.locals = locals;
 }
예제 #10
0
        public IodineModule LoadModule(string name)
        {
            IodineModule module = Context.LoadModule(name);

            if (module == null)
            {
                throw new Exception("Could not find module " + name +
                                    "\n Searched in: " + String.Join("\n", Context.SearchPath));
            }
            return(module);
        }
예제 #11
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;
		}
예제 #12
0
        private IodineObject LoadModule(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }
            IodineString pathStr = args [0] as IodineString;
            IodineModule module  = vm.Context.LoadModule(pathStr.Value);

            module.Invoke(vm, new IodineObject[] { });
            return(module);
        }
예제 #13
0
 StackFrame(
     VirtualMachine vm,
     IodineModule module,
     IodineMethod method,
     IodineObject [] arguments,
     StackFrame parent,
     IodineObject self,
     AttributeDictionary locals,
     AttributeDictionary parentLocals) : this(vm, module, method, arguments, parent, self)
 {
     this.parentLocals = parentLocals;
     this.locals       = locals;
 }
예제 #14
0
 public StackFrame(
     IodineModule module,
     IodineMethod method,
     IodineObject[] arguments,
     StackFrame parent,
     IodineObject self)
 {
     locals       = new AttributeDictionary();
     parentLocals = locals;
     Method       = method;
     Module       = module;
     Self         = self;
     Arguments    = arguments;
     Parent       = parent;
 }
예제 #15
0
        public IodineMethod(
            IodineModule module,
            IodineString name,
            CodeObject bytecode,
            IodineTuple parameters,
            MethodFlags flags,
            IodineObject[] defaultValues,
            int defaultStart = 0
            )
            : base(MethodTypeDef)
        {
            Module                  = module;
            Bytecode                = bytecode;
            ParameterCount          = Parameters.Count;
            Variadic                = (flags & MethodFlags.AcceptsVarArgs) != 0;
            AcceptsKeywordArgs      = (flags & MethodFlags.AcceptsKwargs) != 0;
            HasDefaultValues        = (flags & MethodFlags.HasDefaultParameters) != 0;
            DefaultValuesStartIndex = defaultStart;
            DefaultValues           = defaultValues;
            SetParameters(Parameters, parameters);

            Name = name.ToString();

            SetAttribute("__doc__", IodineString.Empty);
            SetAttribute("__invoke__", new BuiltinMethodCallback(invoke, this));

            if (AcceptsKeywordArgs)
            {
                var lastParameter = Parameters.Last() as IodineNamedParameter;

                KwargsParameter = lastParameter.Name;

                if (Variadic)
                {
                    var secondToLastParameter = Parameters [Parameters.Count - 2] as
                                                IodineNamedParameter;

                    VarargsParameter = secondToLastParameter.Name;
                }
            }
            else if (Variadic)
            {
                var lastParameter = Parameters.Last() as IodineNamedParameter;
                VarargsParameter = lastParameter.Name;
            }
        }
예제 #16
0
        private IodineObject Reload(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length == 0)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }

            IodineModule module = args [0] as IodineModule;

            if (module == null)
            {
                vm.RaiseException(new IodineTypeException("Module"));
                return(IodineNull.Instance);
            }

            return(vm.LoadModule(module.Location, false));
        }
예제 #17
0
        IodineObject Require(VirtualMachine vm, IodineObject self, IodineObject [] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }

            var path = args [0] as IodineString;

            if (path == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(IodineNull.Instance);
            }

            string name = path.Value;

            var fullPath = Path.GetFullPath(name);

            if (args.Length == 1)
            {
                // use <module>
                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    IodineModule module = VirtualMachine.ModuleCache [fullPath];
                    vm.Top.StoreLocal(Path.GetFileNameWithoutExtension(fullPath), module);
                }
                else
                {
                    var module = vm.LoadModule(name);
                    vm.Top.StoreLocal(Path.GetFileNameWithoutExtension(fullPath), module);

                    VirtualMachine.ModuleCache [fullPath] = module;

                    if (module.Initializer != null)
                    {
                        module.Invoke(vm, new IodineObject [] { });
                    }
                }
            }
            else
            {
                // use <types> from <module>
                var names = args [1] as IodineTuple;
                if (names == null)
                {
                    vm.RaiseException(new IodineTypeCastException("Tuple"));
                    return(IodineNull.Instance);
                }
                IodineModule module = null;

                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    module = VirtualMachine.ModuleCache [fullPath];
                }
                else
                {
                    module = vm.LoadModule(name);
                    VirtualMachine.ModuleCache [fullPath] = module;
                    if (module.Initializer != null)
                    {
                        module.Invoke(vm, new IodineObject [] { });
                    }
                }

                vm.Top.StoreLocal(Path.GetFileNameWithoutExtension(fullPath), module);

                if (names.Objects.Length > 0)
                {
                    foreach (IodineObject item in names.Objects)
                    {
                        vm.Top.StoreLocal(
                            item.ToString(),
                            module.GetAttribute(item.ToString())
                            );
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, IodineObject> kv in module.Attributes)
                    {
                        vm.Top.StoreLocal(kv.Key, kv.Value);
                    }
                }
            }
            return(IodineNull.Instance);
        }
예제 #18
0
 public IodineMethod(IodineMethod parent, IodineModule module, string name, bool isInstance, int parameterCount,
                     int localCount) : this(module, name, isInstance, parameterCount, localCount)
 {
     this.parent = parent;
 }
예제 #19
0
		public IodineMethod (IodineMethod parent, IodineModule module, string name, bool isInstance, int parameterCount,
			int localCount) : this (module, name, isInstance, parameterCount, localCount)
		{
			this.parent = parent;
		}
예제 #20
0
		public IodineMethod (IodineModule module, string name, bool isInstance, int parameterCount,
			int localCount) : base (MethodTypeDef)
		{
			Name = name;
			ParameterCount = parameterCount;
			Module = module;
			LocalCount = localCount;
			InstanceMethod = isInstance;
			Parameters = new Dictionary<string, int> ();
			SetAttribute ("__module__", module);
		}
예제 #21
0
 public static IodineModule CompileModule(ErrorLog errorLog, string file)
 {
     if (FindModule (file) != null) {
         Tokenizer lexer = new Tokenizer (errorLog, File.ReadAllText (FindModule (file)), file);
         TokenStream tokenStream = lexer.Scan ();
         if (errorLog.ErrorCount > 0)
             return null;
         Parser parser = new Parser (tokenStream);
         AstRoot root = parser.Parse ();
         if (errorLog.ErrorCount > 0)
             return null;
         SemanticAnalyser analyser = new SemanticAnalyser (errorLog);
         SymbolTable symbolTable = analyser.Analyse (root);
         if (errorLog.ErrorCount > 0)
             return null;
         IodineCompiler compiler = new IodineCompiler (errorLog, symbolTable, Path.GetFullPath (file));
         IodineModule module = new IodineModule (Path.GetFileNameWithoutExtension (file));
         compiler.CompileAst (module, root);
         if (errorLog.ErrorCount > 0)
             return null;
         return module;
     } else {
         errorLog.AddError (ErrorType.ParserError, new Location (0, 0, file),
             "Could not find module {0}", file);
         return null;
     }
 }
예제 #22
0
        public static IodineModule CompileModuleFromSource(ErrorLog errorLog, string source)
        {
            Tokenizer lexer = new Tokenizer (errorLog, source);
            TokenStream tokenStream = lexer.Scan ();
            if (errorLog.ErrorCount > 0)
                return null;
            Parser parser = new Parser (tokenStream);
            AstRoot root = parser.Parse ();
            if (errorLog.ErrorCount > 0)
                return null;
            SemanticAnalyser analyser = new SemanticAnalyser (errorLog);
            SymbolTable symbolTable = analyser.Analyse (root);
            if (errorLog.ErrorCount > 0)
                return null;
            IodineCompiler compiler = new IodineCompiler (errorLog, symbolTable, "");
            IodineModule module = new IodineModule ("");

            compiler.CompileAst (module, root);
            if (errorLog.ErrorCount > 0)
                return null;
            return module;
        }
예제 #23
0
        private IodineObject require(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(null);
            }

            IodineString path = args [0] as IodineString;

            if (path == null)
            {
                vm.RaiseException(new IodineTypeException("Str"));
                return(null);
            }

            string name     = path.Value;
            string fullPath = Path.GetFullPath(name);

            if (args.Length == 1)
            {
                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    IodineModule module = VirtualMachine.ModuleCache [fullPath];
                    vm.Top.Module.SetAttribute(vm, Path.GetFileNameWithoutExtension(fullPath),
                                               module);
                }
                else
                {
                    IodineModule module = vm.LoadModule(name);
                    vm.Top.Module.SetAttribute(vm, Path.GetFileNameWithoutExtension(
                                                   fullPath), module);
                    VirtualMachine.ModuleCache [fullPath] = module;
                    module.Initializer.Invoke(vm, new IodineObject[] { });
                }
            }
            else
            {
                IodineTuple names = args [1] as IodineTuple;
                if (names == null)
                {
                    vm.RaiseException(new IodineTypeCastException("Tuple"));
                    return(null);
                }
                IodineModule module = null;

                if (VirtualMachine.ModuleCache.ContainsKey(fullPath))
                {
                    module = VirtualMachine.ModuleCache [fullPath];
                }
                else
                {
                    module = vm.LoadModule(name);
                    VirtualMachine.ModuleCache [fullPath] = module;
                    module.Initializer.Invoke(vm, new IodineObject[] { });
                }

                vm.Top.Module.SetAttribute(vm, Path.GetFileNameWithoutExtension(fullPath),
                                           module);

                if (names.Objects.Length > 0)
                {
                    foreach (IodineObject item in names.Objects)
                    {
                        vm.Top.Module.SetAttribute(vm, item.ToString(),
                                                   module.GetAttribute(item.ToString()));
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, IodineObject> kv in module.Attributes)
                    {
                        vm.Top.Module.SetAttribute(vm, kv.Key, kv.Value);
                    }
                }
            }
            return(null);
        }
예제 #24
0
		public ModuleCompiler (SymbolTable symbolTable, IodineModule module)
		{
			this.symbolTable = symbolTable;
			this.module = module;
			functionCompiler = new FunctionCompiler (symbolTable, module.Initializer);
		}
예제 #25
0
 public IodineModuleBuilder(IodineModule module)
 {
 }