Expression Tree Calculator
H. Conrad Cunningham
27 April 2022
Browser Advisory: The HTML version of this textbook requires a browser that supports the display of MathML. A good choice as of April 2022 is a recent version of Firefox from Mozilla.
These Scala programs are from the chapter Notes on Scala for Java Programmers [1] (as HTML) (as PDF).
OO versions using abstract base classes
TODO: In the following, I seem to have been exploring various OO approaches. These need to be reexamined to determine which should be included here. In some of these, I was also exploring dataclasses and type annotations.
These programs require the Python package Parsita, a parser combinator library similar to Scala’s.
TODO: These are not complete. At least a test driver is needed.
Abstract syntax
tree calc_ast2.py
adapted and extended to work with
Calculator expression parser – uses frozen data classes
Parser
calc_parser.py
– uses Parsita combinator-based parser
for simple Calculator-like language including function calls
Lua
Recursive Functions with Record Representation
exprRecFuncRecord.lua
Lua
Recursive Functions with List Representation
exprRecFuncList2.lua
Lua Evaluation
Function Table with List Representation
exprEvalTable2.lua
These programs require installation of a compatible LPEG library.
What I call the “recursive function version using case classes” is more or less the first version of these examples. It is closely based on the example in an early version of the Schinz and Haller tutorial [3]. I modified that version and also created the “traditional object-oriented version” for an assignment in the prototype offering of the Multiparadigm Programming course. I subsequently developed the Lua versions in 2013, the Haskell version in 2017, and the Python versions in 2018 for that or other courses. The Expression Tree is one of the examples or assignments I redesign and implement when I am learning and teaching a new language or want an assignment of that nature.
This example has been more or less expanded into the ELI Calculator Language introduced in several chapters of Exploring Languages with Interpreters and Functional Programming (ELIFP) [2].
I retired from the full-time faculty in May 2019. As one of my post-retirement projects, I am continuing work on possible textbooks based on the course materials I had developed during my three decades as a faculty member. In January 2022, I began refining the existing content, integrating separately developed materials together, reformatting the documents, constructing a unified bibliography (e.g., using citeproc), and improving my build workflow and use of Pandoc.
I maintain this chapter as text in Pandoc’s dialect of Markdown using embedded LaTeX markup for the mathematical formulas and then translate the document to HTML, PDF, and other forms as needed.