Skip to content

Instantly share code, notes, and snippets.

@t-sin
Last active June 24, 2018 00:26
Show Gist options
  • Save t-sin/1548eb14f9dcd8dd8c82fa7e8167dedd to your computer and use it in GitHub Desktop.
Save t-sin/1548eb14f9dcd8dd8c82fa7e8167dedd to your computer and use it in GitHub Desktop.
//// Otter Lisp
// string utils
strlen
("", n) = n,
(c:xs, n) = strlen(xs, n+1)
strLen s = strlen(s, 0)
strCat
[] = "",
x:xs = x + strCat(xs)
strrev
("", s) = s,
(c:xs, s) = strrev(xs, c:s)
strRev s = strCat(strrev(s, []))
x = strRev("hogehoge")
// input streams
// stream := [str, len, pos]
makeStream s = [s, strLen(s), 0]
streamStr str:len:pos:[] = str
streamLen str:len:pos:[] = len
streamPos str:len:pos:[] = pos
doInRange (s, fn)
| streamPos(s) >= streamLen(s) = false
| true = fn(s)
streamAt s = doInRange(s, [(str:len:pos:[]) = str[pos]])
streamNext s = doInRange(s, [(str:len:pos:[]) = [str, len, pos+1]])
runWhile (pred, fn, state, s)
| streamAt(s) == false = [false, state, s]
| pred(streamAt(s)) = [true, state, streamNext(s)]
| true = runWhile(pred, fn, fn(s, state), streamNext(s))
// stacks
// stack := [...]
stackPop x:xs = [x, xs]
stackPush (o, s) = o:s
readstring s = runWhile([c = c == "\""], [(s,state) = streamAt(s):state], [], s)
readString s =
[false:_:s:[] = false, // EOF
true:state:s:[] = [strCat(state), s]](readstring(s))
readParen s = "????"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment