Beta

Beta is an extension of Alpha that allows programmers to declare and subsequently use symbols (names). Based on the language and design specifications given below, build a Beta interpreter.

Beta Syntax

The following EBNF rules are added to or modify the rules for Alpha:

<Phrase> ::= <Expression> | <Declaration>
<Expression> ::= <Literal> | <Symbol> | <FunCall> | <SpecialForm>
<Symbol> ::= <Letter>(<Letter> | <Digit>)*
<SpecialForm> ::= (if <Expression> <Expression> <Expression>) |
   (and <Expression>+) | (or <Expression>+)
<Declaration> ::= [define <Symbol> <Expression>]

Note that the expressions (and e1 e2 ...) and (or e1 e2 ...) are no longer considered function calls. They are special forms, along with (if e1 e2).

Beta Semantics

The Beta context includes an environment, which is a table of the form:

The semantics of beta expressions are no different than the semantics of Alpha expressions, except Beta expressions may contain symbols that must be looked up in the Beta console's environment.

Executing the declaration:

[define y (+ 2 2)]

Creates the binding:

(y, 4)

This binding is installed in the context's environment. When the use types a symbol, the environment is searched for the corresponding value.

The execution method(s) for the special forms use short-circuit evaluation. No operands are executed unless necessary.

The Beta Console

Here's a sample interaction with the Beta console:

C:\pearce\cs152\beta>java VMConsole
type "help" for commands
-> [define pi 3.14]
done
-> [define radius 5]
done
-> [define area (* radius radius pi)]
done
-> area
78.5
-> (and (< 3 4) (= 2 1) (= 1 (/ 1 0)))
false
-> (or (< 3 4) (= 2 1) (= 1 (/ 1 0)))
true
-> (if (< 3 4) (* 6 7) (/ 1 0))
42
->

Note that and and or expressions are evaluated using short circuit evaluation, while if expressions are evaluated using conditional evaluation.

The Design of Beta

Most of the classes in Alpha can be used in Beta. This includes OmegaConsole, Environment, and the Phrase hierarchy. Two new subclasses you must add are Declaration and Symbol. Beta will also require a SpecialForm class:

Note: if you must make any changes to Expression, FunCall, Literal, etc., then these should be understood as design flaws in Alpha.