Skip to content

Instantly share code, notes, and snippets.

@lhorie
Last active May 8, 2022 21:03
Show Gist options
  • Save lhorie/3095cf62023b74d27fc9 to your computer and use it in GitHub Desktop.
Save lhorie/3095cf62023b74d27fc9 to your computer and use it in GitHub Desktop.
var readtable = {
"(": form,
" ": space, "\t": space, "\n": space, "\r": space,
}
var white = " \t\n\r"
var atomEnd = " \t\n\r);"
var formEnd = ")"
var escape = "\\"
function parse(s) {
s.marker = s.cursor
return (readtable[s.ch] || atom)(s)
}
function next(s) {s.ch = s.code.charAt(++s.cursor)}
function token(s) {return s.code.slice(s.marker, s.cursor)}
function is(set, s) {return set.indexOf(s.ch) > -1}
function form(s) {
var list = []
next(s)
while (!is(formEnd, s)) {
var item = parse(s)
if (item) list.push(item)
}
next(s)
return list
}
function space(s) {
next(s)
while (is(white, s)) next(s)
}
function atom(s) {
do {ch(s)} while (!is(atomEnd, s) && !(s.ch in readtable))
return token(s)
}
function ch(s) {
if (is(escape, s)) next(s)
next(s)
}
function p(s) {
return parse({cursor: 0, marker: 0, ch: "(", code: "(program " + s + ")"})
}
@lhorie
Copy link
Author

lhorie commented Oct 27, 2015

yeah maybe I should clean up the code and put it up in a repo when I get some time. I was mostly just toying w/ this code, but it's grown quite a bit since I posted this gist and it's not so toy-ish anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment