Skip to content

Instantly share code, notes, and snippets.

@carld
Created July 22, 2017 11:19
Show Gist options
  • Save carld/88e0cdb32f6c0b442e6286010ef8e1f2 to your computer and use it in GitHub Desktop.
Save carld/88e0cdb32f6c0b442e6286010ef8e1f2 to your computer and use it in GitHub Desktop.
parsing lisp in lisp
#lang racket
(define (token)
(let [(c (read-char))]
;(printf "DEBUG: ~s~%" c)
c))
(define (whitespace? ch)
(case ch
((#\space #\tab #\newline #\return) #t)
(else #f)))
(define (char? ch)
(and (>= (char->integer ch) (char->integer #\a))
(<= (char->integer ch) (char->integer #\z))))
(define (lambda? ch) (equal? #\λ ch))
(define (dot? ch) (equal? #\. ch))
(define (lparens? ch) (equal? #\( ch))
(define (rparens? ch) (equal? #\) ch))
(define (intern obj)
(printf "INTERN: ~s~%" obj)
obj)
(define readatom
(lambda (ch)
(cond
((whitespace? ch) (readatom (token)))
((eof-object? ch) null)
((lparens? ch) (readlist (token)))
(else (intern ch)))))
(define readlist
(lambda (ch)
(cond
((rparens? ch) null)
((whitespace? ch) (readlist (token)))
((eof-object? ch) null)
(else (cons (readatom ch) (readlist (token)))))))
(printf "~s~%" (readatom (token)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment