;; BF Lexer in Scheme ;; Jon Simons (simonsj at ccs dot neu dot edu) ;; August, 2007 ;; ------------------------------------------------------------------------- (module bf-lex mzscheme (require scheme/list) (provide lex) ;; Is the given character a valid BF token? ;; ;; valid-token? : character -> boolean (define (valid-token? c) (ormap (lambda (x) (char=? c x)) (list #\- #\+ #\> #\< #\[ #\] #\, #\.))) ;; Lexes a BF source file, returning a list of character tokens. ;; ;; TODO: Some kind of token-mapping so that we can parse Ook! ;; and feckfeck dialects? ;; Make my own dialect! ;; ;; lex : string -> (list of characters) (define (lex input) (letrec ((read-to-list (lambda (lst) (let ((c (read-char))) (if (eof-object? c) lst (if (valid-token? c) (read-to-list (cons c lst)) (read-to-list lst)))))) (get-input-vector (lambda () (reverse (read-to-list ()))))) (with-input-from-file input get-input-vector))) )