Exemple #1
0
        protected void CallConstructor(VM vm, HeronValue[] args, ModuleInstance mi, ClassInstance ci)
        {
            // First we are going to invoke the auto-constructor
            GetAutoConstructor().Invoke(ci, vm, new HeronValue[] { });

            List <FunctionDefn> ctorlist = new List <FunctionDefn>(GetMethods("Constructor"));

            if (ctorlist == null)
            {
                return;
            }
            ctors = new FunDefnListValue(ci, "Constructor", ctorlist);
            if (ctors.Count == 0)
            {
                if (args.Length > 0)
                {
                    throw new Exception("No constructors have been defined and default constructor accepts no arguments");
                }
                else
                {
                    return;
                }
            }

            FunctionValue o = ctors.Resolve(vm, args);

            if (o == null)
            {
                throw new Exception("No matching constructor could be found");
            }

            o.Apply(vm, args);
        }
Exemple #2
0
        /// <summary>
        /// Creates an instance of this class.
        /// </summary>
        /// <param name="env"></param>
        /// <returns></returns>
        public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
        {
            ClassInstance r = new ClassInstance(this, m);

            AddFields(r, m);
            CallConstructor(vm, args, m, r);
            return(r);
        }
Exemple #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public Frame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     this.function = f;
     this.self = self;
     moduleInstance = mi;
     if (self != null)
         moduleDef = self.Type.GetModule();
     AddScope(new Scope());
 }
Exemple #4
0
        /// <summary>
        /// Evaluates the "Main()" function of a module instance
        /// </summary>
        /// <param name="m"></param>
        public void RunMain(ModuleInstance m)
        {
            HeronValue f = m.GetExportedFieldOrMethod("Main");

            if (f == null)
            {
                throw new Exception("Could not find a 'Main' method to run");
            }
            f.Apply(this, new HeronValue[] { });
        }
Exemple #5
0
        /// <summary>
        /// Evaluates the "Meta()" function of a module instance.
        /// </summary>
        /// <param name="m"></param>
        public bool RunMeta(ModuleInstance m)
        {
            HeronValue f = m.GetFieldOrMethod("Meta");

            if (f == null)
            {
                return(false);
            }
            f.Apply(this, new HeronValue[] { program });
            return(true);
        }
Exemple #6
0
        /// <summary>
        /// Instantiates a module, evaluates the meta function, then
        /// evaluates the main function.
        /// </summary>
        /// <param name="m"></param>
        public void RunModule(ModuleDefn m)
        {
            ModuleInstance mi = m.Instantiate(this, new HeronValue[] { }, null) as ModuleInstance;

            if (RunMeta(mi))
            {
                // Re-instantiate,
                mi = m.Instantiate(this, new HeronValue[] { }, null) as ModuleInstance;
            }
            RunMain(mi);
        }
Exemple #7
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public Frame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     this.function  = f;
     this.self      = self;
     moduleInstance = mi;
     if (self != null)
     {
         moduleDef = self.Type.GetModule();
     }
     AddScope(new Scope());
 }
Exemple #8
0
        public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
        {
            Object[] objs = HeronDotNet.ObjectsToDotNetArray(args);
            Object   o    = GetSystemType().InvokeMember(null, BindingFlags.Instance | BindingFlags.Public
                                                         | BindingFlags.Default | BindingFlags.CreateInstance, null, null, objs);

            if (o == null)
            {
                throw new Exception("Unable to construct " + name);
            }
            return(DotNetObject.Marshal(o));
        }
Exemple #9
0
        public ModuleInstance FindModule(string module)
        {
            HeronValue v = LookupName(module);

            if (v == null)
            {
                throw new Exception("Could not find module " + module);
            }
            ModuleInstance r = v as ModuleInstance;

            if (r == null)
            {
                throw new Exception("Value " + module + " is not a module");
            }
            return(r);
        }
Exemple #10
0
        /// <summary>
        /// This is overriden in the variou user types. Only classes and primitives
        /// can be instantiated (not enums or interfaces).
        /// </summary>
        /// <param name="vm"></param>
        /// <param name="funcs"></param>
        /// <param name="mi"></param>
        /// <returns></returns>
        public virtual HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
        {
            if (args.Length != 0)
            {
                throw new Exception("arguments not supported when instantiating primitive type " + name);
            }

            if (type == null)
            {
                throw new Exception("type " + name + " can't be instantiated");
            }

            Object r = type.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, new Object[] { });

            return(r as HeronValue);
        }
