Skip to content

Instantly share code, notes, and snippets.

@Johniel
Created June 10, 2013 23:08
Show Gist options
  • Save Johniel/5753253 to your computer and use it in GitHub Desktop.
Save Johniel/5753253 to your computer and use it in GitHub Desktop.
taplの3章?
(ns tapl.core
(:require [instaparse.core :as insta]))
(def my-parser
(insta/parser
"<T> := 'true' | 'false' | '0' |
'succ'<space>T | 'pred'<space>T | 'iszero'<space>T |
'if'<space>T<space>'then'<space>T<space>'else'<space>T;
space := #'\\s';"))
(defn succ [n] (inc n))
(defn pred [n] (dec n))
(defn iszero [n]
(zero? n))
(defn my-eval [exp]
(try (first ((fn -eval [[head & tail]]
(case head
"succ" (let [[h t] (-eval tail)] [(succ h) t])
"pred" (let [[h t] (-eval tail)] [(pred h) t])
"iszero" (let [[h t] (-eval tail)] [(iszero h) t])
"0" [0 tail]
"true" [true tail]
"false" [false tail]
"if" (let [[cnd rest1] (-eval tail)
[then rest2] (-eval (rest rest1))
[else rest3] (-eval (rest rest2))]
[(case cnd true then false else :else nil) rest3])
:else nil)) exp))
(catch Exception e nil)))
(defn -main [& args]
(->> (line-seq (java.io.BufferedReader. *in*))
(map #(str % ":" (my-eval (my-parser %))))
(interpose "\n")
(apply str)
(println)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment