Skip to content

Instantly share code, notes, and snippets.

@danlentz
Forked from pooya-raz/instaparse-sexp.clj
Created December 4, 2013 06:07
Show Gist options
  • Select an option

  • Save danlentz/7783105 to your computer and use it in GitHub Desktop.

Select an option

Save danlentz/7783105 to your computer and use it in GitHub Desktop.
(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