// parse levels below are in order or precedence
        private JongFunction parse_start() //throws JongError
        {
            // get the first character in expression
            getFirstChar();

            getToken();

            // check if expression is not empty
            if (token.Length == 0)
            {
                throw new JongError(4, row(), col());
            }

            JongFunction ans = parse_variable_ass();

            // check for garbage at the end of the expression
            // an expression ends with a character '\0' and token_type DELIMETER
            if (!(token.Length == 0))
            {
                if (token_type == TOKENTYPE.DELIMETER)
                {
                    // user entered a not existing operator like "//"
                    throw new JongError(101, token, row(), col());
                }
                else
                {
                    throw new JongError(5, token, row(), col());
                }
            }

            return(ans);
        }
        /**
         * Set two parameter for this JongFunction
         * @param value1    First parameter for the JongFunction
         * @param value2    Second parameter for the JongFunction
         */
        public void set(JongFunction value1, JongFunction value2)
        {
            clear();

            values_.Add(value1);
            values_.Add(value2);
        }
        /**
         * Parse the given expression
         * @param new_expr   A string containing an expression, for example "2+3"
         * @return JongFunction
         */
        public JongFunction Parse(String new_expr, ICheckModelName checkModelName, JongErrWarn errW)
        {
            try
            {
                _checkModelName = checkModelName;

                // initialize all variables
                expr = new_expr;
                ans  = 0.0;

                // Pare the expression into a function tree format
                JongFunction function = parse_start();

                //TODO: Equations: Commented out for C#
                //@SuppressWarnings("unused")


                String dbgString = FuncToString(function, "    ", "++++", errW);
                dbgString += "\n";

                //            System.out.print(dbgString);

                return(function);
            }
            catch (JongError err)
            {
                errW.Init(ErrWStat.UNKNOWN_ERROR, err.get(), false);
                return(null);
            }
        }
 /**
  * Set JongFunction name, variables, and expression
  * For example the JongFunction f(x,y)=x^y+2 whould be inserted as:
  * @param name          A string with the JongFunction name, for example "f"
  * @param variables     A vector containing the local variables x and y
  * @param expr          The JongFunction expression, like x^y+2
  */
 public void set(String name,
                 List <JongLocalVariable> variables,
                 JongFunction expr)
 {
     set(expr); // must be executed first, this clears the JongFunction too.
     setName(name);
     setVariables(variables);
 }
 /**
  * Constructor
  */
 public JongFunctionAssign(Dictionary <String, JongFunctionAssign> functionlist,
                           String name,
                           List <JongLocalVariable> variables,
                           JongFunction expr)
 {
     functionlist_ = functionlist;
     set(name, variables, expr);
 }
