2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
2014-09-15 02:38:38 -04:00
|
|
|
static pic_value
|
2015-06-08 09:28:17 -04:00
|
|
|
var_conv(pic_state *pic, struct pic_proc *var, pic_value val)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-08 09:28:17 -04:00
|
|
|
if (pic_proc_env_has(pic, var, "conv") != 0) {
|
2016-02-13 23:46:55 -05:00
|
|
|
return pic_call(pic, pic_proc_ptr(pic_proc_env_ref(pic, var, "conv")), 1, val);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-06-08 09:28:17 -04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
var_get(pic_state *pic, struct pic_proc *var)
|
|
|
|
{
|
|
|
|
pic_value elem, it;
|
2016-02-10 07:50:39 -05:00
|
|
|
struct pic_weak *weak;
|
2014-09-15 02:38:38 -04:00
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
pic_for_each (elem, pic->ptable, it) {
|
2016-02-10 07:50:39 -05:00
|
|
|
weak = pic_weak_ptr(elem);
|
|
|
|
if (pic_weak_has(pic, weak, var)) {
|
|
|
|
return pic_weak_ref(pic, weak, var);
|
2014-09-15 02:38:38 -04:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-06-08 09:28:17 -04:00
|
|
|
pic_panic(pic, "logic flaw");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2015-06-08 09:28:17 -04:00
|
|
|
var_set(pic_state *pic, struct pic_proc *var, pic_value val)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
struct pic_weak *weak;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
weak = pic_weak_ptr(pic_car(pic, pic->ptable));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_set(pic, weak, var, val);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 03:34:45 -04:00
|
|
|
return pic_undef_value();
|
2015-06-08 09:28:17 -04:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
static pic_value
|
|
|
|
var_call(pic_state *pic)
|
|
|
|
{
|
2016-02-06 10:48:02 -05:00
|
|
|
struct pic_proc *self;
|
2015-06-08 09:28:17 -04:00
|
|
|
pic_value val;
|
|
|
|
int n;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-06 10:48:02 -05:00
|
|
|
n = pic_get_args(pic, "&|o", &self, &val);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
if (n == 0) {
|
|
|
|
return var_get(pic, self);
|
|
|
|
} else {
|
2015-06-08 09:41:43 -04:00
|
|
|
return var_set(pic, self, var_conv(pic, self, val));
|
2014-09-15 02:38:38 -04:00
|
|
|
}
|
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
|
|
|
|
2015-06-27 06:02:18 -04:00
|
|
|
var = pic_make_proc(pic, var_call);
|
2015-06-08 09:28:17 -04:00
|
|
|
|
|
|
|
if (conv != NULL) {
|
2015-06-09 05:36:39 -04:00
|
|
|
pic_proc_env_set(pic, var, "conv", pic_obj_value(conv));
|
2015-06-08 09:28:17 -04:00
|
|
|
}
|
|
|
|
|
2016-02-13 23:46:55 -05:00
|
|
|
pic_call(pic, var, 1, init);
|
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
|
|
|
}
|
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
static pic_value
|
|
|
|
pic_var_with_parameter(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *body;
|
|
|
|
pic_value val;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l", &body);
|
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable);
|
2015-06-08 09:28:17 -04:00
|
|
|
|
2016-02-13 23:46:55 -05:00
|
|
|
val = pic_call(pic, body, 0);
|
2015-06-08 09:28:17 -04:00
|
|
|
|
|
|
|
pic->ptable = pic_cdr(pic, pic->ptable);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
void
|
|
|
|
pic_init_var(pic_state *pic)
|
|
|
|
{
|
2014-08-31 22:37:52 -04:00
|
|
|
pic_defun(pic, "make-parameter", pic_var_make_parameter);
|
2015-06-08 09:28:17 -04:00
|
|
|
pic_defun(pic, "with-parameter", pic_var_with_parameter);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|