3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of Mes.
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
23 ;;; read-0.mes - bootstrap reader from Scheme. Use
24 ;;; ./mes --dump < module/mes/read-0.mes > read-0.mo
25 ;;; to read, garbage collect, and dump this reader; then
26 ;;; ./mes --load < tests/gc-3.test
27 ;;; to use this reader to read and run the minimal gc-3.test
28 ;;; TODO: complete this reader, remove reader from C.
34 ;; (define car (make-function 'car 0))
35 ;; (define cdr (make-function 'cdr 1))
36 ;; (define cons (make-function 'cons 1))
39 ;; * use case/cond, expand
42 ;; * read characters, quote, strings
45 (read-word (read-byte) '() (current-module)))
47 (define (read-input-file)
50 (cons x (helper (read)))))
53 (define-macro (cond . clauses)
54 (list 'if (null? clauses) *unspecified*
55 (if (null? (cdr clauses))
56 (list 'if (car (car clauses))
57 (list (cons 'lambda (cons '() (cons (car (car clauses)) (cdr (car clauses))))))
59 (if (eq? (car (cadr clauses)) 'else)
60 (list 'if (car (car clauses))
61 (list (cons 'lambda (cons '() (car clauses))))
62 (list (cons 'lambda (cons '() (cons *unspecified* (cdr (cadr clauses)))))))
63 (list 'if (car (car clauses))
64 (list (cons 'lambda (cons '() (car clauses))))
65 (cons 'cond (cdr clauses)))))))
67 (define (eat-whitespace)
69 ((eq? (peek-byte) 9) (read-byte) (eat-whitespace))
70 ((eq? (peek-byte) 10) (read-byte) (eat-whitespace))
71 ((eq? (peek-byte) 13) (read-byte) (eat-whitespace))
72 ((eq? (peek-byte) 32) (read-byte) (eat-whitespace))
73 ((eq? (peek-byte) 59) (begin (read-line-comment (read-byte))
75 ((eq? (peek-byte) 35) (begin (read-byte)
76 (if (eq? (peek-byte) 33) (begin (read-byte)
77 (read-block-comment (read-byte))
81 (define (read-block-comment c)
82 (if (eq? c 33) (if (eq? (peek-byte) 35) (read-byte)
83 (read-block-comment (read-byte)))
84 (read-block-comment (read-byte))))
86 ;; (define (read-hex c)
88 ;; (read-line-comment (read-byte))))
90 (define (read-line-comment c)
92 (read-line-comment (read-byte))))
96 (if (eq? (peek-byte) 41) (begin (read-byte) '())
98 (if (eq? w '.) (car (read-list a))
99 (cons w (read-list a))))
100 (read-word (read-byte) '() a))))
102 ;;(define (read-string))
104 (define (lookup-char c a)
105 (lookup (cons (integer->char c) '()) a))
107 (define (read-word c w a)
110 ((eq? c 10) (if (null? w) (read-word (read-byte) '() a)
112 ((eq? c 32) (read-word 10 w a))
113 ((eq? c 34) (if (null? w) (read-string)
114 (begin (unread-byte c) (lookup w a))))
116 ((eq? (peek-byte) 33) (begin (read-byte)
117 (read-block-comment (read-byte))
118 (read-word (read-byte) w a)))
119 ((eq? (peek-byte) 40) (read-byte) (list->vector (read-list a)))
120 ((eq? (peek-byte) 92) (read-byte) (read-character))
121 ((eq? (peek-byte) 120) (read-byte) (read-hex))
122 (else (read-word (read-byte) (append w (cons (integer->char c) '())) a))))
123 ((eq? c 39) (if (null? w) (cons (lookup (cons (integer->char c) '()) a)
124 (cons (read-word (read-byte) w a) '()))
125 (begin (unread-byte c)) (lookup w a)))
126 ((eq? c 40) (if (null? w) (read-list a)
127 (begin (unread-byte c) (lookup w a))))
128 ((eq? c 41) (if (null? w) (cons (lookup (cons (integer->char c) '()) a)
129 (cons (read-word (read-byte) w a) '()))
130 (begin (unread-byte c) (lookup w a))))
132 ((eq? (peek-byte) 64) (begin (read-byte)
134 (lookup (symbol->list 'unquote-splicing) a)
135 (cons (read-word (read-byte) w a) '()))))
136 (else (cons (lookup-char c a) (cons (read-word (read-byte) w a)
138 ((eq? c 96) (cons (lookup-char c a) (cons (read-word (read-byte) w a) '())))
139 ((eq? c 59) (read-line-comment c) (read-word 10 w a))
140 (else (read-word (read-byte) (append w (cons (integer->char c) '())) a))))
143 ;;(display 'program=) (display p) (newline)
144 (begin-env p (current-module)))