示例#1
0
        public static void ParseCommand()
        {
            // commands = { "solve", "value", "derivative", "integral", "roots", "plot" , "simplify"};

            switch (comm_inx)
            {
            case 0:         // solve
            case 1:         // value
            {
            }
            break;

            case 2:         // derivative
            {
                int bracket = 0;

                // Parse Function
                while (NotEnd())
                {
                    if (bracket == 0 && p_line[inx] == ',')
                    {
                        break;
                    }
                    if (p_line[inx] == '(')
                    {
                        bracket++;
                    }
                    if (p_line[inx] == ')')
                    {
                        bracket--;
                    }
                    synline[syn_inx] += p_line[inx];
                    inx++;
                }

                if (synline[syn_inx] == "")
                {
                    syntaxError = true;
                    Error();
                    break;
                }

                bool isVariable     = false;
                bool isFunction     = false;
                bool isFunctionName = false;

                int func_fn_inx = 0;
                int var_fn_inx  = 0;

                // Try Parsing Function or Variable
                // All Variables get Added to ML_Variables
                if (isFunctionName = IsExistingFunctionName(synline[syn_inx]))
                {
                    funcs[func_inx] = is_existingFunc;
                    func_inx++;

                    if (End())
                    {
                        output[output_inx] = funcs[func_fn_inx].GetDerivative(funcs[func_fn_inx].VarList[0]).ToString();
                        break;
                    }
                }
                else if (isVariable = IsVariable(synline[syn_inx]))
                {
                    vars[var_inx] = ListHandler.ML_VariableList(MLGUI.AddItem, is_var);
                    var_fn_inx    = var_inx;
                    var_inx++;

                    if (End())
                    {
                        FunctionTree temp = new FunctionTree(vars[var_fn_inx]);
                        output[output_inx] = temp.GetDerivative(vars[var_fn_inx]).ToString();
                        break;
                    }
                }
                else if (isFunction = IsFunction(synline[syn_inx]))
                {
                    funcs[func_inx] = ListHandler.ML_FunctionList(MLGUI.AddItem, "", is_func);
                    func_fn_inx     = func_inx;
                    func_inx++;

                    if (End())
                    {
                        output[output_inx] = funcs[func_fn_inx].GetDerivative(funcs[func_fn_inx].VarList[0]).ToString();
                        break;
                    }
                }
                else
                {
                    syntaxError = true;
                    Error();
                    break;
                }

                // Parse Further
                SkipSpaces();
                inx--;
                if (End())
                {
                    break;
                }

                bool   wrtSet       = false;
                FVar   wrtVar       = null;
                double wrtVarVal    = 0;
                bool   wrtVarValSet = false;
                syn_inx++;
                List <string> varEqList = new List <string>();

                // Next vars[var_inx] Entry will be the w.r.t. variable
                int func_sl_inx = syn_inx;

                // Parse rest of the line
                // Requires addition of "," at end
                p_line = p_line + ",";
                p_len  = p_line.Length;

                inx++;

                while (NotEnd())
                {
                    if (p_line[inx] == ',')
                    {
                        if (!isVariable && IsEquation(synline[syn_inx]) && SplitEquation(synline[syn_inx], VariableAssignment))
                        {
                            if (ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, is_var, is_val) == null)
                            {
                                syntaxError = true;
                                break;
                            }
                            else
                            {
                                varEqList.Add(is_var);
                            }
                        }
                        else if (IsNumber(synline[syn_inx]) && !wrtVarValSet)
                        {
                            wrtVarVal = is_val;
                            if (wrtSet)
                            {
                                ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, wrtVar.varName, is_val);
                                wrtVarVal = is_val;
                            }
                            wrtVarValSet = true;
                        }
                        else if (IsVariable(synline[syn_inx]) && !wrtSet)
                        {
                            wrtVar = ListHandler.ML_VariableList(MLGUI.AddItem, is_var);
                            wrtSet = true;
                        }
                        else
                        {
                            syntaxError = true;
                            break;
                        }
                        syn_inx++;
                    }
                    else
                    {
                        synline[syn_inx] += p_line[inx];
                    }

                    inx++;
                }

                if (Error())
                {
                    break;
                }

                if (isVariable)
                {
                    if (!wrtSet)
                    {
                        wrtVar = vars[var_fn_inx];
                        if (wrtVarValSet)
                        {
                            ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, wrtVar.varName, wrtVarVal);
                        }
                    }

                    output[output_inx] += vars[var_fn_inx].GetDerivative(wrtVar).ToString();
                    break;
                }

                if (!wrtSet)
                {
                    wrtVar = funcs[func_fn_inx].varList[0];
                }

                if (wrtVarValSet)
                {
                    ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, wrtVar.varName, wrtVarVal);
                }

                FunctionTree copy = funcs[func_fn_inx].GetCopy();

                foreach (string varEq in varEqList)
                {
                    if (varEq == wrtVar.varName)
                    {
                        continue;
                    }
                    if (!copy.VariableToSetValue(varEq))
                    {
                        syntaxError = true;
                        break;
                    }
                }

                FunctionTree der = copy.GetDerivative(wrtVar);

                if (Error())
                {
                    break;
                }

                // Store result in output string
                if (der.varList.Count == 0)
                {
                    output[output_inx] = der.GetValue().ToString();

                    // Important to figure out properly
                    comm_isAnsNumVal = true;

                    // Single answer
                    ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                    ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                    ListHandler.ML_VariableList(MLGUI.AddItem, "ans", der.GetValue());
                }
                else
                {
                    output[output_inx] = der.ToString();

                    if (der.CanHaveValue())
                    {
                        string derVal = der.GetValue().ToString();

                        if (!(derVal == output[output_inx]))
                        {
                            output_inx++;
                            output[output_inx] = derVal;

                            // Important to figure out properly
                            comm_isAnsFunc   = true;
                            comm_isAnsNumVal = true;

                            // Two Answers
                            ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans0");
                            ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans0");
                            ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                            ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                            ListHandler.ML_FunctionList(MLGUI.AddItem, "ans0", der.ToString());
                            ListHandler.ML_VariableList(MLGUI.AddItem, "ans", der.GetValue());
                        }
                        else
                        {
                            // Important to figure out properly
                            comm_isAnsNumVal = true;
                            ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                            ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                            ListHandler.ML_VariableList(MLGUI.AddItem, "ans", der.GetValue());
                        }
                    }
                    else
                    {
                        // Important to figure out properly
                        comm_isAnsFunc = true;

                        // Single answer
                        ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                        ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                        ListHandler.ML_FunctionList(MLGUI.AddItem, "ans", der.ToString());
                    }
                }
            }
            break;

            case 3:         // integral
            {
            }
            break;

            case 4:         // roots
            {
            }
            break;

            case 5:         // plot
            {
            }
            break;

            case 6:         // simplify
            {
            }
            break;

            case 7:         // cls
            {
                r2.SelectAll(); r2.SelectionProtected = false;
                r2.Clear();
                r3.Clear();
                mlgui.AddLineNumbers(0);
            }
            break;

            default: break;
            }
        }