Exemple #11
0
        public void AddFields(ClassInstance i, ModuleInstance m)
        {
            i.AddField(new VarDesc("this"), i);

            foreach (FieldDefn field in fields)
            {
                i.AddField(field);
            }

            if (GetBaseClass() != null)
            {
                ClassInstance b = new ClassInstance(GetBaseClass(), m);
                GetBaseClass().AddFields(b, m);
                i.AddField(new VarDesc("base"), b);
            }
        }
        public override HeronValue Eval(VM vm)
        {
            HeronValue[] argvals = args.Eval(vm);

            if (module == null || module.Length == 0)
            {
                return(type.type.Instantiate(vm, argvals, vm.CurrentModuleInstance));
            }
            else
            {
                ModuleInstance mi = vm.FindModule(module);
                if (module == null)
                {
                    throw new Exception("Could not find module " + module);
                }
                return(type.type.Instantiate(vm, argvals, mi));
            }
        }
        public ModuleInstance GetModuleInstance()
        {
            ModuleInstance mi = self as ModuleInstance;

            if (mi != null)
            {
                return(mi);
            }

            ClassInstance ci = GetClassInstance();

            if (ci != null)
            {
                return(ci.GetModuleInstance());
            }

            return(null);
        }
Exemple #14
0
        public void AddFields(ModuleInstance inst, ModuleInstance parent)
        {
            inst.AddField(new VarDesc("thismodule"), inst);

            foreach (FieldDefn field in GetFields())
            {
                inst.AddField(field);
            }

            if (GetBaseClass() != null)
            {
                ModuleDefn baseMod = GetBaseClass() as ModuleDefn;
                if (baseMod == null)
                {
                    throw new Exception("The base type of the module must be a module: " + GetBaseClass().name);
                }

                ModuleInstance baseInst = new ModuleInstance(baseMod);
                baseMod.AddFields(baseInst, parent);
                inst.AddField(new VarDesc("basemodule"), baseInst);
            }
        }
Exemple #15
0
        public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
        {
            if (m != null)
            {
                throw new Exception("A module cannot belong to a module");
            }
            ModuleInstance r = new ModuleInstance(this);

            AddFields(r, m);
            foreach (Import i in imports)
            {
                ModuleDefn     importModDef   = vm.LookupModuleDefn(i.module);
                HeronValue[]   importArgs     = vm.EvalList(i.args).ToArray();
                ModuleInstance importInstance = importModDef.Instantiate(vm, args, null) as ModuleInstance;
                if (importInstance == null)
                {
                    throw new Exception("Failed to create loaded module instance");
                }
                r.imports.Add(i.alias, importInstance);
            }
            CallConstructor(vm, args, m, r);
            return(r);
        }
Exemple #16
0
 /// <summary>
 /// Creates a new frame, and returns a frame manager, which will release the frame
 /// on Dispose.
 /// </summary>
 /// <param name="fun"></param>
 /// <param name="classInstance"></param>
 /// <returns></returns>
 public DisposableFrame CreateFrame(FunctionDefn fun, ClassInstance classInstance, ModuleInstance mi)
 {
     return new DisposableFrame(this, fun, classInstance, mi);
 }
Exemple #17
0
 public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi)
 {
     this.vm = vm;
     vm.PushNewFrame(def, ci, mi);
 }
Exemple #18
0
 /// <summary>
 /// Called when a new function execution starts.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public void PushNewFrame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     frames.Add(new Frame(f, self, mi));
 }
Exemple #19
0
 /// <summary>
 /// Evaluates the "Main()" function of a module instance
 /// </summary>
 /// <param name="m"></param>
 public void RunMain(ModuleInstance m)
 {
     HeronValue f = m.GetExportedFieldOrMethod("Main");
     if (f == null)
         throw new Exception("Could not find a 'Main' method to run");
     f.Apply(this, new HeronValue[] { });
 }
Exemple #20
0
 /// <summary>
 /// Evaluates the "Meta()" function of a module instance.
 /// </summary>
 /// <param name="m"></param>
 public bool RunMeta(ModuleInstance m)
 {
     HeronValue f = m.GetFieldOrMethod("Meta");
     if (f == null)
         return false;
     f.Apply(this, new HeronValue[] { program });
     return true;
 }
Exemple #21
0
 public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi)
 {
     this.vm = vm;
     vm.PushNewFrame(def, ci, mi);
 }
