Example: Scheme-Syntax Calculator

Example: A Scheme-Syntax Calculator

Introduction

This extended example implements a reader for Scheme lists and an evaluator for the Calculator language, an expression language of numbers and arithmetic call expressions.

This example includes several files. They can be downloaded together as a zip archive.
scheme_reader.py A parser for Scheme lists.
scheme_tokens.py A tokenizer for Scheme expressions.
scalc.py An evaluator for numbers and arithmetic call expressions.
buffer.py Utility classes for reading multi-line input.
ucb.py Utility functions for CS 61A.

The Scheme-Syntax Calculator Language

Syntax. Legal Calculator expressions are either numbers or well-formed Scheme lists that have an operator symbol as their first element. The latter are interpretered as call expressions. Legal operator symbols include +, -, *, and /.

Evaluation. The evaluation procedure for each operator is described in the lecture notes section about the Scheme-Syntax Calculator.

Read-Eval-Print. When run interactively, the interpreter reads Scheme expressions (without quotation or dotted lists), evaluates them, and prints the results.

    > 2
    2
    > (+ 1 2 (* 3 4))
    15