示例#1
0
        public object GetVariable(Id id) {

#if _DEBUG
            Console.Write("  "+this + " getting " + id+ "...");
#endif
            try {
                //ncase the path of the id is llarger then 1 you need to search inside the kid symbo table
                if (id.Length > 1) {
                    SymbolTable scope = (SymbolTable) this.GetVariable(new Id(id.Head));
                    id.Path.RemoveFirst();
                    return scope.GetVariable(id);
                }
#if _DEBUG                
                object toReturn = _symbols[id.Head];
                Console.WriteLine("Sucesess");
                return toReturn;
#else
                return _symbols[id.Head];
#endif
            }
            catch {
#if _DEBUG
                Console.WriteLine("Fail. Searching in perent ↓"+id+"...");
#endif
                if (Perent == null)
                    throw new Exception("Variable \""+id+"\" is undefined");
                return this.Perent.GetVariable(id);
            }
        }
示例#2
0
        public object SetVariable(Id id, object value) {
             try {
                 if (id.Length > 1) {
                     SymbolTable scope = (SymbolTable)this.GetVariable(new Id(id.Head));
                     id.Path.RemoveFirst();
                     return scope.SetVariable(id, value);
                 }
                 
                _symbols[id.Head] = value;
                return value;
                 
            }
            catch {
                 if (Perent == null)
                     throw new Exception("Variable \"" + id + "\" is undefined");
                 return this.Perent.SetVariable(id, value);

            }
        }
示例#3
0
 public object GetValue(Id id) {
     object toGet = GetVariable(id);
     while (toGet is Id)
         toGet = GetVariable(toGet as Id);
     return toGet;
 }
示例#4
0
        public void CallFunction(Id name) {
            
#if _DEBUG
            Console.WriteLine("Program calling function "+name+" from scope "+this.activeScope.GetHashCode());
#endif
            //the id of the function is obj.lexema

            //get the fucnion block
            
            FunctionBLock funBlock =(FunctionBLock)this.activeScope.GetValue(name);

            //get the number of aprands that this function resiave
            int limit = funBlock.ParametersCount;
            //the parameters that will be passed to the function call
            List<object> parameters = new List<object>();
#if _DEBUG
            for (int i = 0; i < limit; i++) {
                object toAdd = pStack.Pop();
                Console.WriteLine("Poping parameter n" + i + " : " + toAdd);
                parameters.Add(toAdd);
            }
                
#else
            for (int i = 0; i < limit; i++)
                parameters.Add(pStack.Pop());
#endif
            Interpreter t = new Interpreter(this.ProcessStack, funBlock.GetCallSymbolTable(this.ActiveScope, parameters));
            t.activeScope.AddFunction("return", new ReservedFunction(new ReservedFunction.ReservedFuncDelegate(t.Return)), "toReturn");
            funBlock.Executable.ExecuteByhInterpreter(t);
        }