Example #1
0
 public DynamicFunction(string name, ParseNode node, Variables args, int minParameters, int maxParameters)
 {
     Node = node;
     Arguments = args;
     MinParameters = minParameters;
     MaxParameters = maxParameters;
 }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InputHandler"/> class.
 /// </summary>
 public InputHandler()
 {
     _fraction = new Fraction();
     _setVariable = new SetVariable();
     _unsetVariable = new UnsetVariable();
     _variables = new Variables();
     _calculate = new Calculate();
     _help = new Help();
 }
Example #3
0
 /// <summary>
 /// resets the context to its defaults
 /// </summary> 
 public void Reset()
 {
     inScope = new List<Variables>();
     Functions = new Functions();
     Globals = new Variables();
     Functions.InitDefaults();
     Globals["pi"]  = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679; // Math.Pi is not very precise
     Globals["e"]   = 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274;  // Math.E is not very precise
     Globals["phi"] = 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374;  // no Math.Phi at all
 }
Example #4
0
 public void PushScope(Variables vars)
 {
     inScope.Add(vars);
 }
Example #5
0
        protected override object EvaluateExpression(ParseTree tree, params object[] paramlist)
        {
            // if only left hand side available, this is not an assignment, simple evaluate expression
            if (nodes.Count == 1)
                return this.GetValue(tree, TokenType.AssignmentExpression, 0); // return the result

            if (nodes.Count != 3)
            {
                tree.Errors.Add(new ParseError("Illegal EvaluateExpression format", 1092, this));
                return null;
            }

            // ok, this is an assignment so declare the function or variable
                // assignment only allowed to function or to a variable
                ParseNode v = GetFunctionOrVariable(nodes[0]);
                if (v == null)
                {
                    tree.Errors.Add(new ParseError("Can only assign to function or variable", 1020, this));
                    return null;
                }

                ParseTreeEvaluator root = tree as ParseTreeEvaluator;
                if (root == null)
                {
                    tree.Errors.Add(new ParseError("Invalid parser used", 1040, this));
                    return null;
                }

                if (root.Context == null)
                {
                    tree.Errors.Add(new ParseError("No context defined", 1041, this));
                    return null;
                }

                if (v.Token.Type == TokenType.VARIABLE)
                {

                    // simply overwrite any previous defnition
                    string key = v.Token.Text.ToLowerInvariant();
                    root.Context.Globals[key] = this.GetValue(tree, TokenType.AssignmentExpression, 1);
                    return null;
                }
                else if (v.Token.Type == TokenType.Function)
                {

                    string key = v.Nodes[0].Token.Text;

                    // function lookup is case insensitive
                    if (root.Context.Functions.ContainsKey(key.ToLower()))
                        if (!(root.Context.Functions[key.ToLower()] is DynamicFunction))
                        {
                            tree.Errors.Add(new ParseError("Built in functions cannot be overwritten", 1050, this));
                            return null;
                        }

                    // lets determine the input variables.
                    // functions must be of te form f(x;y;z) = x+y*z;
                    // check the function parameters to be of type Variable, error otherwise
                    Variables vars = new Variables();
                    ParseNode paramsNode = v.Nodes[2];
                    if (paramsNode.Token.Type == TokenType.Params)
                    {   // function has parameters, so check if they are all variable declarations
                        for (int i = 0; i < paramsNode.Nodes.Count; i += 2)
                        {
                            ParseNode varNode = GetFunctionOrVariable(paramsNode.Nodes[i]);
                            if (varNode == null || varNode.Token.Type != TokenType.VARIABLE)
                            {
                                tree.Errors.Add(new ParseError("Function declaration may only contain variables", 1051, this));
                                return null;
                            }
                            // simply declare the variable, no need to evaluate the value of it at this point.
                            // evaluation will be done when the function is executed
                            // note, variables are Case Sensitive (!)
                            vars.Add(varNode.Token.Text, null);
                        }
                    }
                    // we have all the info we need to know to declare the dynamicly defined function
                    // pass on nodes[2] which is the Right Hand Side (RHS) of the assignment
                    // nodes[2] will be evaluated at runtime when the function is executed.
                    DynamicFunction dynf = new DynamicFunction(key, nodes[2], vars, vars.Count, vars.Count);
                    root.Context.Functions[key.ToLower()] = dynf;
                    return null;
                }

            // in an assignment, dont return any result (basically void)
            return null;
        }
Example #6
0
 public DynamicFunction(string name, ParseNode node, Variables args)
     : this(name, node, args, 0, 0)
 {
 }
Example #7
0
 public StaticFunction(string name, FunctionDelegate function, int minParameters, int maxParameters)
 {
     Name = name;
     FunctionDelegate = function;
     MinParameters = minParameters;
     MaxParameters = maxParameters;
     Arguments = new Variables();
 }