Exemplo n.º 1
0
        /// <summary>
        /// Method for given a set of tokens and the location of the plot token, create a Plot function object.
        /// </summary>
        /// <param name="tokens"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static PlotFunction plotFunctionHandle(List <Token> tokens, int i)
        {
            //List for storing the equation to be drawn i.e. "Y=X"
            List <Token> equationTokens = getEquation(tokens, ref i);

            //skip over the comma
            i++;
            //Evaluate the range expression i.e. (1<X<10) - it does this by getting Xmin, Xmax and the necessary increment.
            Token[] elements = evaluateRange(tokens, ref i);
            Token   Xmin     = elements[0];
            Token   Xmax     = elements[1];
            Token   inc      = elements[2];

            PlotFunction newPlot = new PlotFunction(equationTokens, Xmin, Xmax, inc, tokens);

            newPlot.curIndex     = i;
            newPlot.varNameIndex = Convert.ToInt32(elements[3].GetValue());

            return(newPlot);
        }
        public GraphDrawer(PlotFunction plotFunc, MainWindow windowToDrawOn)
        {
            this.plotFunc = plotFunc;

            window = windowToDrawOn;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Method called by the interpreter to analyse the set of tokens gathered by the lexer.
        /// </summary>
        /// <param name="tokens"></param>
        /// <returns> the value of the given expression.</returns>
        public double AnalyseTokens(List <Token> tokens)
        {
            //check if there are any function definitions present.
            for (int i = 0; i < tokens.Count; i++)
            {
                //plot func has the following syntax:
                //plot(Y=X,Xmin,Xmax,inc)
                //So need to gather the algebraic function, Xmin, Xmax and the increment
                //And pass it into a PlotFunction object for processing.
                if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.PLOT)
                {
                    //the plot function must be the first token.
                    if (i != 0)
                    {
                        throw new ItemsBeforePlotFunctionException("Plot function found in unexpected token position " + i);
                    }

                    PlotFunction plot = PlotFunction.plotFunctionHandle(tokens, i);

                    if (plot.curIndex != (tokens.Count - 1))
                    {
                        throw new ItemsAfterPlotFunctionException("Nothing can follow after the plot function definition.");
                    }
                    plot.getValues();

                    /////////////////////////////////////////////////////////////////
                    //Draw onto the canvas.
                    this.g = new GraphDrawer(plot, l.window);
                    if (Globals.SHOW_GRAPH_CANVAS)
                    {
                        g.Draw();
                    }
                    /////////////////////////////////////////////////////////////////

                    //Draw it also onto LiveCharts.
                    l.dataPoints    = g.plotFunc.dataPoints;
                    l.canvasXLabels = g.Xlabels;
                    l.Xname         = g.plotFunc.X;
                    l.Yname         = g.plotFunc.Y;
                    if (Globals.SHOW_LIVE_CHARTS)
                    {
                        l.Draw();
                    }

                    status = STATUSES.PLOT_FUNCTION_CALLED;
                    return(double.NaN);
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.SIN)
                {
                    //create the sin function and find a value.
                    SinFunction s = new SinFunction(tokens, i, false);
                    tokens = s.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.COS)
                {
                    CosFunction c = new CosFunction(tokens, i, false);
                    tokens = c.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.TAN)
                {
                    TanFunction t = new TanFunction(tokens, i, false);
                    tokens = t.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.LOG)
                {
                    LogFunction l = new LogFunction(tokens, i, true);
                    tokens = l.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.LN)
                {
                    LnFunction l = new LnFunction(tokens, i, false);
                    tokens = l.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.SQRT)
                {
                    SqrtFunction s = new SqrtFunction(tokens, i, false);
                    tokens = s.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.ROOT)
                {
                    RootFunction r = new RootFunction(tokens, i, true);
                    tokens = r.getNewEquation();
                }
                else if (tokens[i].GetType() == Globals.SUPPORTED_TOKENS.ABS)
                {
                    AbsFunction a = new AbsFunction(tokens, i, false);
                    tokens = a.getNewEquation();
                }
            }
            return(processTokens(tokens));
        }