Exemple #22
0
        public HeronValue LookupName(string s)
        {
            // Look in the scopes starting with the innermost
            // and moving to the outermost.
            // The outermost scope contains the arguments
            for (int i = scopes.Count; i > 0; --i)
            {
                Scope tbl = scopes[i - 1];
                if (tbl.HasName(s))
                {
                    return(tbl[s]);
                }
            }

            // Nothing found in the local vars,
            // So we look in the "this" pointer (called "self")
            // Note that "self" may be a class instance, or a moduleDef
            // instance
            if (self != null)
            {
                HeronValue r = self.GetFieldOrMethod(s);
                if (r != null)
                {
                    return(r);
                }

                // Nothing found in the "this" pointer. So
                // we look if it has an enclosing module instance pointer.
                // And use that

                ModuleInstance mi = self.GetModuleInstance();
                if (mi != null)
                {
                    r = mi.GetFieldOrMethod(s);
                    if (r != null)
                    {
                        return(r);
                    }
                }
            }

            // Look to see if the name is a type in the current module definition.
            if (moduleDef != null)
            {
                HeronType t = moduleDef.FindType(s);
                if (t != null)
                {
                    return(t);
                }

                // Look to see if the name is a type in one of the imported module definitions.
                List <HeronType> candidates = new List <HeronType>();
                foreach (ModuleDefn defn in moduleDef.GetImportedModuleDefns())
                {
                    t = defn.FindType(s);
                    if (t != null)
                    {
                        candidates.Add(t);
                    }
                }

                if (candidates.Count > 1)
                {
                    throw new Exception("Ambiguous name resolution. Multiple modules contain a type named " + s);
                }
                if (candidates.Count == 1)
                {
                    return(candidates[1]);
                }
            }

            return(null);
        }
Exemple #23
0
        public void AddFields(ModuleInstance inst, ModuleInstance parent)
        {
            inst.AddField(new VarDesc("thismodule"), inst);

            foreach (FieldDefn field in GetFields())
                inst.AddField(field);

            if (GetBaseClass() != null)
            {
                ModuleDefn baseMod = GetBaseClass() as ModuleDefn;
                if (baseMod == null)
                    throw new Exception("The base type of the module must be a module: " + GetBaseClass().name);

                ModuleInstance baseInst = new ModuleInstance(baseMod);
                baseMod.AddFields(baseInst, parent);
                inst.AddField(new VarDesc("basemodule"), baseInst);
            }
        }
 public ClassInstance(ClassDefn c, ModuleInstance m)
 {
     cls    = c;
     module = m;
 }
Exemple #25
0
        /// <summary>
        /// This is overriden in the variou user types. Only classes and primitives
        /// can be instantiated (not enums or interfaces).
        /// </summary>
        /// <param name="vm"></param>
        /// <param name="funcs"></param>
        /// <param name="mi"></param>
        /// <returns></returns>
        public virtual HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
        {
            if (args.Length != 0)
                throw new Exception("arguments not supported when instantiating primitive type " + name);

            if (type == null)
                throw new Exception("type " + name + " can't be instantiated");

            Object r = type.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, new Object[] { });
            return r as HeronValue;
        }
Exemple #26
0
 /// <summary>
 /// Creates a new frame, and returns a frame manager, which will release the frame
 /// on Dispose.
 /// </summary>
 /// <param name="fun"></param>
 /// <param name="classInstance"></param>
 /// <returns></returns>
 public DisposableFrame CreateFrame(FunctionDefn fun, ClassInstance classInstance, ModuleInstance mi)
 {
     return(new DisposableFrame(this, fun, classInstance, mi));
 }
Exemple #27
0
 /// <summary>
 /// Called when a new function execution starts.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public void PushNewFrame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     frames.Add(new Frame(f, self, mi));
 }
Exemple #28
0
 public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
 {
     Object[] objs = HeronDotNet.ObjectsToDotNetArray(args);
     Object o = GetSystemType().InvokeMember(null, BindingFlags.Instance | BindingFlags.Public 
         | BindingFlags.Default | BindingFlags.CreateInstance, null, null, objs);
     if (o == null)
         throw new Exception("Unable to construct " + name);
     return DotNetObject.Marshal(o);
 }
 public ClassInstance(ClassDefn c, ModuleInstance m)
 {
     cls = c;
     module = m;
 }
Exemple #30
0
 public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
 {
     if (m != null)
         throw new Exception("A module cannot belong to a module");
     ModuleInstance r = new ModuleInstance(this);
     AddFields(r, m);
     foreach (Import i in imports)
     {
         ModuleDefn importModDef = vm.LookupModuleDefn(i.module);
         HeronValue[] importArgs = vm.EvalList(i.args).ToArray();
         ModuleInstance importInstance = importModDef.Instantiate(vm, args, null) as ModuleInstance;
         if (importInstance == null)
             throw new Exception("Failed to create loaded module instance");
         r.imports.Add(i.alias, importInstance);
     }
     CallConstructor(vm, args, m, r);
     return r;
 }
Exemple #31
0
 public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
 {
     throw new Exception("Cannot instantiate an enumeration");
 }
 public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
 {
     throw new Exception("Cannot instantiate an interface");
 }