示例#2
0
        public static void ProcessInput(int startpoint)
        {
            processing = true;

            if (startpoint == 0)
            {
                r3.Text = "";
            }

            noOfLines = r2.Lines.Length - 1;

            int u = startpoint;      // process only last line

            for (; u < noOfLines; u++)
            {
                cuurrentLineNo = u;
                Initialize();
                show_output = true;

                p_line = r2.Lines[u];
                p_line = p_line.Trim(' ');
                if (p_line == "")
                {
                    continue;
                }
                if (p_line[p_line.Length - 1] == ';')
                {
                    show_output = false;
                }
                p_line = p_line.Trim(';');
                p_len  = p_line.Length;
                if (p_len == 0)
                {
                    continue;
                }

                // Skip Initial Spaces
                SkipSpaces();
                inx--;
                if (End())
                {
                    continue;
                }

                // Store This Point;
                beginInx = inx;

                if (IsCommand())
                {
                    // Skip Spaces
                    SkipSpaces();
                    inx--;
                    if (End())
                    {
                        continue;
                    }

                    ParseCommand();
                }
                else if (IsEquation(p_line))
                {
                    if (SplitEquation(p_line, VariableAssignment))
                    {
                        FVar temp = null;
                        if ((temp = ListHandler.ML_VariableList(MLGUI.GetVariable, is_var)) != null)
                        {
                            ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, temp.varName, is_val);
                        }
                        else
                        {
                            ListHandler.ML_VariableList(MLGUI.AddItem, is_var, is_val);
                        }
                    }
                    else if (SplitEquation(p_line, FunctionAssignment))
                    {
                        FunctionTree temp = null;
                        if ((temp = ListHandler.ML_FunctionList(MLGUI.GetFunction, is_var)) != null)
                        {
                            ListHandler.ML_FunctionList(MLGUI.UpdateFunctionExpression, is_var, is_func);
                        }
                        else
                        {
                            ListHandler.ML_FunctionList(MLGUI.AddItem, is_var, is_func);
                        }

                        //foreach (FVar v in temp.varList)
                        //{
                        //if (ListHandler.ML_FunctionList(MLGUI.GetFunction, v.varName))
                        //}
                    }
                    else
                    {
                        syntaxError = true;
                        Error();
                    }
                }
                else if (IsVariable(p_line))
                {
                    ListHandler.ML_VariableList(MLGUI.RemoveItem, is_var);
                    ListHandler.ML_VariableList(MLGUI.AddItem, is_var);
                }
                else if (IsFunction(p_line))
                {
                    FunctionEntry temp = null;
                    if ((temp = ListHandler.ML_FunctionListSecondary(MLGUI.GetFunctionFromExpression, is_func)) != null)
                    {
                        r2.AppendText(temp.Name);
                        r2.SelectionStart = r2.TextLength;
                        r2.ScrollToCaret();
                    }
                    else
                    {
                        ListHandler.ML_FunctionList(MLGUI.AddItem, "", is_func);
                    }
                }
                else
                {
                    parseError = true;
                    Error();
                }

                if (output[0] != "" && show_output)
                {
                    string lu = (u + 1) + "";
                    r3.AppendText("line-" + lu);
                    for (int o = 0; o < (5 - lu.Length); o++)
                    {
                        r3.Text += "  ";
                    }
                    r3.AppendText("\t" + output[0] + "\n");

                    int z = 1;
                    while (output[z] != "" && z < output.Length)
                    {
                        for (int o = 0; o < 10; o++)
                        {
                            r3.Text += "  ";
                        }
                        r3.AppendText("\t" + output[z] + "\n");
                        z++;
                    }
                }
            }
            processing = false;
        }