Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created January 28, 2012 13:13
Show Gist options
  • Save martintrojer/1694263 to your computer and use it in GitHub Desktop.
Save martintrojer/1694263 to your computer and use it in GitHub Desktop.
scheme-clojure
(let [[fst & rst] (parse "(+ 1 1)")]
[fst rst])
--> [:+ (1.0 1.0)]
(defn _eval [exp env]
(cond
; self-evaluating?
(or (number? exp) (string? exp) (fn? exp)) [exp env]
; var reference to be looked up in env
(keyword? exp) [(lookup exp env) env]
; parsed combinations (function calls)
(vector? exp) (let [[fst & rst] exp
[r e] (_eval fst env)]
(cond
; built-in function calls
(fn? r) (_apply r rst e)
; user defined function/lambda calls
(list? r) (let [[args body] r
n (zipmap args (map #(get-evval % e) rst))
new-env (cons n e)]
(_eval body new-env)) ; eval the first form only
:else [exp env]))
:else (throw (Exception. (format "invalid interpreter state %s %s" (str exp) (str env))))))
(defn _apply [f args env]
(f args env))
(parse-all "foo bar")
--> [:foo :bar]
(parse "12")
--> 12.0
(parse "(+ 1 a)")
--> [:+ 1.0 :a]
$ java -jar mtscheme-0.0.1-SNAPSHOT-standalone.jar
mtscheme 0.1
nil
=> (define (foreach f l) (if (not (null? l)) (begin (f (car l)) (foreach f (cdr l)))))
nil
=> (foreach display (list 1 2 3))
1.0
2.0
3.0
nil
=>
(tokenize "(foo)")
--> [[:open] [:symbol "foo"] [:close]]
(tokenize "\"foo\"")
--> [[:string "foo"]]
(tokenize "12")
--> [[:symbol "12"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment