picrin/extlib/benz/var.c

100 lines
1.7 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-20 10:58:58 -05:00
#include "picrin/extra.h"
2016-02-18 10:03:34 -05:00
#include "picrin/object.h"
#include "picrin/state.h"
2014-08-25 00:38:09 -04:00
2015-06-08 09:28:17 -04:00
static pic_value
2016-02-19 10:03:16 -05:00
var_get(pic_state *pic, pic_value var)
2015-06-08 09:28:17 -04:00
{
2016-02-19 14:35:15 -05:00
pic_value weak, it;
2016-02-19 14:35:15 -05:00
pic_for_each (weak, pic->ptable, it) {
if (pic_weak_has(pic, weak, var)) {
return pic_weak_ref(pic, weak, var);
}
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
2016-02-19 10:03:16 -05:00
var_set(pic_state *pic, pic_value var, pic_value val)
2014-08-25 00:38:09 -04:00
{
2016-02-19 14:35:15 -05:00
pic_value weak;
2014-08-25 00:38:09 -04:00
2016-02-19 14:35:15 -05:00
weak = pic_car(pic, pic->ptable);
2014-08-25 00:38:09 -04:00
2016-02-19 14:35:15 -05:00
pic_weak_set(pic, weak, var, val);
2014-08-25 00:38:09 -04:00
return pic_undef_value(pic);
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-19 10:03:16 -05:00
pic_value self, val;
2015-06-08 09:28:17 -04:00
int n;
2014-08-25 00:38:09 -04: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 {
pic_value conv;
conv = pic_closure_ref(pic, 0);
if (! pic_false_p(pic, conv)) {
2016-02-19 10:03:16 -05:00
val = pic_call(pic, conv, 1, val);
}
return var_set(pic, self, val);
}
2014-08-25 00:38:09 -04:00
}
2016-02-19 10:03:16 -05:00
pic_value
pic_make_var(pic_state *pic, pic_value init, pic_value conv)
2014-08-25 00:38:09 -04:00
{
2016-02-19 10:03:16 -05:00
pic_value var;
2015-06-08 09:28:17 -04:00
2016-02-19 10:03:16 -05:00
var = pic_lambda(pic, var_call, 1, 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
return var;
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_var_make_parameter(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-19 10:03:16 -05:00
pic_value init, conv = pic_false_value(pic);
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|l", &init, &conv);
2014-08-25 00:38:09 -04:00
2016-02-19 10:03:16 -05:00
return 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)
{
2016-02-19 10:03:16 -05:00
pic_value body, val;
2015-06-08 09:28:17 -04:00
pic_get_args(pic, "l", &body);
2016-02-19 14:35:15 -05:00
pic->ptable = pic_cons(pic, 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
}