Esempio n. 1
0
        private static Expression SimplifyFunction(Function node)
        {
            bool @const = true;

            for (int i = 0; i < node.Arguments.Count; i++)
            {
                node.Arguments[i] = Simplify(node.Arguments[i]);
                if (node.Arguments[i].Type != ExpressionType.Float)
                {
                    @const = false;
                }
            }
            if (!@const)
            {
                return(node);
            }
            double         value;
            FunctionEntry  entry    = node.Entry;
            List <Operand> operands = new List <Operand>();

            for (int i = 0; i < node.Arguments.Count; i++)
            {
                operands.Add(new Operand(((Float)node.Arguments[i]).Value));
            }
            value = entry.Exec(operands).value;
            return(new Float {
                Value = value
            });
        }
 public StackFunction(FunctionEntry func) : base(StackElementType.Function)
 {
     this.func = func;
 }
Esempio n. 3
0
        static public string Print(RPNExpression exp, string [] variableNames)
        {
            List <StackElement>  rpn      = exp.rpn;
            Stack <PrintElement> operands = new Stack <PrintElement>();

            for (int i = 0; i < rpn.Count; i++)
            {
                switch (rpn[i].Type)
                {
                case StackElementType.Negation:
                {
                    PrintElement operand = operands.Pop();
                    if (operand.Type < PrintElement.ElementType.Negation)
                    {
                        operands.Push(new PrintElement
                            {
                                Type   = PrintElement.ElementType.Negation,
                                String = "-(" + operand.String + ")"
                            });
                    }
                    else
                    {
                        operands.Push(new PrintElement
                            {
                                Type   = PrintElement.ElementType.Negation,
                                String = "-" + operand.String
                            });
                    }
                    break;
                }

                case StackElementType.Addition:
                {
                    PrintElement right = operands.Pop();
                    PrintElement left  = operands.Pop();
                    if (left.Type < PrintElement.ElementType.Addition)
                    {
                        left.String = "(" + left.String + ")";
                    }
                    if (right.Type < PrintElement.ElementType.Addition)
                    {
                        right.String = "(" + right.String + ")";
                    }
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Addition,
                            String = left.String + "+" + right.String
                        });
                    break;
                }

                case StackElementType.Subtraction:
                {
                    PrintElement right = operands.Pop();
                    PrintElement left  = operands.Pop();
                    if (left.Type < PrintElement.ElementType.Addition)
                    {
                        left.String = "(" + left.String + ")";
                    }
                    if (right.Type <= PrintElement.ElementType.Addition)
                    {
                        right.String = "(" + right.String + ")";
                    }
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Addition,
                            String = left.String + "-" + right.String
                        });
                    break;
                }

                case StackElementType.Multiplication:
                {
                    PrintElement right = operands.Pop();
                    PrintElement left  = operands.Pop();
                    if (left.Type < PrintElement.ElementType.Multiplication)
                    {
                        left.String = "(" + left.String + ")";
                    }
                    if (right.Type < PrintElement.ElementType.Multiplication)
                    {
                        right.String = "(" + right.String + ")";
                    }
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Multiplication,
                            String = left.String + "*" + right.String
                        });
                    break;
                }

                case StackElementType.Division:
                {
                    PrintElement right = operands.Pop();
                    PrintElement left  = operands.Pop();
                    if (left.Type < PrintElement.ElementType.Multiplication)
                    {
                        left.String = "(" + left.String + ")";
                    }
                    if (right.Type < PrintElement.ElementType.Multiplication)
                    {
                        right.String = "(" + right.String + ")";
                    }
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Multiplication,
                            String = left.String + "/" + right.String
                        });
                    break;
                }

                case StackElementType.Power:
                {
                    PrintElement right = operands.Pop();
                    PrintElement left  = operands.Pop();
                    if (left.Type <= PrintElement.ElementType.Power)
                    {
                        left.String = "(" + left.String + ")";
                    }
                    if (right.Type < PrintElement.ElementType.Power)
                    {
                        right.String = "(" + right.String + ")";
                    }
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Multiplication,
                            String = left.String + "^" + right.String
                        });
                    break;
                }

                case StackElementType.Variable:
                {
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Identifier,
                            String = variableNames[((StackVariable)rpn[i]).index]
                        });
                    break;
                }

                case StackElementType.Function:
                {
                    StackFunction func    = (StackFunction)rpn[i];
                    FunctionEntry entry   = func.GetFunctionEntry();
                    string        @string = "";
                    for (int j = 0; j < entry.ArgNumber; j++)
                    {
                        PrintElement operand = operands.Pop();
                        if (j != 0)
                        {
                            @string = ", " + @string;
                        }
                        @string = operand.String + @string;
                    }
                    operands.Push(new PrintElement
                        {
                            Type   = PrintElement.ElementType.Function,
                            String = entry.FuncName + "(" + @string + ")"
                        });
                    break;
                }

                case StackElementType.Operand:
                {
                    PrintElement element = new PrintElement();
                    element.Type   = PrintElement.ElementType.Float;
                    element.String = Convert.ToString(((Operand)rpn[i]).value);
                    operands.Push(element);
                    break;
                }
                }
            }
            return(operands.Pop().String);
        }
Esempio n. 4
0
 public Function(FunctionEntry entry, List <Expression> arguments)
 {
     Type      = ExpressionType.Function;
     Entry     = entry;
     Arguments = arguments;
 }