Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates this node and all its children
        /// </summary>
        /// <param name="scope">The scope of this expression</param>
        /// <returns></returns>
        public override Expression Evaluate(Scope scope)
        {
            // Evaluate all arguments
            for (int i = 0; i < arguments.Count; i++)
            {
                arguments[i] = arguments[i].Evaluate(scope);
            }

            // Get signature from symbol table
            Signature sig = scope.GetFunction(this);

            // If no function is retrieved
            if (sig == null)
            {
                // Unknown function error
                Compiler.Compiler.errors.SemErr(t.line, t.col, "Unknown function or ambiguos call to " + methodName);
            }
            else
            {
                // Set return type
                this.returnType = sig.returnType;

                // Step through each argument
                for (int i = 0; i < sig.arguments.Count; i++)
                {
                    // If argument expression type is different than expected argument
                    // Arguments are guaranteed to be compatible since a matching signature was found in the symbol table
                    if (!arguments[i].returnType.Equals(sig.arguments[i]))
                    {
                        // Create implicit type cast to expected argument
                        Expression expr = arguments[i];
                        arguments[i] = new CastExpression(sig.arguments[i]);
                        (arguments[i] as CastExpression).operand = expr;
                    }
                }
            }

            return this;
        }