Exploring Languages with Interpreters
and Functional Programming
Chapter 43

H. Conrad Cunningham

24 November 2018

Copyright (C) 2017, 2018, H. Conrad Cunningham
Professor of Computer and Information Science
University of Mississippi
211 Weir Hall
P.O. Box 1848
University, MS 38677
(662) 915-5358

Browser Advisory: The HTML version of this textbook requires a browser that supports the display of MathML. A good choice as of November 2018 is a recent version of Firefox from Mozilla.

43 Calculator: Modular Structure

43.1 Chapter Introduction

TODO: Write missing pieces and flesh out other sections

43.2 Module Dependencies

An ELI Calculator interpreter consists of seven modules with the module dependencies shown in Figure 43-1.

Figure 43-1: ELI Calculator language module dependencies
Figure 43-1: ELI Calculator language module dependencies

We examine each module in the following sections.

43.3 Values Module

The Values module Values is introduced in Chapter 42. It encapsulates the definitions and functions that know the specific representation of an ELI language’s data. Other modules should use its public features to enable the representation to be changed easily.

The secret of the Values module is the specific representation for the values supported by the language.

This module currently supports both the ELI Calculator language and the ELI Imperative Core language we examine in a later chapter. For both languages, the only type of values supported are integers. Booleans are encoded as integers.

The Values module’s abstract interface includes the following public features

The interface also includes the following, which are intended for the exclusive use of the lexical analysis module to support finite range integers.

43.4 Environments Module

An environment is a mapping between a name and its value.

The Environments module Environments is introduced in Chapter 42. It encapsulates the definitions and functions that know the specific representation of an environment for an ELI language. Other modules should use its public features to enable the representation to be changed easily.

The secret of the Environments module is the specific representation for the environments used in interpreter for the ELI language.

This module currently supports both the ELI Calculator and the ELI Imperative Core languages (in a future chapter).

The Environments module’s abstract interface includes the following public features.

43.5 Abstract Syntax Module

The Abstract_Synax module AbSynCalc module is introduced in Chapter 42. It centralizes the abstract syntax definition for the ELI Calculator language so it can be imported where needed.

The abstract syntax consists of algebraic data type definitions. The semantics of the abstract syntax tree is known by modules that must create (e.g. parser) and use (e.g. evaluator) the abstract syntax trees.

The module defines and exports the algebraic data type Expr and implements it as an instance of class Show. Values of type Expr are the abstract syntax trees for the ELI Calculator language.

The module also exports types ValType and Name that it imports from the the Values module.

43.6 Evaluator Module

The Evaluator module EvalCalc encapsulates the definition of the evaluation function (i.e. the semantics) of the ELI Calculator language.

The secret of the EvalCalc is the implementation of the semantics of the language, including the specifics of the environment.

The Evaluator module’s abstract interface includes the following public features.

43.7 Lexical Analysis Module

The Lexical Analyzer module LexCalc is introduced in Chapter 44. It is common to both the prefix and infix parsers for the ELI Calculator language.

The secret of this module is the lexical structure of the concrete language syntax.

The Lexical Analyzer module’s abstract interface consists of the following public features.

43.8 Parser Modules

Chapter 44 introduces two alternative implementations of the Parser abstract module for the ELI Calculator language. These implementations correspond to the two different concrete syntaxes given in Chapter 41. Both use the same Lexical Analyzer.

The secret of the abstract parser module is how the input syntax is recognized and translated to the abstract syntax.

The Parser abstract module’s abstract interface consists of the following public features.

43.9 REPL Modules

A REPL (Read-Evaluate-Print Loop) is a command line interface with the following cycle of steps:

  1. Read an input from the command line.

    If the input is an exit command, exitloop ; else continue.

  2. Evaluate the expression after parsing.

  3. Print the resulting value.

  4. Loop back to step 1.

The secret of the REPL modules is how the user interacts with the interpreter.

The ELI Calculator language interpreter provides two REPL modules:

In addition to accepting ELI Calculator expressions, they accept the REPL commands :set, :display, and :quit.

43.10 Code Improvement Modules

In addition, the partially implemented Process AST module includes the skeleton simplify and deriv functions discussed in Chapter 42.

This module is “wrapper” for the EvalCalc module currently.

43.11 What Next?

TODO

43.12 Exercises

TODO

43.13 Acknowledgements

I initially developed the ELI Calculator language (then called the Expression Language) case study for the Haskell-based offering of CSci 556, Multiparadigm Programming, in Spring 2017. I based this work, in part, on ideas from:

In 2017, I continued to develop this work as Chapter 10, Expression Language Syntax and Semantics, of my 2017 Haskell-based programming languages textbook.

In Summer 2018, I divided the previous Expression Language Syntax and Semantics chapter into three chapters in the 2018 version of the textbook, now titled Exploring Languages with Interpreters and Functional Programming. Section 10.2 became chapter 42, Calculator Concrete Syntax, sections 10.3-5 and 10.7-8 became chapter 43, Calculator Abstract Syntax & Evaluation, and sections 10-6 and 10-9 and section 11.5 became Chapter 44, Calculator Architecture (this chapter).

In Summer 2018, I divided the previous Expression Language Syntax and Semantics chapter into three chapters in the 2018 version of the textbook, now titled Exploring Languages with Interpreters and Functional Programming. Section 10.2 became Chapter 41, Calculator Concrete Syntax, sections 10.3-5 and 10.7-8 became Chapter 42, Calculator Abstract Syntax & Evaluation, and sections 10-6 and 10-9 and section 11.5 were expanded into Chapter 43, Calculator Modular Structure (this chapter).

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.

43.14 References

[Cunningham 2018]:
H. Conrad Cunningham. Notes on Scala for Java Programmers, 2018 (which is itself adapted from the tutorial [Schinz 2018] Scala for Java Programmers
[Kamin 1990]:
Samuel N. Kamin. Programming Languages: An Interpreter-Based Approach, Addison-Wesley, 1990.
[Linz 2017]:
Peter Linz. An Introduction to Formal Languages and Automata, Fifth Edition, Jones and Bartlett, 2017.
[Schinz 2018]:
Michel Schinz and Philipp Haller. A Scala Tutorial for Java Programmers, Scala Language Website, accessed February 2018.
[Sestoft 2012]:
Peter Sestoft. Programming Language Concepts, Springer, 2012.
[Wikipedia 2018a]:
Wikipedia. Articles on Formal Grammar, Regular Grammar, Context-Free Grammar, Backus-Naur Form, Extended Backus-Naur Form, and Parsing. Accessed 9 August 2018.
[Wikipedia 2018b]:
Wikipedia. Articles on Abstract Syntax and Associative Array, Accessed 9 August 2018.

43.15 Terms and Concepts

TODO