Exemple #1
0
        protected virtual string EmitFactor(FactorTree factor)
        {
            StandardFunctionResolver resolver = new StandardFunctionResolver();
            string src = string.Empty;

            if (factor.Token.TokenType == TokenType.Identifier)
            {
                if (resolver.IsStandardVariable(factor.Token.Value))
                {
                    src = resolver.ResolveToStandardVariableName(factor.Token.Value);
                }
                else if (factor.Token.Value == FunctionParameterName)
                {
                    // refers to actual parameter of function, which will always be emitted as ___x
                    src = "___x";
                }
                else
                {
                    src = string.Format(
                        "MathContext.Variables[\"{0}\"]",
                        factor.Token.Value
                        );
                }
            }
            else
            {
                src = factor.Token.Value;
            }
            return(src);
        }
Exemple #2
0
        protected virtual string EmitFunctionCallTree(FunctionCallTree tree)
        {
            Debug.Assert(tree.Arguments.Count == 1);
            string src = string.Empty;
            StandardFunctionResolver resolver = new StandardFunctionResolver();

            // if it's a standard math function, then call it here
            if (resolver.IsStandardFunction(tree.Token.Value))
            {
                src = string.Format(
                    "{0}({1})",
                    resolver.ResolveToStandardFunctionName(tree.Token.Value),
                    EmitExpression(tree.Arguments[0])
                    );
            }
            else
            {
                // else call it from the math context
                src = string.Format(
                    "MathContext.Functions[\"{0}\"].Eval({1})",
                    tree.Token.Value,
                    EmitExpression(tree.Arguments[0])
                    );
            }
            return(src);
        }
        protected virtual List <CodeErrorException> TypeCheckFunctionCall(FunctionCallTree functionCallTree)
        {
            List <CodeErrorException> errors   = new List <CodeErrorException>();
            StandardFunctionResolver  resolver = new StandardFunctionResolver();

            if (!resolver.IsStandardFunction(functionCallTree.Token.Value))
            {
                // check function exists if its not standard function
                if (!MathContext.Functions.ContainsFunction(functionCallTree.Token.Value))
                {
                    errors.Add(
                        new CodeErrorException(
                            functionCallTree.Token.Line,
                            functionCallTree.Token.StartPosition,
                            functionCallTree.Token.EndPosition,
                            string.Format(
                                "Function '{0}' not defined in math context",
                                functionCallTree.Token.Value
                                )
                            )
                        );
                }
            }

            // check single parameter call
            if (functionCallTree.Arguments.Count != 1)
            {
                errors.Add(
                    new CodeErrorException(
                        functionCallTree.Token.Line,
                        functionCallTree.Token.StartPosition,
                        functionCallTree.Token.EndPosition,
                        "Functions only take 1 parameter"
                        )
                    );
            }

            // check parameters
            foreach (SyntaxTree arg in functionCallTree.Arguments)
            {
                errors.AddRange(TypeCheckExpression(arg));
            }

            return(errors);
        }