git @ Cat's Eye Technologies Tamsin / master eg / sexpr-eval.tamsin
master

Tree @master (Download .tar.gz)

sexpr-eval.tamsin @masterraw · history · blame

# This example Tamsin program was written by Chris Pressey, and is
# hereby placed in the public domain.  It comes with NO WARRANTY.

main = sexp → S using scanner & reverse(S, nil) → SR & eval(SR).
scanner = scan using $:utf8.
scan = {" "} & ("(" | ")" | (T ← '' & {$:alnum  S & T  T + S} & return T)).
sexp = $:alnum | list.
list = "(" & listtail(nil).
listtail(L) = sexp → S & listtail(pair(S, L)) | ")" & L.
head(pair(A, B)) = return A.
tail(pair(A, B)) = return B.
cons(A, B) = return pair(A, B).
eval(pair(head, pair(X, nil))) = eval(X) → R & head(R).
eval(pair(tail, pair(X, nil))) = eval(X) → R & tail(R).
eval(pair(cons, pair(A, pair(B, nil)))) =
   eval(A) → AE & eval(B) → BE & return pair(AE, BE).
eval(X) = X.
reverse(pair(H, T), A) = reverse(H, nil) → HR & reverse(T, pair(HR, A)).
reverse(nil, A) = A.
reverse(X, A) = X.