Пример #1
0
 /// <summary>
 /// Function executed when the fucntion is called in hlang
 /// </summary>
 /// <param name="interpreter">Interpreter to execute code block</param>
 /// <param name="arguments">Arguments passed in</param>
 /// <returns></returns>
 public object Call(Interpreter interpreter, List <object> arguments)
 {
     // Create a new environment and add the arguments to each paramter
     // Pass the closure (outter scope) to the new environment (functiions scope)
     Lib.Environment env = new Lib.Environment(_closure);
     for (int i = 0; i < _funcDeclaration.Paramters.Count; i++)
     {
         env.Add(_funcDeclaration.Paramters[i].Lexeme, arguments[i]);
     }
     // execute code block and catch any return statements
     try
     {
         interpreter.ExecuteBlock(_funcDeclaration.Body, env);
     }
     catch (HlangReturn value)
     {
         return(value.Value);
     }
     return(null);
 }
Пример #2
0
        /// <summary>
        /// Function executed when the lambda is called in hlang
        /// </summary>
        /// <param name="interpreter">Interpreter to execute expression</param>
        /// <param name="arguments">Arguments passed in</param>
        /// <returns></returns>
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            // keep track of the old environment
            // and create a new one for the lambda execution
            Lib.Environment old = interpreter.Environment;
            Lib.Environment env = new Lib.Environment(old);

            // add arguments passed in to new environment
            for (int i = 0; i < Expr.Parameters.Count; i++)
            {
                env.Add(Expr.Parameters[i].Lexeme, arguments[i]);
            }

            // evaluate expression and change interpeter environment back
            interpreter.Environment = env;
            object value = interpreter.Evaluate(Expr.Body);

            interpreter.Environment = old;
            return(value);
        }