-
-
Save danlentz/7783105 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (ns parse.core | |
| (:require [instaparse.core :as insta])) | |
| (def parser | |
| (insta/parser | |
| "sexp = lparen operation rparen | |
| <lparen> = <'('> | |
| <rparen> = <')'> | |
| operation = operator + args | |
| operator = '+' | |
| args = snumber+ ssexp* | |
| <ssexp> = space sexp | |
| <snumber> = space number | |
| <space> = <#'[ ]*'> | |
| number = #'[0-9]+' | |
| ")) | |
| (defn choose-op [op] | |
| (case op | |
| "+" +)) | |
| (def transform-options | |
| {:number read-string | |
| :args vector | |
| :operator choose-op | |
| :operation apply | |
| :sexp identity | |
| }) | |
| (defn lisp [input] | |
| (->> (parser input) (insta/transform transform-options))) | |
| (lisp "(+ 1 2 (+ 3 4))") ;; => 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment