95d913097d
* module/mes/read-0.mes (read-word, read-block-comment): Implement #|. * reader.c (read_word, read_block_comment)[READER]: Likewise. * tests/read.test: Test it. * NEWS: Mention it.
175 lines
7.4 KiB
Scheme
175 lines
7.4 KiB
Scheme
;;; -*-scheme-*-
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
|
;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
|
|
;;;
|
|
;;; This file is part of Mes.
|
|
;;;
|
|
;;; Mes is free software; you can redistribute it and/or modify it
|
|
;;; under the terms of the GNU General Public License as published by
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
;;; your option) any later version.
|
|
;;;
|
|
;;; Mes is distributed in the hope that it will be useful, but
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
;;; GNU General Public License for more details.
|
|
;;;
|
|
;;; You should have received a copy of the GNU General Public License
|
|
;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;;; read-0.mes - bootstrap reader from Scheme. Use
|
|
;;; ./mes --dump < module/mes/read-0.mes > read-0.mo
|
|
;;; to read, garbage collect, and dump this reader; then
|
|
;;; ./mes --load < tests/gc-3.test
|
|
;;; to use this reader to read and run the minimal gc-3.test
|
|
;;; TODO: complete this reader, remove reader from C.
|
|
|
|
;;; Code:
|
|
|
|
(begin
|
|
|
|
;; (define car (make-function 'car 0))
|
|
;; (define cdr (make-function 'cdr 1))
|
|
;; (define cons (make-function 'cons 1))
|
|
|
|
;; TODO:
|
|
;; * use case/cond, expand
|
|
;; * etc int/char?
|
|
;; * lookup in Scheme
|
|
;; * read characters, quote, strings
|
|
|
|
(define (read)
|
|
(read-word (read-byte) (list) (current-module)))
|
|
|
|
(define (read-env a)
|
|
(read-word (read-byte) (list) a))
|
|
|
|
(define (read-input-file)
|
|
(define (helper x)
|
|
(if (null? x) x
|
|
(cons x (helper (read)))))
|
|
(helper (read)))
|
|
|
|
(define-macro (cond . clauses)
|
|
(list 'if (pair? clauses)
|
|
(list 'if (car (car clauses))
|
|
(if (pair? (cdar clauses))
|
|
(if (eq? (cadar clauses) '=>)
|
|
(append2 (cddar clauses) (list (caar clauses)))
|
|
(list (cons 'lambda (cons '() (car clauses)))))
|
|
(list (cons 'lambda (cons '() (car clauses)))))
|
|
(if (pair? (cdr clauses))
|
|
(cons 'cond (cdr clauses))))))
|
|
|
|
(define (eat-whitespace)
|
|
(cond
|
|
((eq? (peek-byte) 9) (read-byte) (eat-whitespace))
|
|
((eq? (peek-byte) 10) (read-byte) (eat-whitespace))
|
|
((eq? (peek-byte) 12) (read-byte) (eat-whitespace))
|
|
((eq? (peek-byte) 13) (read-byte) (eat-whitespace))
|
|
((eq? (peek-byte) 32) (read-byte) (eat-whitespace))
|
|
((eq? (peek-byte) 59) (begin (read-line-comment (read-byte))
|
|
(eat-whitespace)))
|
|
((eq? (peek-byte) 35) (begin (read-byte)
|
|
(cond ((eq? (peek-byte) 33)
|
|
(read-byte)
|
|
(read-block-comment 33 (read-byte))
|
|
(eat-whitespace))
|
|
((eq? (peek-byte) 124)
|
|
(read-byte)
|
|
(read-block-comment 124 (read-byte))
|
|
(eat-whitespace))
|
|
(#t (unread-byte 35)))
|
|
))))
|
|
|
|
(define (read-block-comment s c)
|
|
(if (eq? c s) (if (eq? (peek-byte) 35) (read-byte)
|
|
(read-block-comment s (read-byte)))
|
|
(read-block-comment s (read-byte))))
|
|
|
|
;; (define (read-hex c)
|
|
;; (if (eq? c 10) c
|
|
;; (read-line-comment (read-byte))))
|
|
|
|
(define (read-line-comment c)
|
|
(if (eq? c 10) c
|
|
(read-line-comment (read-byte))))
|
|
|
|
(define (read-list a)
|
|
(eat-whitespace)
|
|
(if (eq? (peek-byte) 41) (begin (read-byte) (list))
|
|
((lambda (w)
|
|
(if (eq? w *dot*) (car (read-list a))
|
|
(cons w (read-list a))))
|
|
(read-word (read-byte) (list) a))))
|
|
|
|
;;(define (read-string))
|
|
|
|
(define (lookup-char c a)
|
|
(lookup (cons (integer->char c) (list)) a))
|
|
|
|
(define (read-word c w a)
|
|
(cond
|
|
((eq? c -1) (list))
|
|
((eq? c 10) (if (null? w) (read-word (read-byte) (list) a)
|
|
(lookup w a)))
|
|
((eq? c 12) (read-word 10 w a))
|
|
((eq? c 32) (read-word 10 w a))
|
|
((eq? c 34) (if (null? w) (read-string)
|
|
(begin (unread-byte c) (lookup w a))))
|
|
((eq? c 35) (cond
|
|
((eq? (peek-byte) 33) (begin (read-byte)
|
|
(read-block-comment 33 (read-byte))
|
|
(read-word (read-byte) w a)))
|
|
((eq? (peek-byte) 124) (begin (read-byte)
|
|
(read-block-comment 124 (read-byte))
|
|
(read-word (read-byte) w a)))
|
|
((eq? (peek-byte) 40) (read-byte) (list->vector (read-list a)))
|
|
((eq? (peek-byte) 92) (read-byte) (read-character))
|
|
((eq? (peek-byte) 120) (read-byte) (read-hex))
|
|
((eq? (peek-byte) 44)
|
|
(read-byte)
|
|
(cond ((eq? (peek-byte) 64)
|
|
(read-byte)
|
|
(cons (lookup (symbol->list (quote unsyntax-splicing)) a)
|
|
(cons (read-word (read-byte) w a) (list))))
|
|
(#t
|
|
(cons (lookup (symbol->list (quote unsyntax)) a)
|
|
(cons (read-word (read-byte) w a) (list))))))
|
|
((eq? (peek-byte) 39) (read-byte)
|
|
(cons (lookup (cons (integer->char 35) (cons (integer->char 39) (list))) a)
|
|
(cons (read-word (read-byte) w a) (list))))
|
|
((eq? (peek-byte) 59) (read-byte)
|
|
(read-word (read-byte) w a)
|
|
(read-word (read-byte) w a))
|
|
((eq? (peek-byte) 96) (read-byte)
|
|
(cons (lookup (cons (integer->char 35) (cons (integer->char 96) (list))) a)
|
|
(cons (read-word (read-byte) w a) (list))))
|
|
(#t (read-word (read-byte) (append2 w (cons (integer->char c) (list))) a))))
|
|
((eq? c 39) (if (null? w) (cons (lookup (cons (integer->char c) (list)) a)
|
|
(cons (read-word (read-byte) w a) (list)))
|
|
(begin (unread-byte c) (lookup w a))))
|
|
((eq? c 40) (if (null? w) (read-list a)
|
|
(begin (unread-byte c) (lookup w a))))
|
|
((eq? c 41) (if (null? w) (cons (lookup (cons (integer->char c) (list)) a)
|
|
(cons (read-word (read-byte) w a) (list)))
|
|
(begin (unread-byte c) (lookup w a))))
|
|
((eq? c 44) (cond
|
|
((eq? (peek-byte) 64) (begin (read-byte)
|
|
(cons
|
|
(lookup (symbol->list (quote unquote-splicing)) a)
|
|
(cons (read-word (read-byte) w a) (list)))))
|
|
(#t (cons (lookup-char c a) (cons (read-word (read-byte) w a)
|
|
(list))))))
|
|
((eq? c 96) (cons (lookup-char c a) (cons (read-word (read-byte) w a) (list))))
|
|
((eq? c 59) (read-line-comment c) (read-word 10 w a))
|
|
(#t (read-word (read-byte) (append2 w (cons (integer->char c) (list))) a))))
|
|
|
|
((lambda (p)
|
|
;;(display (quote scheme-program=)) (display p) (newline)
|
|
(begin-env p (current-module)))
|
|
(read-input-file)))
|