A small JavaScript application built in Node that converts infix expression (e.g., 1 + 1) to prefix (e. g., + 1 1)
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Polsky - Converts infix expressions to prefix

Polsky is a tool for converting mathematical infix expressions into prefix notation (polish notation).

Currently the expressions are required to be a fairly strict format where they spaced out by at least one space per operand/operator.

e.g., ( 1 + 2 ) * 5

The exposed api will provides the following methods:

  • #parse() method which returns an Abstract Syntax Tree
  • #print([reduce]) - returns the expression as a string in Polish/Prefix notation.
    • Optionally when reduce is set to true the print formatter will attempt to reduce the function. If the expression is easily solvable it will return the evaluated expression result. Otherwise it will:
      • Attempt to reduce instance of Division/Subtraction in the tree as they increase complexity by needing to have their leaf nodes in left/right order
      • Attempt to flatten multiplication and addition nodes reducing the amount of sub nodes
      • Attempt to combine like terms in multiplication and addition will be condensed (e.g., a + a + a + a -> (* a 4) or a * a * a * a -> (^ a 4))


Clone the repo and enter the directory

npm install - necessary for the command line script which relies on one external dependency.

You can either run through node, your own script or parse a file of expressions using:

./prefixer.js expression_file

./prefixer.js -r expression_file

prefixer.js takes -r or --reduce to reduce the format. prefixer only exposes the print portion of Polsky.