/// <summary> /// Method called to process an expression. /// </summary> /// <param name="tokens"></param> /// <returns></returns> private double processTokens(List <Token> tokens) { this.tokens = tokens; //Analyse the syntax to see if it is valid. SyntaxAnalyser s = new SyntaxAnalyser(tokens); s.checkTokens(); //If it is then do some preprocessing on the input. Preprocessor p = new Preprocessor(tokens); this.tokens = p.processTokens(); getNextToken(); double value = analyseExpressions(double.NaN); if (WRITE_TO_FILE) { VariableFileHandle.saveVariables(variables); } return(value); }
/// <summary> /// Function to load the JSON file given by the user into the datagrid. /// </summary> /// <param name="w"></param> public static void loadVarFileIntoDataGrid(MainWindow w) { var openfileDialog = new OpenFileDialog { Filter = "JSON File (*.json)|*.json" }; var dialogResult = openfileDialog.ShowDialog(); if (dialogResult == true) { var filename = openfileDialog.FileName; JArray array = null; //Display a warning before proceeding. CustomMsg c = new CustomMsg("Are you sure you want to continue? This will override all variables currently stored in the table.", "Load Variable File"); c.ShowDialog(); bool?res = c.DialogResult; if (!((bool)res)) { return; } try { array = VariableFileHandle.LoadFromExternalFile(filename); Hashtable vars = new Hashtable(); // Put all the variables loaded into a hashtable of the form // key -> Name // value -> Tuple( Value, Type). foreach (JObject variable in array) { string variableName = variable["name"].ToString(); string variableValue = variable["value"].ToString(); vars.Add(variableName, variableValue); } VariableFileHandle.saveVariables(vars); w.loadVarsIntoDataGrid(); } catch (Exception e) { ErrorLoadingVariableFileException e1 = new ErrorLoadingVariableFileException("Error loading variable file."); ErrorMsg err = new ErrorMsg(e1.Message, e1.ErrorCode); err.ShowDialog(); } } }
/// <summary> /// Method to run the interpreter given a certain input. /// </summary> /// <param name="codeToRun"></param> /// <returns></returns> public string RunInterpreter(string codeToRun) { string temp = Regex.Replace(codeToRun, @"\s+", ""); //Check if the user has typed "clear" if (temp.Contains("clear")) { if (temp == "clear") { VariableFileHandle.clearVariables(); return("\tCleared Variables."); } else { throw new ClearCommandException("'clear' can only be used on its own, nothing else can be added."); } } //Use the lexer to tokenise the input List <Token> tokens = lexer.TokeniseInput(codeToRun); //Then put the gathered tokens into the parser. double result = parser.AnalyseTokens(tokens); switch (parser.status) { case Parser.STATUSES.PLOT_FUNCTION_CALLED: return("\tRefer to figure."); case Parser.STATUSES.UNASSIGNED_RESULT: string res = Convert.ToString(result); //assign it to Ans as that is the default variable name. vars["Ans"] = res; VariableFileHandle.saveVariables(vars); return("\tvar Ans = \n\t\t" + Convert.ToString(result)); case Parser.STATUSES.VARIABLE_ASSIGNED: return("\tvar " + parser.varName + " = \n\t\t" + vars[parser.varName]); default: return(""); } }
/// <summary> /// Method for getting the list of data points from the expression so they can be drawn by the graph drawer. /// Result stored in Plot Function object under data points. /// </summary> /// <returns></returns> public void getValues() { //Say if your expression is Y=X+2 //First step is to remove the Y= part and then evaulate the X+2 part by parsing it through the parser. removeFirst2Tokens(); //Next step is to get the variable name in the equation. string varName = getVariableName(); Hashtable vars = VariableFileHandle.getVariables(); //get the number of data values to plot. int numIncrements = Convert.ToInt32(Math.Floor((Xmax - Xmin) / inc) + 1); Parser p; bool flag = false; //Save the original equation. List <Token> eqnCopy = new List <Token>(Equation); //Iterate through every value of X and get a Y value. for (int i = 0; i < numIncrements; i++) { double Xvalue = Xmin + i * inc; if (varName != null) { //Update the value of the variable so that it can be used in the expression. vars[varName] = Convert.ToString(Xvalue); //Save the change VariableFileHandle.saveVariables(vars); p = new Parser(vars); double Yvalue = p.AnalyseTokens(Equation); //Copy the Equation over incase it has been modified. Equation = new List <Token>(eqnCopy); dataPoints.Add(new DataPoint(Xvalue, Yvalue)); } else { //This only occurs if only 1 variable has been specified. p = new Parser(vars); double Yvalue = p.AnalyseTokens(Equation); //If the equation specified is 'X=3' or 'x=3' then you want a vertical line. if (Y.ToLower() == "x") { flag = true; dataPoints.Add(new DataPoint(Yvalue, Xvalue)); } else { //otherwise draw a horizontal line. X = "x"; dataPoints.Add(new DataPoint(Xvalue, Yvalue)); } } } //Check if the dependent variable name in the equation matches the one given in the range. if (!(X is null)) { if (allTokens[varNameIndex].GetValue() != X) { throw new InvalidVariableNameInRangeException("Dependent variable " + X + " expected. " + allTokens[varNameIndex].GetValue() + " found instead."); } } if (flag) { X = Y; Y = "y"; } setMinAndMaxXandYValues(); printDataPoints(); }