Turning "price - product cost - shipping" into a calculator: a plain-word formula parser with no eval
Most people don't think in spreadsheet syntax. Ask an online seller how they work out profit per order and they'll say something like "price minus product cost minus shipping." Not =B2-C2-D2 . I wanted a calculator builder where you can type exactly that, price - product cost - shipping , and get a working calculator with labelled inputs. Here's how the parser behind it works, in about 100 lines…
Most people don't think in spreadsheet syntax when calculating profit per online order. They might say something like "price minus product cost minus shipping" instead of writing "=B2-C2-D2" in a spreadsheet. To create a calculator builder that accepts formulas in plain English, the parser uses a series of steps without using a built-in eval function.
First, it cleans up the formula by normalizing the typography. Then, it converts multi-word names into a single variable, such as "price - product cost - shipping" becoming "price_product_cost_shipping". Next, it detects the inputs required for the formula and organizes them in the order they appear. After that, it converts abbreviations into human-readable labels based on a predefined dictionary.
Finally, the parser evaluates the formula safely, without using eval, by using a recursive-descent evaluator that supports numbers, basic arithmetic operations, parentheses, and a limited set of functions. This ensures that the calculator can be chained together for more complex calculations and can be accessed through a JSON API or remote MCP server, allowing tools like AI assistants to run them accurately.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.