2016-07-09 11:23:58 +00:00
|
|
|
;;; -*-scheme-*-
|
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
|
|
|
;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
;;;
|
|
|
|
;;; mes.mes: 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/>.
|
|
|
|
|
|
|
|
;; The Maxwell Equations of Software -- John McCarthy page 13
|
|
|
|
;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
|
|
|
|
|
|
|
|
;; (define (caar x) (car (car x)))
|
|
|
|
;; (define (cadr x) (car (cdr x)))
|
|
|
|
;; (define (cdar x) (cdr (car x)))
|
|
|
|
;; (define (cddr x) (cdr (cdr x)))
|
|
|
|
;; (define (caadr x) (car (car (cdr x))))
|
|
|
|
;; (define (caddr x) (car (cdr (cdr x))))
|
|
|
|
;; (define (cddar x) (cdr (cdr (car x))))
|
|
|
|
;; (define (cdadr x) (cdr (car (cdr x))))
|
|
|
|
;; (define (cadar x) (car (cdr (car x))))
|
|
|
|
;; (define (cdddr x) (cdr (cdr (cdr x))))
|
|
|
|
|
|
|
|
;; ;; Page 12
|
|
|
|
;; (define (pairlis x y a)
|
|
|
|
;; ;;(debug "pairlis x=~a y=~a a=~a\n" x y a)
|
|
|
|
;; (cond
|
2016-07-09 21:12:25 +00:00
|
|
|
;; ((null? x) a)
|
|
|
|
;; ((atom? x) (cons (cons x y) a))
|
2016-07-09 11:23:58 +00:00
|
|
|
;; (#t (cons (cons (car x) (car y))
|
|
|
|
;; (pairlis (cdr x) (cdr y) a)))))
|
|
|
|
|
2016-07-11 08:48:25 +00:00
|
|
|
;; (define (assq x a)
|
|
|
|
;; ;;(stderr "assq x=~a\n" x)
|
|
|
|
;; ;;(debug "assq x=~a a=~a\n" x a)
|
2016-07-09 11:23:58 +00:00
|
|
|
;; (cond
|
2016-07-09 21:12:25 +00:00
|
|
|
;; ((null? a) #f)
|
|
|
|
;; ((eq? (caar a) x) (car a))
|
2016-07-11 08:48:25 +00:00
|
|
|
;; (#t (assq x (cdr a)))))
|
2016-07-09 11:23:58 +00:00
|
|
|
|
|
|
|
;; ;; Page 13
|
|
|
|
;; (define (eval-quote fn x)
|
|
|
|
;; ;(debug "eval-quote fn=~a x=~a" fn x)
|
|
|
|
;; (apply fn x '()))
|
|
|
|
|
|
|
|
(define (evcon c a)
|
|
|
|
;;(debug "evcon c=~a a=~a\n" c a)
|
2016-05-15 22:07:44 +00:00
|
|
|
(cond
|
2016-07-09 11:23:58 +00:00
|
|
|
;; single-statement cond
|
|
|
|
;; ((eval (caar c) a) (eval (cadar c) a))
|
|
|
|
((eval (caar c) a)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((null? (cddar c)) (eval (cadar c) a))
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (eval (cadar c) a)
|
|
|
|
(evcon
|
|
|
|
(cons (cons #t (cddar c)) '())
|
|
|
|
a))))
|
|
|
|
(#t (evcon (cdr c) a))))
|
2016-05-15 22:07:44 +00:00
|
|
|
|
2016-07-09 11:23:58 +00:00
|
|
|
(define (evlis m a)
|
|
|
|
;;(debug "evlis m=~a a=~a\n" m a)
|
|
|
|
;; (display 'mes-evlis:)
|
|
|
|
;; (display m)
|
|
|
|
;; (newline)
|
2016-05-15 22:07:44 +00:00
|
|
|
(cond
|
2016-07-09 21:12:25 +00:00
|
|
|
((null? m) '())
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (cons (eval (car m) a) (evlis (cdr m) a)))))
|
2016-05-15 22:07:44 +00:00
|
|
|
|
|
|
|
|
2016-07-09 11:23:58 +00:00
|
|
|
(define (apply fn x a)
|
|
|
|
;; (display 'mes-apply:)
|
|
|
|
;; (newline)
|
|
|
|
;; (display 'fn:)
|
|
|
|
;; (display fn)
|
|
|
|
;; (newline)
|
|
|
|
;; (display 'builtin:)
|
2016-07-09 21:12:25 +00:00
|
|
|
;; (display (builtin? fn))
|
2016-07-09 11:23:58 +00:00
|
|
|
;; (newline)
|
|
|
|
;; (display 'x:)
|
|
|
|
;; (display x)
|
|
|
|
;; (newline)
|
2016-05-15 22:07:44 +00:00
|
|
|
(cond
|
2016-07-09 21:12:25 +00:00
|
|
|
((atom? fn)
|
2016-05-15 22:07:44 +00:00
|
|
|
(cond
|
2016-07-11 17:32:11 +00:00
|
|
|
((eq? fn 'current-module)
|
|
|
|
(c:apply current-module '() a))
|
|
|
|
((eq? fn 'call-with-values)
|
|
|
|
(c:apply 'call-with-values x a))
|
2016-07-09 21:12:25 +00:00
|
|
|
((builtin? fn)
|
2016-07-09 11:23:58 +00:00
|
|
|
(call fn x))
|
2016-05-15 22:07:44 +00:00
|
|
|
(#t (apply (eval fn a) x a))))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? (car fn) 'lambda)
|
2016-07-10 11:45:54 +00:00
|
|
|
(begin-env (cddr fn) (pairlis (cadr fn) x a)))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? (car fn) 'label) (apply (caddr fn) x (cons (cons (cadr fn)
|
2016-05-15 22:07:44 +00:00
|
|
|
(caddr fn)) a)))))
|
|
|
|
|
2016-07-10 08:43:26 +00:00
|
|
|
(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)
|
2016-07-11 08:48:25 +00:00
|
|
|
(set-cdr! (assq x a) e))
|
2016-07-10 08:43:26 +00:00
|
|
|
|
2016-05-15 22:07:44 +00:00
|
|
|
(define (eval e a)
|
2016-07-09 11:23:58 +00:00
|
|
|
;;(debug "eval e=~a a=~a\n" e a)
|
2016-07-09 21:12:25 +00:00
|
|
|
;;(debug "eval (atom? ~a)=~a\n" e (atom? e))
|
2016-07-09 11:23:58 +00:00
|
|
|
;; (display 'mes-eval:)
|
|
|
|
;; (display e)
|
|
|
|
;; (newline)
|
|
|
|
;; (display 'a:)
|
|
|
|
;; (display a)
|
|
|
|
;; (newline)
|
2016-05-15 22:07:44 +00:00
|
|
|
(cond
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? e #t) #t)
|
|
|
|
((eq? e #f) #f)
|
2016-07-10 22:15:28 +00:00
|
|
|
((char? e) e)
|
|
|
|
((number? e) e)
|
2016-07-10 20:43:23 +00:00
|
|
|
((string? e) e)
|
2016-07-11 08:38:02 +00:00
|
|
|
((vector? e) e)
|
2016-07-11 08:48:25 +00:00
|
|
|
((atom? e) (cdr (assq e a)))
|
2016-07-09 21:12:25 +00:00
|
|
|
((builtin? e) e)
|
|
|
|
((atom? (car e))
|
2016-05-15 22:07:44 +00:00
|
|
|
(cond
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? (car e) 'quote) (cadr e))
|
|
|
|
((eq? (car e) 'lambda) e)
|
2016-07-10 08:43:26 +00:00
|
|
|
((eq? (car e) 'set!) (set-env! (cadr e) (caddr e) a))
|
2016-07-09 21:12:25 +00:00
|
|
|
((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))
|
2016-07-11 08:48:25 +00:00
|
|
|
((pair? (assq (car e) (cdr (assq '*macro* a))))
|
2016-07-09 11:23:58 +00:00
|
|
|
(c:eval
|
|
|
|
(c:apply
|
2016-07-11 08:48:25 +00:00
|
|
|
(cdr (assq (car e) (cdr (assq '*macro* a))))
|
2016-07-09 11:23:58 +00:00
|
|
|
(cdr e)
|
|
|
|
a)
|
|
|
|
a))
|
2016-05-15 22:07:44 +00:00
|
|
|
(#t (apply (car e) (evlis (cdr e) a) a))))
|
|
|
|
(#t (apply (car e) (evlis (cdr e) a) a))))
|
|
|
|
|
2016-07-09 12:29:39 +00:00
|
|
|
(define (eval-quasiquote e a)
|
|
|
|
;; (display 'mes-eval-quasiquote:)
|
|
|
|
;; (display e)
|
|
|
|
;; (newline)
|
2016-07-09 21:12:25 +00:00
|
|
|
(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) '()))
|
2016-07-09 12:29:39 +00:00
|
|
|
(#t (cons (car e) (eval-quasiquote (cdr e) a)))))
|
|
|
|
|
2016-07-09 11:23:58 +00:00
|
|
|
;; readenv et al works, but slows down dramatically
|
|
|
|
(define (DISABLED-readenv a)
|
|
|
|
(readword (getchar) '() a))
|
2016-05-15 22:07:44 +00:00
|
|
|
|
2016-07-09 11:23:58 +00:00
|
|
|
(define (readword c w a)
|
2016-07-09 16:56:59 +00:00
|
|
|
;; (display 'mes-readword:)
|
|
|
|
;; (display c)
|
|
|
|
;; (newline)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((eq? c -1) ;; eof
|
|
|
|
(cond ((eq? w '()) '())
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (lookup w a))))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 10) ;; \n
|
|
|
|
(cond ((eq? w '()) (readword (getchar) w a))
|
|
|
|
;; DOT ((eq? w '(*dot*)) (car (readword (getchar) '() a)))
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (lookup w a))))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 32) ;; \space
|
2016-07-09 11:23:58 +00:00
|
|
|
(readword 10 w a))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 40) ;; (
|
2016-07-11 08:38:02 +00:00
|
|
|
(cond ((eq? w '()) (readlist a))
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (ungetchar c) (lookup w a))))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 41) ;; )
|
|
|
|
(cond ((eq? w '()) (ungetchar c) w)
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (ungetchar c) (lookup w a))))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 39) ;; '
|
|
|
|
(cond ((eq? w '())
|
2016-07-09 11:23:58 +00:00
|
|
|
(cons (lookup (cons c '()) a)
|
|
|
|
(cons (readword (getchar) w a) '())))
|
|
|
|
(#t (ungetchar c) (lookup w a))))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 59) ;; ;
|
2016-07-09 16:56:59 +00:00
|
|
|
(readcomment c)
|
2016-07-09 11:23:58 +00:00
|
|
|
(readword 10 w a))
|
2016-07-09 21:12:25 +00:00
|
|
|
((eq? c 35) ;; #
|
|
|
|
(cond ((eq? (peekchar) 33) ;; !
|
2016-07-09 16:56:59 +00:00
|
|
|
(getchar)
|
|
|
|
(readblock (getchar))
|
|
|
|
(readword 10 w a))
|
2016-07-11 08:38:02 +00:00
|
|
|
;; TODO: char, vector
|
2016-07-09 16:56:59 +00:00
|
|
|
(#t (readword (getchar) (append w (cons c '())) a))))
|
2016-07-09 11:23:58 +00:00
|
|
|
(#t (readword (getchar) (append w (cons c '())) a))))
|
|
|
|
|
2016-07-09 16:56:59 +00:00
|
|
|
(define (readblock c)
|
|
|
|
;; (display 'mes-readblock:)
|
|
|
|
;; (display c)
|
|
|
|
;; (newline)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((eq? c 33) (cond ((eq? (peekchar) 35) (getchar))
|
2016-07-09 16:56:59 +00:00
|
|
|
(#t (readblock (getchar)))))
|
|
|
|
(#t (readblock (getchar)))))
|
|
|
|
|
|
|
|
(define (eat-whitespace)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((eq? (peekchar) 10) (getchar) (eat-whitespace))
|
|
|
|
((eq? (peekchar) 32) (getchar) (eat-whitespace))
|
|
|
|
((eq? (peekchar) 35) (getchar) (eat-whitespace))
|
2016-07-09 16:56:59 +00:00
|
|
|
(#t #t)))
|
|
|
|
|
2016-07-11 08:38:02 +00:00
|
|
|
(define (readlist a)
|
|
|
|
;; (display 'mes-readlist:)
|
2016-07-09 16:56:59 +00:00
|
|
|
;; (newline)
|
|
|
|
(eat-whitespace)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((eq? (peekchar) 41) ;; )
|
2016-07-09 11:23:58 +00:00
|
|
|
(getchar)
|
|
|
|
'())
|
|
|
|
;; TODO *dot*
|
2016-07-11 08:38:02 +00:00
|
|
|
(#t (cons (readword (getchar) '() a) (readlist a)))))
|
2016-07-09 11:23:58 +00:00
|
|
|
|
|
|
|
(define (readcomment c)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((eq? c 10) ;; \n
|
2016-07-09 11:23:58 +00:00
|
|
|
c)
|
|
|
|
(#t (readcomment (getchar)))))
|