Created
January 28, 2012 13:13
-
-
Save martintrojer/1694263 to your computer and use it in GitHub Desktop.
scheme-clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let [[fst & rst] (parse "(+ 1 1)")] | |
[fst rst]) | |
--> [:+ (1.0 1.0)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(parse-all "foo bar") | |
--> [:foo :bar] | |
(parse "12") | |
--> 12.0 | |
(parse "(+ 1 a)") | |
--> [:+ 1.0 :a] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 | |
=> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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