Code Monkey home page Code Monkey logo

graphexpression's Introduction

GraphExpressionEvaluator

A small lib for compiling and evaluating simple math expressions with a variable 'x'

A small project made for fun. I felt like writing a compiler having watched some of Jonathan Blow's talks about his own programming language. However writing a full language compiler is a big task, and all I really wanted to do was handwriting a simple parser for fun. So I decided to write a 'compiler' for simple mathematical expressions.

The expressions can contain the following symbols:

  • Real numbers
  • The variable X
  • Parenthesis ( )
        • / and ^ (power)

Expressions like "2x^2+3x-4" are compiled into a simple bytecode which can then be evaluated with a specific value for the variable X.

How to use

The compiling of math expressions is done in 3 steps each represented by a class:

  • Lexer
  • Parser
  • Compiler

These 3 classes can be used to create a byte string like this:

Lexer _lexer = new Lexer();
Parser _parser = new Parser();
Compiler _compiler = new Compiler();

string expression = "2x+3";

IList<Token> tokens = _lexer.Tokenize(expression); // Turn the expression into a list of reconised tokens - no error checking
try
{
  IList<Token> rpn = _parse.Parse(tokens); // Parses the tokens into Reverse Polish Notation - will throw an exception if the expression is not valid
  byte[] bytes = _compiler.Compile(rpn); // Compile the RPN into a byte code
} 
catch (Parser.ParsingException exception)
{
  System.Console.WriteLine(exception.Message);
}

The compiled expression is in bytes and can be evaluated for different values of X like this with the Interpreter class:

byte[] bytes; // contains the byte code we compiled previously for expression: 2x+3

Interpreter _interpreter = new Interpreter();

try
{
  // Evaluate 2x+3 or diferent values of X
  float xIs2 = _interpreter.Evaluate(bytes, 2); // xIs2 == 7
  float xIsNeg2 = _interpreter.Evaluate(bytes, 2); // xIsNeg2 == -1
  float xIs0 = _interpreter.Evaluate(bytes, 2); // xIs0 == 3
}
catch (InterpreterException exception)
{
  System.Console.WriteLine(exception.Message);
}

GraphExpressionDrawer

To demonstrate GraphExpressionEvaluator I have created a simple WPF program that can draw the expressions as graphs.

It is quite limited in features, but it offers live evaluation and drawing of an expression as you enter it, and the choice between drawing graphs as straight line segments or as bezier segments.

GraphExpressionDrawer Screenshot

graphexpression's People

Contributors

pstudio avatar

Stargazers

GuanQH avatar

Watchers

 avatar

graphexpression's Issues

TODO: Cached Graph Drawing

Cache all graph drawing elements instead of recalculating them each time something changes.

For instance there is no need to recalc transformations, draw axis, and calc/draw all graphs again, just because a new graph is added.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.