454851008f90611a4752350cae37fc19d4a3bb6b
[mes.git] / module / mes / base-0.mes
1 ;;; -*-scheme-*-
2
3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; base-0.mes: This file is part of Mes.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
21 ;;; Commentary:
22
23 ;;; base-0.mes is the first file being loaded from the Mes core.  It
24 ;;; provides primitives that use Mes internals to create the illusion
25 ;;; of compatibility with Guile.  It is not safe to be run by Guile.
26
27 ;;; Code:
28
29 (define (apply f x) (apply-env f x (current-module)))
30 (define (primitive-eval e) (eval e (current-module)))
31 (define (expand-macro e) (expand-macro-env e (current-module)))
32
33 (define quotient /)
34
35 (define-macro (defined? x)
36   (list 'assq x '(cddr (current-module))))
37
38 (define (current-input-port) 0)
39 (define (current-output-port) 1)
40 (define (current-error-port) 2)
41 (define (port-filename port) "<stdin>")
42 (define (port-line port) 0)
43 (define (port-column port) 0)
44 (define (ftell port) 0)
45 (define (false-if-exception x) x)
46
47 (define (cons* x . rest)
48   (define (loop rest)
49     (if (null? (cdr rest)) (car rest)
50         (cons (car rest) (loop (cdr rest)))))
51   (loop (cons x rest)))
52
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))))))
58                  *unspecified*)
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)))))))
66
67 (define else #t)
68
69 (define (map f l . r)
70   (if (null? l) '()
71       (if (null? r) (cons (f (car l)) (map f (cdr l)))
72           (if (null? (cdr r))
73               (cons (f (car l) (caar r)) (map f (cdr l) (cdar r)))))))
74
75 (define-macro (simple-let bindings . rest)
76   (cons (cons 'lambda (cons (map car bindings) rest))
77         (map cadr bindings)))
78
79 (define-macro (let bindings . rest)
80   (cons 'simple-let (cons bindings rest)))