Update Mes in Guile.

* guile/mes.scm: Update for guile-2.2.
* guile/mes.mes: Update from module/mes/mes-0.mes.
* guile/reader.mes: Update from module/mes/read-0.mes.
This commit is contained in:
Jan Nieuwenhuizen 2016-12-12 20:07:17 +01:00
parent ddca30271e
commit 661954a352
5 changed files with 349 additions and 242 deletions

View file

@ -46,6 +46,10 @@
((eq? (caar a) x) (car a))
(#t (assq x (cdr a)))))
(define (assq-ref-cache x a)
(let ((e (assq x a)))
(if (eq? e #f) '*undefined* (cdr e))))
;; Page 13
(define (evcon c a)
(cond
@ -60,131 +64,106 @@
a))))
(#t (evcon (cdr c) a))))
(define (evlis m a)
(define (evlis-env m a)
(cond
((null? m) '())
(#t (cons (eval (car m) a) (evlis (cdr m) a)))))
((not (pair? m)) (eval-env m a))
(#t (cons (eval-env (car m) a) (evlis-env (cdr m) a)))))
(define (apply-env fn x a)
(cond
((atom? fn)
(cond
((eq? fn 'current-module)
(c:apply-env current-module '() a))
((eq? fn 'call-with-values)
(c:apply-env 'call-with-values x a))
((builtin? fn)
(call fn x))
(#t (apply-env (eval fn a) x a))))
((builtin? fn) (call fn x))
((eq? fn 'call-with-values) (call call-with-values-env (append x (cons a '()))))
((eq? fn 'current-module) a)))
((eq? (car fn) 'lambda)
(begin-env (cddr fn) (pairlis (cadr fn) x a)))
((eq? (car fn) 'label) (apply-env (caddr fn) x (cons (cons (cadr fn)
(caddr fn)) a)))))
(let ((p (pairlis (cadr fn) x a)))
(eval-begin-env (cddr fn) (cons (cons '*closure* p) p))))
((eq? (car fn) '*closure*)
(let ((args (caddr fn))
(body (cdddr fn))
(a (cddr (cadr fn))))
(let ((p (pairlis args x a)))
(eval-begin-env body (cons (cons '*closure* p) p)))))
;;((eq? (car fn) 'label) (apply-env (caddr fn) x (cons (cons (cadr fn) (caddr fn)) a)))
(#t (apply-env (eval-env fn a) x a))))
(define (begin-env body a)
(cond ((null? body) *unspecified*)
((null? (cdr body)) (eval (car body) a))
(#t (eval (car body) a)
(begin-env (cdr body) a))))
(define (set-env! x e a)
(set-cdr! (assq x a) e))
(define (eval e a)
(define (eval-expand e a)
(cond
((eq? e #t) #t)
((eq? e #f) #f)
((char? e) e)
((number? e) e)
((string? e) e)
((vector? e) e)
((atom? e) (cdr (assq e a)))
((builtin? e) e)
((eq? e '*undefined*) e)
((symbol? e) (assq-ref-cache e a))
((atom? e) e)
((atom? (car e))
(cond
((eq? (car e) 'quote) (cadr e))
((eq? (car e) 'begin) (begin-env (cdr e) a))
((eq? (car e) 'lambda) e)
((eq? (car e) 'set!) (set-env! (cadr e) (caddr e) a))
((eq? (car e) 'unquote) (eval (cadr e) a))
((eq? (car e) 'quasiquote) (eval-quasiquote (cadr e) a))
((eq? (car e) 'cond) (evcon (cdr e) a))
((pair? (assq (car e) (cdr (assq '*macro* a))))
(c:eval
(c:apply-env
(cdr (assq (car e) (cdr (assq '*macro* a))))
(cdr e)
a)
a))
(#t (apply-env (car e) (evlis (cdr e) a) a))))
(#t (apply-env (car e) (evlis (cdr e) a) a))))
((eq? (car e) 'syntax) (cadr e))
((eq? (car e) 'begin) (eval-begin-env e a))
((eq? (car e) 'lambda) (make-closure (cadr e) (cddr e) (assq '*closure* a)))
((eq? (car e) '*closure*) e)
((eq? (car e) 'if) (eval-if-env (cdr e) a))
((eq? (car e) 'define) (env:define (cons (sexp:define e a) '()) a))
((eq? (car e) 'define-macro) (env:define (env:macro (sexp:define e a)) a))
((eq? (car e) 'set!) (set-env! (cadr e) (eval-env (caddr e) a) a))
((eq? (car e) 'apply-env) (apply-env (eval-env (cadr e) a) (evlis-env (caddr e) a) a))
((eq? (car e) 'unquote) (eval-env (cadr e) a))
((eq? (car e) 'quasiquote) (eval-quasiquote (cadr e) (add-unquoters a)))
(#t (apply-env (eval-env (car e) a) (evlis-env (cdr e) a) a))))
(#t (apply-env (eval-env (car e) a) (evlis-env (cdr e) a) a))))
(define (unquote x) (cons 'unquote x))
(define (unquote-splicing x) (cons 'quasiquote x))
(define %the-unquoters
(cons
(cons 'unquote unquote)
(cons (cons 'unquote-splicing unquote-splicing) '())))
(define (add-unquoters a)
(cons %the-unquoters a))
(define (eval-env e a)
(eval-expand (expand-macro-env e a) a))
(define (expand-macro-env e a)
(if (pair? e) ((lambda (macro)
(if macro (expand-macro-env (apply-env macro (cdr e) a) a)
e))
(lookup-macro (car e) a))
e))
(define (eval-begin-env e a)
(if (null? e) *unspecified*
(if (null? (cdr e)) (eval-env (car e) a)
(begin
(eval-env (car e) a)
(eval-begin-env (cdr e) a)))))
(define (eval-if-env e a)
(if (eval-env (car e) a) (eval-env (cadr e) a)
(if (pair? (cddr e)) (eval-env (caddr e) a))))
(define (eval-quasiquote e a)
(cond ((null? e) e)
((atom? e) e)
((atom? (car e)) (cons (car e) (eval-quasiquote (cdr e) a)))
((eq? (caar e) 'unquote) (cons (eval (cadar e) a) '()))
((eq? (caar e) 'quote) (cons (cadar e) '()))
((eq? (caar e) 'quasiquote) (cons (cadar e) '()))
(#t (cons (car e) (eval-quasiquote (cdr e) a)))))
((eq? (car e) 'unquote) (eval-env (cadr e) a))
((and (pair? (car e))
(eq? (caar e) 'unquote-splicing))
(append2 (eval-env (cadar e) a) (eval-quasiquote (cdr e) a)))
(#t (cons (eval-quasiquote (car e) a) (eval-quasiquote (cdr e) a)))))
(define (readenv a)
(readword (read-byte) '() a))
(define (sexp:define e a)
(if (atom? (cadr e)) (cons (cadr e) (eval-env (caddr e) a))
(cons (caadr e) (eval-env (cons 'lambda (cons (cdadr e) (cddr e))) a))))
(define (readword c w a)
(cond ((eq? c -1) ;; eof
(cond ((eq? w '()) '())
(#t (lookup w a))))
((eq? c 10) ;; \n
(cond ((eq? w '()) (readword (read-byte) w a))
;; DOT ((eq? w '(*dot*)) (car (readword (read-byte) '() a)))
(#t (lookup w a))))
((eq? c 32) ;; \space
(readword 10 w a))
((eq? c 40) ;; (
(cond ((eq? w '()) (readlist a))
(#t (unread-byte c) (lookup w a))))
((eq? c 41) ;; )
(cond ((eq? w '()) (unread-byte c) w)
(#t (unread-byte c) (lookup w a))))
((eq? c 39) ;; '
(cond ((eq? w '())
(cons (lookup (cons c '()) a)
(cons (readword (read-byte) w a) '())))
(#t (unread-byte c) (lookup w a))))
((eq? c 59) ;; ;
(readcomment c)
(readword 10 w a))
((eq? c 35) ;; #
(cond ((eq? (peek-byte) 33) ;; !
(read-byte)
(readblock (read-byte))
(readword 10 w a))
;; TODO: char, vector
(#t (readword (read-byte) (append2 w (cons c '())) a))))
(#t (readword (read-byte) (append2 w (cons c '())) a))))
(define (env:define a+ a)
(set-cdr! a+ (cdr a))
(set-cdr! a a+)
(set-cdr! (assq '*closure* a) a))
(define (readblock c)
(cond ((eq? c 33) (cond ((eq? (peek-byte) 35) (read-byte))
(#t (readblock (read-byte)))))
(#t (readblock (read-byte)))))
(define (eat-whitespace)
(cond ((eq? (peek-byte) 10) (read-byte) (eat-whitespace))
((eq? (peek-byte) 32) (read-byte) (eat-whitespace))
((eq? (peek-byte) 35) (read-byte) (eat-whitespace))
(#t #t)))
(define (readlist a)
(eat-whitespace)
(cond ((eq? (peek-byte) 41) ;; )
(read-byte)
'())
;; TODO *dot*
(#t (cons (readword (read-byte) '() a) (readlist a)))))
(define (readcomment c)
(cond ((eq? c 10) ;; \n
c)
(#t (readcomment (read-byte)))))
(define (env:macro name+entry)
(cons
(cons (car name+entry)
(make-macro (car name+entry)
(cdr name+entry)))
'()))

View file

@ -27,23 +27,23 @@ exec guile -L $(pwd) -e '(mes)' -s "$0" "$@"
(define-module (mes)
#:export (main))
(set-current-module
(make-module 10 `(,(resolve-interface
(let ((guile (resolve-interface
'(guile)
#:select '(
#:select `(
;; Debugging
apply
cons*
current-error-port
current-output-port
current-module
display
eof-object?
eval
exit
force-output
format
list
map
newline
read
with-input-from-string
;; Guile admin
module-define!
@ -56,12 +56,11 @@ exec guile -L $(pwd) -e '(mes)' -s "$0" "$@"
eq?
null?
pair?
*unspecified*
;; READER
char->integer
integer->char
read-char
unread-char
;; non-primitive BUILTINS
char?
@ -71,7 +70,23 @@ exec guile -L $(pwd) -e '(mes)' -s "$0" "$@"
<
-
)
#:renamer (symbol-prefix-proc 'guile:)))))
#:renamer (symbol-prefix-proc 'guile:)))
(guile-2.0 (resolve-interface '(guile) #:select '(define)))
(guile-2.2 (resolve-interface '(guile) #:select '(quasiquote unquote)))
(ports (resolve-interface
(if (equal? (effective-version) "2.0")'(guile) '(ice-9 ports))
#:select '(
;; Debugging
current-error-port
current-output-port
;; READER
;;peek-char
read-char
unread-char)
#:renamer (symbol-prefix-proc 'guile:))))
(set-current-module
(make-module 10 `(,guile ,guile-2.0 ,guile-2.2 ,ports))))
(define (logf port string . rest)
(guile:apply guile:format (guile:cons* port string rest))
@ -85,7 +100,7 @@ exec guile -L $(pwd) -e '(mes)' -s "$0" "$@"
(guile:apply logf (guile:cons* (guile:current-output-port) string rest)))
(define (debug . x) #t)
;;(define debug stderr)
(define debug stderr)
;; TODO
(define (atom? x)
@ -108,8 +123,9 @@ exec guile -L $(pwd) -e '(mes)' -s "$0" "$@"
(define call guile:apply)
(define (peek-byte)
(unread-byte (read-byte)))
;;(define peek-byte guile:peek-char)
(define (read-byte)
(guile:char->integer (guile:read-char)))
(char->integer (guile:read-char)))
(define (unread-byte x)
(guile:unread-char (guile:integer->char x))
x)
@ -118,113 +134,91 @@ exec guile -L $(pwd) -e '(mes)' -s "$0" "$@"
(stderr "lookup x=~a\n" x)
x)
(define (char->integer c)
(if (guile:eof-object? c) -1 (guile:char->integer c)))
(include "mes.mes")
;; guile-2.2 only, guile-2.0 has no include?
(include "reader.mes")
(define (append2 x y)
(cond ((null? x) y)
(#t (cons (car x) (append2 (cdr x) y)))))
(define (eval-environment e a)
(eval e (append2 a environment)))
(define (apply-environment fn e a)
(apply-env fn e (append2 a environment)))
;; READER: TODO lookup
(define (readenv a)
(define (read)
(let ((x (guile:read)))
(if (guile:eof-object? x) '()
x)))
(define (lookup-macro e a)
#f)
(define environment
`(
(() . ())
(guile:map
(lambda (x) (cons (car x) (guile:eval (cdr x) (guile:current-module))))
'(
((guile:list) . (guile:list))
(#t . #t)
(#f . #f)
(*unspecified* . ,*unspecified*)
(*unspecified* . guile:*unspecified*)
(atom? . ,atom?)
(car . ,car)
(cdr . ,cdr)
(cons . ,cons)
(cond . ,evcon)
(eq? . ,eq?)
(atom? . atom?)
(car . car)
(cdr . cdr)
(cons . cons)
;; (cond . evcon)
(eq? . eq?)
(null? . ,null?)
(pair? . ,guile:pair?)
;;(quote . ,quote)
(null? . null?)
(pair? . guile:pair?)
;;(quote . quote)
(evlis . ,evlis)
(evcon . ,evcon)
(pairlis . ,pairlis)
(assq . ,assq)
(evlis-env . evlis-env)
(evcon . evcon)
(pairlis . pairlis)
(assq . assq)
(assq-ref-cache . assq-ref-cache)
(eval . ,eval-environment)
(apply-env . ,apply-environment)
(eval-env . eval-env)
(apply-env . apply-env)
(readenv . ,readenv)
(display . ,guile:display)
(newline . ,guile:newline)
(read . read)
(display . guile:display)
(newline . guile:newline)
(builtin? . ,builtin?)
(number? . ,number?)
(call . ,call)
(builtin? . builtin?)
(number? . number?)
(call . call)
(< . ,guile:<)
(- . ,guile:-)
(< . guile:<)
(- . guile:-)
;; DERIVED
(caar . ,caar)
(cadr . ,cadr)
(cdar . ,cdar)
(cddr . ,cddr)
(caadr . ,caadr)
(caddr . ,caddr)
(cdadr . ,cdadr)
(cadar . ,cadar)
(cddar . ,cddar)
(cdddr . ,cdddr)
(caar . caar)
(cadr . cadr)
(cdar . cdar)
(cddr . cddr)
(caadr . caadr)
(caddr . caddr)
(cdadr . cdadr)
(cadar . cadar)
(cddar . cddar)
(cdddr . cdddr)
(append2 . ,append2)
(exit . ,guile:exit)
(append2 . append2)
(exit . guile:exit)
(*macro* . ())
(*macro* . (guile:list))
;;
(stderr . ,stderr)))
(define (mes-define-lambda x a)
(cons (caadr x) (cons 'lambda (cons (cdadr x) (cddr x)))))
(define (mes-define x a)
(if (atom? (cadr x))
(cons (cadr x) (eval (caddr x) a))
(mes-define-lambda x a)))
(define (mes-define-macro x a)
(cons '*macro*
(cons (mes-define-lambda x a)
(cdr (assq '*macro* a)))))
(define (loop r e a)
(cond ((null? e) r)
((eq? e 'exit)
(apply-env (cdr (assq 'loop a))
(cons *unspecified* (cons #t (cons a '())))
a))
((atom? e) (loop (eval e a) (readenv a) a))
((eq? (car e) 'define)
(loop *unspecified* (readenv a) (cons (mes-define e a) a)))
((eq? (car e) 'define-macro)
(loop *unspecified* (readenv a) (cons (mes-define-macro e a) a)))
(#t (loop (eval e a) (readenv a) a))))
(stderr . stderr))))
(define (main arguments)
(let ((a (append2 environment `((*a* . ,environment)))))
;;(guile:display (eval (readenv a) a))
(guile:display (loop *unspecified* (readenv a) a))
)
(let ((program (read-input-file)))
;;(stderr "program:~a\n" program)
(guile:display (eval-env program environment)))
(guile:newline))
(guile:module-define! (guile:resolve-interface '(mes)) 'main main)

141
guile/reader.mes Normal file
View file

@ -0,0 +1,141 @@
;;; -*-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:
;;; copy of mes/read-0.mes, comment-out read-input-file
;;; 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) '() (current-module)))
(define (read-input-file)
(define (helper x)
(if (null? x) x
(cons x (helper (read)))))
(helper (read)))
(define-macro (cond . clauses)
(list 'if (null? clauses) *unspecified*
(if (null? (cdr clauses))
(list 'if (car (car clauses))
(list (cons 'lambda (cons '() (cons (car (car clauses)) (cdr (car clauses))))))
*unspecified*)
(if (eq? (car (cadr clauses)) 'else)
(list 'if (car (car clauses))
(list (cons 'lambda (cons '() (car clauses))))
(list (cons 'lambda (cons '() (cons *unspecified* (cdr (cadr clauses)))))))
(list 'if (car (car clauses))
(list (cons 'lambda (cons '() (car 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) 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)
(if (eq? (peek-byte) 33) (begin (read-byte)
(read-block-comment (read-byte))
(eat-whitespace))
(unread-byte 35))))))
(define (read-block-comment c)
(if (eq? c 33) (if (eq? (peek-byte) 35) (read-byte)
(read-block-comment (read-byte)))
(read-block-comment (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) '())
((lambda (w)
(if (eq? w '.) (car (read-list a))
(cons w (read-list a))))
(read-word (read-byte) '() a))))
;;(define (read-string))
(define (lookup-char c a)
(lookup (cons (integer->char c) '()) a))
(define (read-word c w a)
(cond
((eq? c -1) '())
((eq? c 10) (if (null? w) (read-word (read-byte) '() a)
(lookup 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 (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))
(else (read-word (read-byte) (append2 w (cons (integer->char c) '())) a))))
((eq? c 39) (if (null? w) (cons (lookup (cons (integer->char c) '()) a)
(cons (read-word (read-byte) w a) '()))
(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) '()) a)
(cons (read-word (read-byte) w a) '()))
(begin (unread-byte c) (lookup w a))))
((eq? c 44) (cond
((eq? (peek-byte) 64) (begin (read-byte)
(cons
(lookup (symbol->list 'unquote-splicing) a)
(cons (read-word (read-byte) w a) '()))))
(else (cons (lookup-char c a) (cons (read-word (read-byte) w a)
'())))))
((eq? c 96) (cons (lookup-char c a) (cons (read-word (read-byte) w a) '())))
((eq? c 59) (read-line-comment c) (read-word 10 w a))
(else (read-word (read-byte) (append2 w (cons (integer->char c) '())) a))))
;; ((lambda (p)
;; ;;(display 'program=) (display p) (newline)
;; (begin-env p (current-module)))
;; (read-input-file))
)

View file

@ -92,19 +92,13 @@
(#t (apply-env (eval-env fn a) x a))))
((eq? (car fn) 'lambda)
(let ((p (pairlis (cadr fn) x a)))
(cache-invalidate-range p (cdr a))
(let ((r (eval-begin-env (cddr fn) (cons (cons '*closure* p) p))))
(cache-invalidate-range p (cdr a))
r)))
(eval-begin-env (cddr fn) (cons (cons '*closure* p) p))))
((eq? (car fn) '*closure*)
(let ((args (caddr fn))
(body (cdddr fn))
(a (cddr (cadr fn))))
(let ((p (pairlis args x a)))
(cache-invalidate-range p (cdr a))
(let ((r (eval-begin-env body (cons (cons '*closure* p) p))))
(cache-invalidate-range p (cdr a))
r))))
(eval-begin-env body (cons (cons '*closure* p) p)))))
;;((eq? (car fn) 'label) (apply-env (caddr fn) x (cons (cons (cadr fn) (caddr fn)) a)))
(#t (apply-env (eval-env fn a) x a))))
@ -187,5 +181,4 @@
'()))
;; boot into loop-0
(cache-invalidate-range (current-module) '())
()

View file

@ -122,7 +122,7 @@
(else (read-word (read-byte) (append w (cons (integer->char c) '())) a))))
((eq? c 39) (if (null? w) (cons (lookup (cons (integer->char c) '()) a)
(cons (read-word (read-byte) w a) '()))
(begin (unread-byte c)) (lookup w a)))
(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) '()) a)