This is an implementation of Scheme interpreter, but not completed compared to the Scheme standard.
Scheme syntax is different from most other languages you may be familiar with. Consider:
# Python Code
if x.val() > 0:
func(A[i] + 1, ["one", "two"])
; Scheme Code
(if (> (val x) 0)
(func (+ (aref A i) 1)
(quote (one two)))
Scheme syntax is much simpler compared to other programming languages:
- Using a parenthesized-list Polish notation. Everything else is a list.
- Consisting solely of expressions.
- The first element of the list determines what it means. A list may start with a keyword e.g. (if ...) or a non-keyword, e.g. (func ...), is a function call.
This interpreter defines a language that is a subset of Scheme using only six special forms:
- variable reference. var
- constant literal. number
- quotation. (quote exp)
- conditional. (if test conseq alt)
- definition. (define var exp)
- procedure. (proc arg...)
Interpretation process: program(str) -> parse -> abstract syntax tree (list) -> eval -> result (object)
And here is a short example to show what the parse and eval do:
>> program = "(begin (define r 10) (* pi (* r r)))"
>>> parse(program)
['begin', ['define', 'r', 10], ['*', 'pi', ['*', 'r', 'r']]]
>>> eval(parse(program))
314.1592653589793
There is no need to enter "eval(parse(...))" everytime. There is a
repl
function which allows you to enter an expression, and see it
immediately.
>>> repl()
Scheme> (begin (define r 10) (* pi (* r r)))
314.159265359
Scheme> (if (> (* 10 10) 20) (* 7 6) oops)
42
###References