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) {
|
|
|
|
return pic_apply1(pic, pic_proc_ptr(pic_proc_env_ref(pic, var, "conv")), 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;
|
2015-06-09 05:36:39 -04:00
|
|
|
struct pic_reg *reg;
|
2014-09-15 02:38:38 -04:00
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
pic_for_each (elem, pic->ptable, it) {
|
2015-06-09 05:36:39 -04:00
|
|
|
reg = pic_reg_ptr(elem);
|
|
|
|
if (pic_reg_has(pic, reg, var)) {
|
|
|
|
return pic_reg_ref(pic, reg, 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
|
|
|
{
|
2015-06-09 05:36:39 -04:00
|
|
|
struct pic_reg *reg;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 05:36:39 -04:00
|
|
|
reg = pic_reg_ptr(pic_car(pic, pic->ptable));
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 05:36:39 -04:00
|
|
|
pic_reg_set(pic, reg, 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)
|
|
|
|
{
|
|
|
|
struct pic_proc *self = pic_get_proc(pic);
|
|
|
|
pic_value val;
|
|
|
|
int n;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
n = pic_get_args(pic, "|o", &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
|
|
|
}
|
|
|
|
|
|
|
|
pic_apply1(pic, var, 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);
|
|
|
|
|
2015-06-09 05:36:39 -04:00
|
|
|
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_reg(pic)), pic->ptable);
|
2015-06-08 09:28:17 -04:00
|
|
|
|
|
|
|
val = pic_apply0(pic, body);
|
|
|
|
|
|
|
|
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
|
|
|
}
|