Ejemplo n.º 6
0
 /**
  * Constructor
  */
 public JongVariableAssign(Dictionary <String, Double> variablelist,
                           String name,
                           JongFunction value)
 {
     variablelist_ = variablelist;
     //setName(name);
     set(value);
     name_ = name;         //This has to be here like this, otherwise the name is cleared by the Set
 }
        /*
         * Factorial
         */
        private JongFunction parse_factorial() //throws JongError
        {
            JongFunction ans = parse_unaryminus();

            while (token.Equals("!"))
            {
                //getToken();
                String newFncName = token; getToken(); ans = new JongFuncSpecific(newFncName, ans);

                //Commented out by Ant: ans = new Factorial(ans);
            }

            return(ans);
        }
        /*
         * add or subtract
         */
        private JongFunction parse_addsubtract() //throws JongError
        {
            JongFunction ans = parse_multiplydivide();

            while (token.Equals("+") ||
                   token.Equals("-"))
            {
                String newFncName = token; getToken(); ans = new JongFuncSpecific(newFncName, ans, parse_multiplydivide());

                //Commented out by Ant: if      (token.Equals("+")) {getToken(); ans = new Add(ans, parse_multiplydivide());}
                //Commented out by Ant: else if (token.Equals("-")) {getToken(); ans = new Subtract(ans, parse_multiplydivide());}
            }

            return(ans);
        }
        private List <JongLocalVariable> localvariablelist           = new List <JongLocalVariable>();                // list with local function variables (used during function assignment)

        public static String FuncToString(JongFunction func, String strSpaceIndent, String strLineIndent, JongErrWarn errW)
        {
            try
            {
                String retStr = "";
                if (func.name() == "Value")
                {
                    retStr += func.eval();
                }
                else if (func.name() == "VariableGet")
                {
                    retStr += ((JongVariableGet)func).getName();
                }
                else if (func.name() == "VariableAssign")
                {
                    retStr += ((JongVariableAssign)func).getName();
                    retStr += " = ";
                }
                else
                {
                    retStr += func.name();
                }

                List <JongFunction> argFuncs = func.get();

                String nextSpaceIndent = (strSpaceIndent + strSpaceIndent);

                foreach (JongFunction argFunc in argFuncs)
                {
                    retStr += "\n";
                    retStr += (strSpaceIndent + strLineIndent);

                    String subStr = FuncToString(argFunc, nextSpaceIndent, strLineIndent, errW);
                    retStr += subStr;
                }

                return(retStr);
            }
            catch (JongError err)
            {
                errW.Init(ErrWStat.UNKNOWN_ERROR, err.get(), false);
                return("");
            }
        }
        /*
         * parentheses
         */
        private JongFunction parse_parentheses() //throws JongError
        {
            // check if it is a parenthesized expression
            if (token.Equals("("))
            {
                getToken();
                JongFunction ans = parse_conditions(); // start again

                if (!token.Equals(")"))
                {
                    throw new JongError(3, row(), col());
                }
                getToken();
                return(ans);
            }

            // if not parenthesized then the expression is a value
            return(parse_number());
        }
        /*
         * multiply, divide, modulus
         */
        private JongFunction parse_multiplydivide() //throws JongError
        {
            JongFunction ans = parse_pow();

            while (token.Equals("*") ||
                   token.Equals("/") ||
                   token.Equals("%") ||
                   token.Equals("||"))
            {
                String newFncName = token; getToken(); ans = new JongFuncSpecific(newFncName, ans, parse_pow());

                //Commented out by Ant: if      (token.Equals("*")) {getToken(); ans = new Multiply(ans, parse_pow());}
                //Commented out by Ant: else if (token.Equals("/")) {getToken(); ans = new Divide(ans, parse_pow());}
                //Commented out by Ant: else if (token.Equals("%")) {getToken(); ans = new Modulus(ans, parse_pow());}
                //Commented out by Ant: else if (token.Equals("||")) {getToken(); ans = new Xor(ans, parse_pow());}
            }

            return(ans);
        }
        /*
         * conditional operators and bitshift
         */
        private JongFunction parse_conditions() //throws JongError
        {
            JongFunction ans = parse_comparison();

            while (token.Equals("&") ||
                   token.Equals("|") ||
                   token.Equals("<<") ||
                   token.Equals(">>"))
            {
                String newFncName = token; getToken(); ans = new JongFuncSpecific(newFncName, ans, parse_comparison());

                //Commented out by Ant:            if      (token.Equals("&")) {getToken(); ans = new And(ans, parse_comparison());}
                //Commented out by Ant:             else if (token.Equals("|")) {getToken(); ans = new Or(ans, parse_comparison());}
                //Commented out by Ant: else if (token.Equals("<<")) {getToken(); ans = new Bitshiftleft(ans, parse_comparison());}
                //Commented out by Ant: else if (token.Equals(">>")) {getToken(); ans = new Bitshiftright(ans, parse_comparison());}
            }

            return(ans);
        }
        /*
         * comparison operators
         */
        private JongFunction parse_comparison() //throws JongError
        {
            JongFunction ans = parse_addsubtract();

            while (token.Equals("=") ||
                   token.Equals("<>") ||
                   token.Equals("<") ||
                   token.Equals(">") ||
                   token.Equals("<=") ||
                   token.Equals(">="))
            {
                String newFncName = token; getToken(); ans = new JongFuncSpecific(newFncName, ans, parse_addsubtract());

                //Commented out by Ant: if      (token.Equals("="))  {getToken(); ans = new Equal(ans, parse_addsubtract());}
                //Commented out by Ant: else if (token.Equals("<>")) {getToken(); ans = new Unequal(ans, parse_addsubtract());}
                //Commented out by Ant: else if (token.Equals("<"))  {getToken(); ans = new Smaller(ans, parse_addsubtract());}
                //Commented out by Ant: else if (token.Equals(">"))  {getToken(); ans = new Larger(ans, parse_addsubtract());}
                //Commented out by Ant: else if (token.Equals("<=")) {getToken(); ans = new Smallereq(ans, parse_addsubtract());}
                //Commented out by Ant: else if (token.Equals(">=")) {getToken(); ans = new Largereq(ans, parse_addsubtract());}
            }

            return(ans);
        }
 /**
  * Set one parameter for this JongFunction
  * @param value    The parameter for the JongFunction
  */
 public void set(JongFunction value)
 {
     clear();
     values_.Add(value);
 }
        /**
         * Parse the given expression
         * @param new_expr   A string containing an expression, for example "2+3"
         * @return ans       A string containing the answer, or an error when
         *                   an error occurred
         */
        public String ParseAndEval(String new_expr, ICheckModelName checkModelName, JongErrWarn errW)
        {
            try
            {
                _checkModelName = checkModelName;

                // initialize all variables
                expr = new_expr;
                ans  = 0.0;

                // Pare the expression into a function tree format
                JongFunction function = parse_start();

                String dbgString = FuncToString(function, "    ", "----", errW);
                dbgString += "\n";

                //System.out.print(dbgString);

                // evaluate the expression
                ans = function.eval();


                if (function is JongFunctionAssign)
                {
                    // function assignment
                    JongFunctionAssign fa   = (JongFunctionAssign)function;
                    String             desc = "";
                    desc += fa.getName();
                    desc += "(";
                    for (int i = 0; i < fa.getVariables().Count(); i++)
                    {
                        if (i > 0)
                        {
                            desc += ", ";
                        }
                        desc += fa.getVariables().ElementAt(i).getName();
                    }
                    desc += ")";

                    ans_str = "Function " + desc + " defined";
                }
                else if (function is JongVariableAssign)
                {
                    // variable assignment
                    JongVariableAssign JongVariableAssign = (JongVariableAssign)function;
                    ans_str = string.Format("%s = %g",
                                            JongVariableAssign.getName(),
                                            ans);

                    // add the answer to memory as variable "Ans"
                    variablelist["ANS"] = ans;
                }
                else
                {
                    //DecimalFormat formatter = new DecimalFormat("#.##########");
                    ans_str = string.Format("{0:#.##########}", ans);

                    // add the answer to memory as variable "Ans"
                    variablelist["ANS"] = ans;
                }
            }
            catch (JongError err)
            {
                errW.Init(ErrWStat.UNKNOWN_ERROR, err.get(), false);
                ans_str = err.get();
                expr    = "";
            }

            return(ans_str);
        }
Ejemplo n.º 16
0
 public JongFuncSpecific(String funcName, JongFunction arg1, JongFunction arg2)
 {
     _name = funcName;
     clear();
     set(arg1, arg2);
 }