2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/pair.h"
|
2014-09-15 02:38:38 -04:00
|
|
|
#include "picrin/proc.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
static pic_value
|
|
|
|
var_lookup(pic_state *pic, pic_value var)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-01-17 07:01:13 -05:00
|
|
|
pic_value val, env, binding;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
val = pic_ref(pic, pic->PICRIN_BASE, "current-dynamic-environment");
|
|
|
|
if (pic_eq_p(val, var)) {
|
|
|
|
return pic_false_value();
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 02:51:31 -04:00
|
|
|
env = pic_apply0(pic, pic_proc_ptr(val));
|
2014-09-15 02:38:38 -04:00
|
|
|
while (! pic_nil_p(env)) {
|
2015-01-17 07:01:13 -05:00
|
|
|
binding = pic_car(pic, env);
|
2014-09-15 02:38:38 -04:00
|
|
|
|
2015-01-17 07:01:13 -05:00
|
|
|
while (! pic_nil_p(binding)) {
|
|
|
|
if (pic_eq_p(pic_caar(pic, binding), var)) {
|
|
|
|
return pic_car(pic, binding);
|
|
|
|
}
|
|
|
|
binding = pic_cdr(pic, binding);
|
2014-09-15 02:38:38 -04:00
|
|
|
}
|
|
|
|
env = pic_cdr(pic, env);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
return pic_false_value();
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-15 02:38:38 -04:00
|
|
|
var_call(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2014-09-15 02:38:38 -04:00
|
|
|
struct pic_proc *self = pic_get_proc(pic);
|
|
|
|
pic_value val, tmp, box, conv;
|
2014-09-26 02:14:30 -04:00
|
|
|
int n;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
n = pic_get_args(pic, "|oo", &val, &tmp);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
box = var_lookup(pic, pic_obj_value(self));
|
|
|
|
if (! pic_test(box)) {
|
2014-09-24 20:44:21 -04:00
|
|
|
box = pic_attr_ref(pic, pic_obj_value(self), "@@box");
|
2014-09-15 02:38:38 -04:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
switch (n) {
|
|
|
|
case 0:
|
2015-01-17 07:01:13 -05:00
|
|
|
return pic_cdr(pic, box);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
case 1:
|
2014-09-24 20:44:21 -04:00
|
|
|
conv = pic_attr_ref(pic, pic_obj_value(self), "@@converter");
|
2014-09-15 02:38:38 -04:00
|
|
|
if (pic_test(conv)) {
|
|
|
|
pic_assert_type(pic, conv, proc);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
val = pic_apply1(pic, pic_proc_ptr(conv), val);
|
|
|
|
}
|
2015-01-17 07:01:13 -05:00
|
|
|
pic_set_cdr(pic, box, val);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
return pic_none_value();
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
case 2:
|
|
|
|
assert(pic_false_p(tmp));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-24 20:44:21 -04:00
|
|
|
conv = pic_attr_ref(pic, pic_obj_value(self), "@@converter");
|
2014-09-15 02:38:38 -04:00
|
|
|
if (pic_test(conv)) {
|
|
|
|
pic_assert_type(pic, conv, proc);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
return pic_apply1(pic, pic_proc_ptr(conv), val);
|
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 16:11:48 -05:00
|
|
|
PIC_UNREACHABLE();
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
struct pic_proc *
|
|
|
|
pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2014-09-15 02:38:38 -04:00
|
|
|
struct pic_proc *var;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
var = pic_make_proc(pic, var_call, "<var-call>");
|
2015-01-17 07:01:13 -05:00
|
|
|
pic_attr_set(pic, pic_obj_value(var), "@@box", pic_cons(pic, pic_false_value(), init));
|
2014-09-24 20:44:21 -04:00
|
|
|
pic_attr_set(pic, pic_obj_value(var), "@@converter", conv ? pic_obj_value(conv) : pic_false_value());
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
return var;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-15 02:38:38 -04:00
|
|
|
pic_var_make_parameter(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2014-09-15 02:38:38 -04:00
|
|
|
struct pic_proc *conv = NULL;
|
|
|
|
pic_value init;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
pic_get_args(pic, "o|l", &init, &conv);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
return pic_obj_value(pic_make_var(pic, init, conv));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_var(pic_state *pic)
|
|
|
|
{
|
2014-09-15 11:22:54 -04:00
|
|
|
pic_define_noexport(pic, "current-dynamic-environment", pic_false_value());
|
2014-09-15 02:38:38 -04:00
|
|
|
|
2014-08-31 22:37:52 -04:00
|
|
|
pic_defun(pic, "make-parameter", pic_var_make_parameter);
|
2014-09-15 02:38:38 -04:00
|
|
|
|
|
|
|
pic_set(pic, pic->PICRIN_BASE, "current-dynamic-environment", pic_obj_value(pic_make_var(pic, pic_nil_value(), NULL)));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|