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:
parent
70bdab7090
commit
97300ef6ae
4
mes.c
4
mes.c
|
@ -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: ");
|
||||
|
|
|
@ -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*)))))
|
||||
|
|
|
@ -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
21
tests/data/load.scm
Normal 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)
|
Loading…
Reference in a new issue