Пример #1
0
 /// <summary>
 /// Sets the value on the named field. Does not automatically add a field if missing.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="val"></param>
 public override void SetField(string name, HeronValue val)
 {
     if (fields.HasName(name))
     {
         fields[name] = val;
     }
     else if (GetBase() != null)
     {
         GetBase().SetField(name, val);
     }
     else
     {
         throw new Exception("Field '" + name + "' does not exist");
     }
 }
Пример #2
0
        private void GetFreeVars(VM vm, Statement st, Stack<string> boundVars, Scope result)
        {
            // InternalCount and store the names defined by this statement 
            int nNewVars = 0;
            foreach (string name in st.GetDefinedNames())
            {
                ++nNewVars;
                boundVars.Push(name);
            }

            // Iterate over all names used by expressions in the statement
            foreach (string name in st.GetUsedNames())
            {
                // Is this not a boundVar, and not already marked as a free var
                if (!boundVars.Contains(name) && !result.HasName(name))
                {
                    // Throws an exception if the name is not found
                    HeronValue v = vm.LookupName(name);
                    result.Add(new VarDesc(name), v);
                }
            }

            // Recurse over all sub statements, getting the free vars
            foreach (Statement child in st.GetSubStatements())
                GetFreeVars(vm, child, boundVars, result);

            // Pop all variables added by this variable
            for (int i = 0; i < nNewVars; ++i)
                boundVars.Pop();
        }
Пример #3
0
        private void GetFreeVars(VM vm, Statement st, Stack <string> boundVars, Scope result)
        {
            // InternalCount and store the names defined by this statement
            int nNewVars = 0;

            foreach (string name in st.GetDefinedNames())
            {
                ++nNewVars;
                boundVars.Push(name);
            }

            // Iterate over all names used by expressions in the statement
            foreach (string name in st.GetUsedNames())
            {
                // Is this not a boundVar, and not already marked as a free var
                if (!boundVars.Contains(name) && !result.HasName(name))
                {
                    // Throws an exception if the name is not found
                    HeronValue v = vm.LookupName(name);
                    result.Add(new VarDesc(name), v);
                }
            }

            // Recurse over all sub statements, getting the free vars
            foreach (Statement child in st.GetSubStatements())
            {
                GetFreeVars(vm, child, boundVars, result);
            }

            // Pop all variables added by this variable
            for (int i = 0; i < nNewVars; ++i)
            {
                boundVars.Pop();
            }
        }
Пример #4
0
 public bool HasVar(string s)
 {
     for (int i = scopes.Count; i > 0; --i)
     {
         Scope tbl = scopes[i - 1];
         if (tbl.HasName(s))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #5
0
 public HeronValue GetVar(string s)
 {
     for (int i = scopes.Count; i > 0; --i)
     {
         Scope tbl = scopes[i - 1];
         if (tbl.HasName(s))
         {
             return(tbl[s]);
         }
     }
     throw new Exception("No field named '" + s + "' could be found");
 }
Пример #6
0
 public bool SetVar(string s, HeronValue o)
 {
     for (int i = scopes.Count; i > 0; --i)
     {
         Scope tbl = scopes[i - 1];
         if (tbl.HasName(s))
         {
             tbl[s] = o;
             return(false);
         }
     }
     return(false);
 }
Пример #7
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);
        }