Implement load.

* mes.c (symbol_primitive_load): New symbol.
  (builtin_eval): Use it to implement primitive-load.
* module/mes/base-0.mes (push!, pop!): New macro.
  (load): New macro.
* tests/data/load.scm: New file.
* tests/base.test (load): New test.
This commit is contained in:
Jan Nieuwenhuizen 2016-11-02 20:25:08 +01:00
parent 70bdab7090
commit 97300ef6ae
4 changed files with 51 additions and 2 deletions

4
mes.c
View file

@ -112,7 +112,7 @@ scm symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
scm symbol_call_with_values = {SYMBOL, "call-with-values"};
scm symbol_current_module = {SYMBOL, "current-module"};
scm symbol_primitive_load = {SYMBOL, "primitive-load"};
scm char_nul = {CHAR, .name="nul", .value=0};
scm char_backspace = {CHAR, .name="backspace", .value=8};
@ -437,6 +437,8 @@ builtin_eval (scm *e, scm *a)
return define_env (e, a);
if (e->car == &symbol_define_macro)
return define_env (e, a);
if (e->car == &symbol_primitive_load)
return load_env (a);
#else
if (e->car == &symbol_define) {
fprintf (stderr, "C DEFINE: ");

View file

@ -35,7 +35,9 @@
(define-macro (defined? x)
(list 'assq x '(cddr (current-module))))
(define (current-input-port) 0)
(if (defined? 'current-input-port) #t
(define (current-input-port) 0))
(define (current-output-port) 1)
(define (current-error-port) 2)
(define (port-filename port) "<stdin>")
@ -83,3 +85,20 @@
(define-macro (let bindings . rest)
(cons 'simple-let (cons bindings rest)))
(define *input-ports* '())
(define-macro (push! stack o)
`(begin
(set! ,stack (cons ,o ,stack))
,stack))
(define-macro (pop! stack)
`(let ((o (car ,stack)))
(set! ,stack (cdr ,stack))
o))
(define-macro (load file)
`(primitive-eval
(begin
(push! *input-ports* (current-input-port))
(set-current-input-port (open-input-file ,file))
(primitive-load)
(set-current-input-port (pop! *input-ports*)))))

View file

@ -70,4 +70,11 @@ exit $?
(pass-if "apply 2" (sequal? (apply list 1 '(2)) '(1 2)))
(pass-if "apply 3" (sequal? (apply list 1 2 '(3)) '(1 2 3)))
(begin
(define local-answer 41))
(pass-if-equal "begin 2" 41 (begin local-answer))
(if (not guile?)
(pass-if-equal "load" 42 (begin (load "tests/data/load.scm") the-answer)))
(result 'report)

21
tests/data/load.scm Normal file
View file

@ -0,0 +1,21 @@
;;; -*-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/>.
(define the-answer 42)