Continuation-Passing Interpreters II, Scheme.java
- Here is the resulting continuation passing version of interp3-7: interp4-1cps.scm
- Since context is built for "tail" calls, calls in which the value is simply returned and is not needed to complete a calculation, the values of arguments can simply be stored in global variables or "registers". This leads to an imperative interpreter, discussed in 4.3.
We should discuss this a little further.
- In section 4.4, exception handling is added to the defined language.
The handler-exp is evaluates to a function of one variable, which is installed as an exception handler continuation.[expression] ::= try [expression] handle [expression] (try-exp (body-exp expression?) (handler-exp expression?)) [epxression ::= raise [expression] (raise-exp (exp expression?))If body-exp finishes normally, its value is returned by the try. If a raise is encountered, its value is passed to the handler of the most recent try.
A handler can deal with the exception or can pass it up to the next nearest handler.
We should discuss this further.
- Java is an example of an OOL. It has
- objects which encapsulate state (stored in fields) and behavior (methods). Methods are called on objects, with possible parameters, or intuitively, messages are sent to objects. The state of the object may be changed, and a value (perhaps "void") is returned.
- classes that have instance objects differing only in their state.
- inheritance, in which new classes can be derived from existing ones, adding or possibly overriding state and/or behavior.
- polymorphism which allows objects of different classes to respond in their own way to messages.
- These features are illustrated in Scheme.java, a call-by-value interpreter in Java that parses and evaluates list syntax.
Classes with constructors, methods: (underline = class, italics = abstract)
- Env
Env() Env(vars : Vector, vals : Vector, h : Hashtable)
- extendPrim(opname : String, op : int)
- initEnv() : Env
- extend(args : Vector, vals : Vector) : Env
- apply(var : String) : Vector
- Value
- println()
- print()
- ProcValue extends Value
- apply(args : Vector) : Value
- ClosureValue extends ProcValue
ClosureValue(_formals : Vector, _body : Exp, _env : Env)
- print()
- apply(args : Vector) : Value
- PrimValue extends ProcValue
PrimValue(_opname : String, _op : int)
- print()
- apply(args : Vector) : Value
- getVal(args : Vector, i : int) : Value
- getInt(args : Vector, i : int) : int
- getPair(args : Vector, i : int) : PairValue
- IntValue extends Value
IntValue(_val : int)
- get() : int
- print()
- BoolValue extends Value
BoolValue(_val : boolean)
- get() : boolean
- print()
- VoidValue extends Value
- print()
- println()
- NullValue extends Value
- print()
- PairValue extends Value
PairValue(_v1 : Value, _v2 : Value)
- print()
- car() : Value
- cdr() : Value
- Exp
- eval(env : Env) : Value
- evalRands(rands : Vector, env : Env) : Vector
- Varref extends Exp
Varref(_var : String)
- eval(env : Env) : Value
- Lambda extends Exp
Lambda(_formals : Vector, _body : Exp)
- eval(env : Env) : Value
- App extends Exp
App(_rator : Exp, _rands : Vector)
- eval(env : Env) : Value
- Num extends Exp
Num(_val : int)
- eval(env : Env) : Value
- Num extends Exp
Bool(_val : boolean)
- eval(env : Env) : Value
- Bool extends Exp
- eval(env : Env) : Value
- Begin extends Exp
Begin(_exps : Vector)
- eval(env : Env) : Value
- If extends Exp
THIS NEEDS TO BE COMPLETED
If(_e1 : Exp, _e2 : Exp, _e3 : Exp)
- eval(env : Env) : Value
- Let extends Exp
Let(_vars : Vector, _exps : Vector, _body : Exp)
- eval(env : Env) : Value
- Letrec extends Exp
Letrec(_vars : Vector, _formalss : Vector, _exps : Vector, _body : Exp)
- eval(env : Env) : Value
- Set extends Exp
Set(_var : String, _exp : Exp)
- eval(env : Env) : Value
- EvalException extends RuntimeException
EvalException(msg : String)
- Scheme
- doFile(fname : String)
- main(argv : String[])
- Parser
...
- ...
- Assignment includes completing
Ifin Scheme.java.
- Coming, School (Scheme OOL)