ANSForth/forth_read.rkt
#lang racket

(provide forth_read_no_eof forth_read read read-syntax)

(define (read in)
  (define (get_first_char_in_list)
    (let ((new_char (read-char in)))
      (cond [(eof-object? new_char)
             new_char]
            [(eq? new_char #\newline)
             new_char]
            [(char-whitespace? new_char)
             (get_first_char_in_list)]
            [else
             (list new_char)])))
  (define (iter lst)
    (if (or (eof-object? (peek-char in)) (eq? #\newline (peek-char in)))
        lst
        (let [(new_char (read-char in))]
          (if (char-whitespace? new_char)
              lst
              (iter (append lst (list new_char)))))))
  (let [(first_char (get_first_char_in_list))]
    (if (or (eof-object? first_char) (eq? first_char #\newline))
        first_char
        (let [(lst (iter first_char))]
          (list->string lst)))))

(define (read-syntax src in)
  (read in))

(define (forth_read)
  (read (current-input-port)))

(define (forth_read_no_eof)
  (let [(res (forth_read))]
    (if (eof-object? res)
        (error "Unexpected EOF")
        res)))