A simplified Scheme interpreter (2017 Spring PL Project 2).
This project extends Project 1 by adding evaluation of expressions, definitions, conditionals, sequencing, and primitive functions.
- All primitive expressions can be evaluated.
definesupported (binding symbols to S-expressions).- Conditionals supported (
if,cond). - Sequencing / functional composition supported (
begin). - Environment management with
clean-environment. - Maintains Project 1’s scanner, parser, pretty-printer, and error reporting.
- Starts with:
Welcome to OurScheme! - Prompts with
>and waits for input. - Reads and evaluates an S-expression:
- If no syntax error:
- Calls
EvalSExp. - If no evaluation error → prints result.
- Else → prints evaluation error.
- Calls
- If syntax error → prints syntax error.
- Terminates when
(exit)is entered:Thanks for using OurScheme! - If EOF is reached before
(exit), prints:ERROR (no more input) : END-OF-FILE encountered
cons (2)list (>=0)
quote (1)' (1)
define (2)→ binds a symbol to an S-expression- Cannot redefine system primitives (e.g.,
cons,car).
car (1)cdr (1)
atom?pair?list?null?integer?real?number?(same asreal?in OurScheme)string?boolean?symbol?
- Arithmetic:
+ - * /(≥ 2 arguments) - Logical:
not (1),and (≥ 2),or (≥ 2) - Comparison:
> >= < <= =(≥ 2 arguments) - String operations:
string-append,string>?,string<?,string=?
eqv? (2)equal? (2)
begin (≥ 1)
if (2 or 3)cond (≥ 1)withelsekeyword
clean-environment (0)
Welcome to OurScheme!
> (cons 3 4)
( 3
.
4
)
> (define a 5)
a defined
> (cons a 10)
( 5
.
10
)
> (if (> a 3) 'good 'bad)
good
> (cond ((> a 10) 'big) (else 'small))
small
> (clean-environment)
environment cleaned
> (exit)
Thanks for using OurScheme!Four possible error types:
Project 1 errors (still valid in Project 2)
- ERROR (unexpected token) : atom or '(' expected ...
- ERROR (unexpected token) : ')' expected ...
- ERROR (no closing quote) : END-OF-LINE ...
- ERROR (no more input) : END-OF-FILE encountered New Project 2 errors
- ERROR (non-list) : ...
- ERROR (incorrect number of arguments) :
- ERROR (exit with arguments) : exit
- ERROR ( with incorrect argument type) :
- ERROR (attempt to apply non-function) :
- ERROR (no return value) :
- ERROR (unbound symbol) :
- ERROR (division by zero) : /
- ERROR (DEFINE format) : ( define ... )
- ERROR (COND format) : ( cond ... )
Notes
- Scheme is case-sensitive (cons ≠ CONS).
- Primitives cannot be redefined.
- else is a keyword only in the last condition of cond.
- String formatting:
- Floats always printed with 3 decimal places.
- (clean-environment) clears all user definitions.