Exemplo n.º 1
0
 /// <summary>
 /// Assign a value to an already defined variable.
 /// </summary>
 /// <param name="varName"></param>
 /// <param name="value"></param>
 public void Assign(String varName, Value value)
 {
     if (variables.ContainsKey(varName))
     {
         variables[varName] = value;
     }
     else if (HasParent())
     {
         // The variable was not declared in this scope, so let
         // the parent scope re-assign it
         parent.Assign(varName, value);
     }
     else
     {
         // The variable was not declared at all
         throw new CompilerException(-1, 218, varName);
     }
 }
Exemplo n.º 2
0
        public override Value Invoke(List<Value> parameters)
        {
            if (parameters.Count == parameterNames.Count)
            {
                // create new scope for the method call
                var methodScope = new Scope();
                for (int i = 0; i < parameterNames.Count; i++)
                {
                    methodScope.Define(parameterNames[i]);
                    methodScope.Assign(parameterNames[i], parameters[i]);
                }

                var nodeStream = new CommonTreeNodeStream(functionBody);
                // Create a tree walker to evaluate this method's code block  
                var walker = new SGLTreeWalker(nodeStream, methodScope);

                // Ok, executing the function then
                Value returnValue = walker.main().Evaluate();
                if (returnValue.Type == ValType.Return)
                {
                    return returnValue.ReturnValue;
                }
                else
                {
                    return returnValue;
                }

                // we shouldn't check the return type
                /*if (!returnValue.GetVarType().Equals(this.returnType))
                {
                    throw new Exception("The method doesn't return the expected return type (" + returnValue.ToString()  + " is not from type " + this.returnType + ")");
                }*/
                
            }

			throw new CompilerException(definedLine, 318, name, parameterNames.Count.ToString(), parameters.Count.ToString());
        }
Exemplo n.º 3
0
        public Value Invoke(Value objectVar, List<Value> parameters)
        {
            if (parameters.Count == parameterNames.Count)
            {
                // create new scope for the method call
                var methodScope = new Scope();
                methodScope.Define("this");
                methodScope.Assign("this", objectVar);
                for (int i = 0; i < parameterNames.Count; i++)
                {
                    methodScope.Define(parameterNames[i]);
                    methodScope.Assign(parameterNames[i], parameters[i]);
                }

                var nodeStream = new CommonTreeNodeStream(functionBody);
                // Create a tree walker to evaluate this method's code block
                var walker = new SGLTreeWalker(nodeStream, methodScope);

                Value returnValue = null;

                // Ok, executing the function then
                returnValue = walker.main().Evaluate();

                // we shouldn't check the return type
                /*if (!returnValue.GetVarType().Equals(this.returnType))
                {
                    throw new Exception("The method doesn't return the expected return type (" + returnValue.ToString()  + " is not from type " + this.returnType + ")");
                }*/
                return returnValue;
            }
            // TODO: Exception
            throw new Exception();
        }