Skip to content

junwei9638/OurScheme-Project2

Repository files navigation

OurScheme – Project 2

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.


Features

  • All primitive expressions can be evaluated.
  • define supported (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.

Program Workflow

  1. Starts with:
    Welcome to OurScheme!
    
  2. Prompts with > and waits for input.
  3. Reads and evaluates an S-expression:
  • If no syntax error:
    • Calls EvalSExp.
    • If no evaluation error → prints result.
    • Else → prints evaluation error.
  • If syntax error → prints syntax error.
  1. Terminates when (exit) is entered:
    Thanks for using OurScheme!
    
  2. If EOF is reached before (exit), prints:
    ERROR (no more input) : END-OF-FILE encountered
    

Supported Primitives

1. Constructors

  • cons (2)
  • list (>=0)

2. Quoting

  • quote (1)
  • ' (1)

3. Definition

  • define (2) → binds a symbol to an S-expression
  • Cannot redefine system primitives (e.g., cons, car).

4. Part Accessors

  • car (1)
  • cdr (1)

5. Predicates (1 argument each)

  • atom?
  • pair?
  • list?
  • null?
  • integer?
  • real?
  • number? (same as real? in OurScheme)
  • string?
  • boolean?
  • symbol?

6. Arithmetic, Logic, Strings

  • Arithmetic: + - * / (≥ 2 arguments)
  • Logical: not (1), and (≥ 2), or (≥ 2)
  • Comparison: > >= < <= = (≥ 2 arguments)
  • String operations:
    string-append, string>?, string<?, string=?

7. Equivalence

  • eqv? (2)
  • equal? (2)

8. Sequencing

  • begin (≥ 1)

9. Conditionals

  • if (2 or 3)
  • cond (≥ 1) with else keyword

10. Environment Reset

  • clean-environment (0)

Example Interaction

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!

Error Handling

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors