More elegant tokenizer

This commit is contained in:
2025-10-23 16:36:18 +02:00
parent 83a5208600
commit 9d0016561a

View File

@@ -1,28 +1,24 @@
(import (rnrs)) (import (rnrs))
(define get-word (define get-word
(lambda (file word) (lambda (file)
(let ([c (lookahead-char file)]) (let ([c (lookahead-char file)])
(if (char-alphabetic? c) (cond
(begin [(char-alphabetic? c)
(get-char file) (get-char file)
(get-word file (string-append word (string c)))) (string-append (string c) (get-word file))]
word)))) [else ""]))))
(define get-words (define get-words
(lambda (file) (lambda (file)
(letrec ([loop
(lambda (file words)
(let ([c (lookahead-char file)]) (let ([c (lookahead-char file)])
(cond (cond
[(eof-object? c) words] [(eof-object? c) '()]
[(char-whitespace? c) [(char-whitespace? c)
(get-char file) (get-char file)
(loop file words)] (get-words file)]
[(char-alphabetic? c) [(char-alphabetic? c)
(loop file (cons (get-word file "") words))] (cons (get-word file) (get-words file))]))))
[else (error 'get-words "wtf" c)])))])
(reverse (loop file '())))))
(define tokenize (define tokenize
(lambda (filename) (lambda (filename)