/flatcalc

world's worst interpreter

Primary LanguageRustMIT LicenseMIT

flatcalc

This repository contains two different implementations of a very simple calculator language. The "language" is just binary integer operations, like (47 - 26) * 2. We have a parser, pretty printer, interpreter, and random expression generator for this language. The implementations are:

  • On the main branch, a "normal" version where the AST nodes are allocated normally and use pointers to refer to their children.
  • On the flat branch, a "flattened" version where the AST nodes are packed together into one big array---i.e., an arena or a region. The nodes refer to each other with plain integer indices into the array.

The best way to view this repository is by comparing the two branches. That way, you can see exactly what has to change to "flatten" the whole implementation.

Run It

Use flatcalc or flatcalc interp to parse an expression from stdin and evaluate it. There is also flatcalc pretty, which just pretty-prints the expression from stdin back out on stdout.

flatcalc gen randomly generates a really big expression. There is an optional seed, so do flatcalc gen 42 to get a deterministic expression. You can also immediately run the randomly generated expression (without printing it) by typing flatcalc gen_interp 42. This is nice for benchmarking.

There is a make bench target that uses Hyperfine for a quick performance measurement.

Author

This is by Adrian Sampson. I can't imagine why you'd want to use the code, but the license is MIT nonetheless.