add call-with-values, values.

This commit is contained in:
Jan Nieuwenhuizen 2016-07-11 19:32:11 +02:00
parent 2e1e307f13
commit 7f35686b61
5 changed files with 51 additions and 5 deletions

4
TODO
View file

@ -13,7 +13,7 @@ v "string"
v #(v e c t o r) v #(v e c t o r)
#\CHAR #\CHAR
v assq v assq
call-with-values v call-with-values
v char? v char?
v length v length
v list v list
@ -25,7 +25,7 @@ v string
v string-append v string-append
v string? v string?
v symbol? v symbol?
values v values
v vector v vector
v vector->list v vector->list
v vector-length v vector-length

25
mes.c
View file

@ -42,7 +42,7 @@
#define QUOTE_SUGAR 1 #define QUOTE_SUGAR 1
#endif #endif
enum type {CHAR, NUMBER, PAIR, STRING, SYMBOL, VECTOR, enum type {CHAR, NUMBER, PAIR, STRING, SYMBOL, VALUES, VECTOR,
FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn}; FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn};
struct scm_t; struct scm_t;
typedef struct scm_t* (*function0_t) (void); typedef struct scm_t* (*function0_t) (void);
@ -99,12 +99,14 @@ scm scm_macro = {SYMBOL, "*macro*"};
scm scm_symbol_EOF = {SYMBOL, "EOF"}; scm scm_symbol_EOF = {SYMBOL, "EOF"};
scm scm_symbol_EOF2 = {SYMBOL, "EOF2"}; scm scm_symbol_EOF2 = {SYMBOL, "EOF2"};
scm scm_symbol_call_with_values = {SYMBOL, "call-with-values"};
scm scm_symbol_current_module = {SYMBOL, "current-module"}; scm scm_symbol_current_module = {SYMBOL, "current-module"};
scm scm_symbol_define = {SYMBOL, "define"}; scm scm_symbol_define = {SYMBOL, "define"};
scm scm_symbol_define_macro = {SYMBOL, "define-macro"}; scm scm_symbol_define_macro = {SYMBOL, "define-macro"};
scm scm_symbol_eval = {SYMBOL, "eval"}; scm scm_symbol_eval = {SYMBOL, "eval"};
scm scm_symbol_loop2 = {SYMBOL, "loop2"}; scm scm_symbol_loop2 = {SYMBOL, "loop2"};
scm scm_symbol_set_x = {SYMBOL, "set!"}; scm scm_symbol_set_x = {SYMBOL, "set!"};
scm scm_symbol_values = {SYMBOL, "values"};
// PRIMITIVES // PRIMITIVES
@ -277,6 +279,8 @@ apply_ (scm *fn, scm *x, scm *a)
{ {
if (fn == &scm_symbol_current_module) // FIXME if (fn == &scm_symbol_current_module) // FIXME
return a; return a;
if (eq_p (fn, &scm_symbol_call_with_values) == &scm_t)
return call (&scm_call_with_values_env, append (x, cons (a, &scm_nil)));
if (builtin_p (fn) == &scm_t) if (builtin_p (fn) == &scm_t)
return call (fn, x); return call (fn, x);
return apply (eval (fn, a), x, a); return apply (eval (fn, a), x, a);
@ -479,6 +483,8 @@ call (scm *fn, scm *x)
#endif #endif
if (fn->type == FUNCTION0) if (fn->type == FUNCTION0)
return fn->function0 (); return fn->function0 ();
if (x->car->type == VALUES)
x = cons (x->car->cdr->car, &scm_nil);
if (fn->type == FUNCTION1) if (fn->type == FUNCTION1)
return fn->function1 (car (x)); return fn->function1 (car (x));
if (fn->type == FUNCTION2) if (fn->type == FUNCTION2)
@ -608,6 +614,23 @@ vector (scm *x/*...*/) // int
} }
#endif #endif
scm *
values (scm *x/*...*/)
{
scm *v = cons (0, x);
v->type = VALUES;
return v;
}
scm *
call_with_values_env (scm *producer, scm *consumer, scm *a)
{
scm *v = apply_ (producer, &scm_nil, a);
if (v->type == VALUES)
v = v->cdr;
return apply_ (consumer, v, a);
}
scm * scm *
vector_length (scm *x) vector_length (scm *x)
{ {

View file

@ -92,8 +92,10 @@
(cond (cond
((atom? fn) ((atom? fn)
(cond (cond
((eq? fn 'current-module) ;; FIXME ((eq? fn 'current-module)
(c:apply current-module '() a)) (c:apply current-module '() a))
((eq? fn 'call-with-values)
(c:apply 'call-with-values x a))
((builtin? fn) ((builtin? fn)
(call fn x)) (call fn x))
(#t (apply (eval fn a) x a)))) (#t (apply (eval fn a) x a))))

View file

@ -21,6 +21,9 @@
;; The Maxwell Equations of Software -- John McCarthy page 13 ;; The Maxwell Equations of Software -- John McCarthy page 13
;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf ;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
(define (defined? x)
(assq x (current-module)))
(define (list . rest) rest) (define (list . rest) rest)
(define (vector . rest) (list->vector rest)) (define (vector . rest) (list->vector rest))
(define assv assq) (define assv assq)

View file

@ -238,4 +238,22 @@
(display (memq 'd '(a b c))) (display (memq 'd '(a b c)))
(newline) (newline)
(cond ((defined? 'loop2)
(display "mes:values broken after loop2")
(newline))
(#t
(values 0 1)
(display "(values 0 1): ")
(display (values 0 1))
(newline)
(display "call-with-values ==> 6: ")
(display
(call-with-values (lambda () (values 1 2 3))
(lambda (a b c) (+ (+ a b) c))))
(newline)
(display "call-with-values ==> 1: ")
(display ((lambda (x) x) (values 1 2 3)))
(newline)))
'() '()