2016-12-12 14:41:48 +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)
|
|
|
|
(cond
|
|
|
|
((null? x) a)
|
|
|
|
((atom? x) (cons (cons x y) a))
|
|
|
|
(#t (cons (cons (car x) (car y))
|
|
|
|
(pairlis (cdr x) (cdr y) a)))))
|
|
|
|
|
|
|
|
(define (assq x a)
|
|
|
|
(cond
|
|
|
|
((null? a) #f)
|
|
|
|
((eq? (caar a) x) (car a))
|
|
|
|
(#t (assq x (cdr a)))))
|
|
|
|
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
(define (assq-ref-env x a)
|
2016-12-12 19:07:17 +00:00
|
|
|
(let ((e (assq x a)))
|
|
|
|
(if (eq? e #f) '*undefined* (cdr e))))
|
|
|
|
|
2016-12-12 14:41:48 +00:00
|
|
|
;; Page 13
|
|
|
|
(define (evcon c a)
|
|
|
|
(cond
|
|
|
|
((null? c) *unspecified*)
|
|
|
|
;; single-statement cond
|
|
|
|
;; ((eval (caar c) a) (eval (cadar c) a))
|
|
|
|
((eval (caar c) a)
|
|
|
|
(cond ((null? (cddar c)) (eval (cadar c) a))
|
|
|
|
(#t (eval (cadar c) a)
|
|
|
|
(evcon
|
|
|
|
(cons (cons #t (cddar c)) '())
|
|
|
|
a))))
|
|
|
|
(#t (evcon (cdr c) a))))
|
|
|
|
|
2016-12-12 19:07:17 +00:00
|
|
|
(define (evlis-env m a)
|
2016-12-12 14:41:48 +00:00
|
|
|
(cond
|
|
|
|
((null? m) '())
|
2016-12-12 19:07:17 +00:00
|
|
|
((not (pair? m)) (eval-env m a))
|
|
|
|
(#t (cons (eval-env (car m) a) (evlis-env (cdr m) a)))))
|
2016-12-12 14:41:48 +00:00
|
|
|
|
|
|
|
(define (apply-env fn x a)
|
|
|
|
(cond
|
|
|
|
((atom? fn)
|
|
|
|
(cond
|
2016-12-12 19:07:17 +00:00
|
|
|
((builtin? fn) (call fn x))
|
|
|
|
((eq? fn 'call-with-values) (call call-with-values-env (append x (cons a '()))))
|
|
|
|
((eq? fn 'current-module) a)))
|
2016-12-12 14:41:48 +00:00
|
|
|
((eq? (car fn) 'lambda)
|
2016-12-12 19:07:17 +00:00
|
|
|
(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 (eval-expand e a)
|
2016-12-12 14:41:48 +00:00
|
|
|
(cond
|
2016-12-12 19:07:17 +00:00
|
|
|
((eq? e '*undefined*) e)
|
core+mini-mes: Replace manual snippets by snarfed includes.
* build-aux/mes-snarf.scm (symbol->source, function->header,
function->source, function->environment): Add workarounds to
avoid struct-copy initializers.
* GNUmakefile (mini-mes): Snarf symbols and functions.
* scaffold/mini-mes.c: Include mini-mes.h, mini-mes.symbols.h,
mini-mes.symbols.i, mini-mes.i, mini-mes.environment.i.
Add snarfable symbol/special definitions.
(type_t): Prefix all types with `T', update users.
(assert_defined, gc_push_frame, gc_peek_frame, gc_init_cells): Mark
as internal.
* mes.c (type_t): Prefix all types with `T', update users.
* scaffold/mini-mes.c (eq_p, type_, car_, cdr_,
list_of_char_equal_p, lookup_macro, write_byte): New functions (from
mes.c).
(assq): Add debugging, workaround.
2017-03-10 19:56:18 +00:00
|
|
|
((symbol? e) (assq-ref-env e a))
|
2016-12-12 19:07:17 +00:00
|
|
|
((atom? e) e)
|
2016-12-12 14:41:48 +00:00
|
|
|
((atom? (car e))
|
|
|
|
(cond
|
|
|
|
((eq? (car e) 'quote) (cadr e))
|
2016-12-12 19:07:17 +00:00
|
|
|
((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)
|
2016-12-22 15:34:28 +00:00
|
|
|
(eval-expand (macro-expand-env e a) a))
|
2016-12-12 19:07:17 +00:00
|
|
|
|
2016-12-22 15:34:28 +00:00
|
|
|
(define (macro-expand-env e a)
|
2016-12-12 19:07:17 +00:00
|
|
|
(if (pair? e) ((lambda (macro)
|
2016-12-22 15:34:28 +00:00
|
|
|
(if macro (macro-expand-env (apply-env macro (cdr e) a) a)
|
2016-12-12 19:07:17 +00:00
|
|
|
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))))
|
2016-12-12 14:41:48 +00:00
|
|
|
|
|
|
|
(define (eval-quasiquote e a)
|
|
|
|
(cond ((null? e) e)
|
|
|
|
((atom? e) e)
|
2016-12-12 19:07:17 +00:00
|
|
|
((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 (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 (env:define a+ a)
|
|
|
|
(set-cdr! a+ (cdr a))
|
|
|
|
(set-cdr! a a+)
|
|
|
|
(set-cdr! (assq '*closure* a) a))
|
|
|
|
|
|
|
|
(define (env:macro name+entry)
|
|
|
|
(cons
|
|
|
|
(cons (car name+entry)
|
|
|
|
(make-macro (car name+entry)
|
|
|
|
(cdr name+entry)))
|
|
|
|
'()))
|