picrin/src/var.c

187 lines
3.0 KiB
C
Raw Normal View History

2014-01-17 06:58:31 -05:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2014-01-08 10:38:09 -05:00
#include "picrin/var.h"
2014-07-12 11:48:03 -04:00
#include "picrin/pair.h"
2014-01-08 10:38:09 -05:00
2014-07-12 01:47:18 -04:00
static pic_value
var_ref(pic_state *pic, struct pic_var *var)
2014-01-08 10:38:09 -05:00
{
2014-07-12 11:48:03 -04:00
return pic_car(pic, var->stack);
2014-01-08 10:38:09 -05:00
}
2014-07-12 01:47:18 -04:00
static void
var_set(pic_state *pic, struct pic_var *var, pic_value value)
2014-01-08 10:38:09 -05:00
{
2014-07-12 11:48:03 -04:00
pic_set_car(pic, var->stack, value);
}
static void
var_push(pic_state *pic, struct pic_var *var, pic_value value)
{
var->stack = pic_cons(pic, value, var->stack);
}
static void
var_pop(pic_state *pic, struct pic_var *var)
{
var->stack = pic_cdr(pic, var->stack);
2014-01-08 10:38:09 -05:00
}
2014-07-12 01:47:18 -04:00
struct pic_var *
pic_var_new(pic_state *pic, pic_value init)
2014-01-08 10:38:09 -05:00
{
2014-07-12 01:47:18 -04:00
struct pic_var *var;
var = (struct pic_var *)pic_obj_alloc(pic, sizeof(struct pic_var), PIC_TT_VAR);
2014-07-12 11:48:03 -04:00
var->stack = pic_nil_value();
var_push(pic, var, init);
2014-07-12 01:47:18 -04:00
return var;
2014-01-08 10:38:09 -05:00
}
pic_value
pic_var_ref(pic_state *pic, const char *name)
2014-01-08 10:38:09 -05:00
{
pic_value v;
struct pic_var *var;
2014-01-08 10:38:09 -05:00
v = pic_ref(pic, name);
2014-01-08 10:38:09 -05:00
pic_assert_type(pic, v, var);
2014-01-08 10:38:09 -05:00
var = pic_var_ptr(v);
return var_ref(pic, var);
}
void
pic_var_set(pic_state *pic, const char *name, pic_value value)
2014-01-17 22:58:00 -05:00
{
pic_value v;
struct pic_var *var;
v = pic_ref(pic, name);
pic_assert_type(pic, v, var);
var = pic_var_ptr(v);
var_set(pic, var, value);
2014-01-17 22:58:00 -05:00
}
2014-07-12 11:48:03 -04:00
void
pic_var_push(pic_state *pic, const char *name, pic_value value)
{
pic_value v;
struct pic_var *var;
v = pic_ref(pic, name);
pic_assert_type(pic, v, var);
var = pic_var_ptr(v);
var_push(pic, var, value);
}
void
pic_var_pop(pic_state *pic, const char *name)
{
pic_value v;
struct pic_var *var;
v = pic_ref(pic, name);
pic_assert_type(pic, v, var);
var = pic_var_ptr(v);
var_pop(pic, var);
}
2014-01-08 10:38:09 -05:00
static pic_value
pic_var_make_var(pic_state *pic)
2014-01-08 10:38:09 -05:00
{
pic_value init;
pic_get_args(pic, "o", &init);
2014-01-08 10:38:09 -05:00
return pic_obj_value(pic_var_new(pic, init));
2014-01-08 10:38:09 -05:00
}
static pic_value
pic_var_var_ref(pic_state *pic)
2014-01-08 10:38:09 -05:00
{
struct pic_var *var;
pic_value v;
pic_get_args(pic, "o", &v);
pic_assert_type(pic, v, var);
2014-01-08 10:38:09 -05:00
var = pic_var_ptr(v);
2014-01-08 10:38:09 -05:00
2014-07-12 01:47:18 -04:00
return var_ref(pic, var);
2014-01-08 10:38:09 -05:00
}
static pic_value
pic_var_var_set(pic_state *pic)
2014-01-08 10:38:09 -05:00
{
struct pic_var *var;
pic_value v, val;
2014-01-08 10:38:09 -05:00
pic_get_args(pic, "oo", &v, &val);
2014-01-08 10:38:09 -05:00
pic_assert_type(pic, v, var);
2014-01-08 10:38:09 -05:00
var = pic_var_ptr(v);
var_set(pic, var, val);
2014-07-12 11:48:03 -04:00
return pic_none_value();
}
static pic_value
pic_var_var_push(pic_state *pic)
{
struct pic_var *var;
pic_value v, val;
pic_get_args(pic, "oo", &v, &val);
pic_assert_type(pic, v, var);
var = pic_var_ptr(v);
var_push(pic, var, val);
return pic_none_value();
}
2014-01-08 10:38:09 -05:00
2014-07-12 11:48:03 -04:00
static pic_value
pic_var_var_pop(pic_state *pic)
{
struct pic_var *var;
pic_value v;
pic_get_args(pic, "o", &v);
pic_assert_type(pic, v, var);
var = pic_var_ptr(v);
var_pop(pic, var);
return pic_none_value();
2014-01-08 10:38:09 -05:00
}
void
pic_init_var(pic_state *pic)
{
pic_deflibrary ("(picrin var)") {
pic_defun(pic, "make-var", pic_var_make_var);
pic_defun(pic, "var-ref", pic_var_var_ref);
pic_defun(pic, "var-set!", pic_var_var_set);
2014-07-12 11:48:03 -04:00
pic_defun(pic, "var-push!", pic_var_var_push);
pic_defun(pic, "var-pop!", pic_var_var_pop);
2014-01-08 10:38:09 -05:00
}
}