picrin/var.c

111 lines
2.4 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "picrin/pair.h"
#include "picrin/proc.h"
2014-08-25 00:38:09 -04:00
static pic_value
var_lookup(pic_state *pic, pic_value var)
2014-08-25 00:38:09 -04:00
{
Squashed 'extlib/benz/' changes from 414f790..057b5f2 057b5f2 Merge pull request #245 from picrin-scheme/heap-symbol c91f6cd [bugfix] build failure in debug mode b1849c4 pic_sym is not a pointer e353b07 s/pic_sym/pic_sym_ptr/g a11fb91 [bugfix] irep->name is missed to mark 1820a25 [bugfix] wrong type specified for cxt->syms b8d2b8e better error messages against invalid use of auxiliary syntax d9ade33 mark only interned symbols and some specisl uninterned symbols da2217b move symbol constants to pic_state 78b035b [bugfix] pic_intern must count up reference of the return value bbdc663 rename internal object ba01821 s/SYMBOL_P/SYMBOLP/g, s/PAIR_P/PAIRP/g 1af32d1 improve error message dd09fbf don't malloc in pic_interned_p 7f51070 turn on GC 7460e81 add gc on/off flag f3742db move symbol-related macros to symbol.h ded6759 remove pic_sym_value ec97d07 remove pic_symbol_value 28bd059 heap symbol seems working (with GC stopped) e0d6fe9 change pic_intern interface 6750693 remove pic_ungensym 4ea7d3c add irep->syms ede7a99 use dictionary for senv->map b3cb50c use dictionaries for temporary import table fc698b5 use dictionary for rec->data 1b814d4 use dictionary for lib->exports 7ae1af4 use dictionaries for pic->globals and pic->macros 74f9979 remove 'struct pic_macro'. define-syntax spec is changed. 86136c5 some procedures are moved to contrib/ da99761 gather all includes of standard headers into picrin.h 7df8d77 add dictionary-map and dictionary-for-each b625ff8 revert 48f0ec90. dicitonary is now symbol-to-object structure f7657d7 [prepare] dictionary is to be changed to have only symbols for its keys git-subtree-dir: extlib/benz git-subtree-split: 057b5f29110ab3f75513573d291ea18acb782357
2015-01-20 03:44:06 -05:00
pic_value val, env, binding;
2014-08-25 00:38:09 -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
}
env = pic_apply0(pic, pic_proc_ptr(val));
while (! pic_nil_p(env)) {
Squashed 'extlib/benz/' changes from 414f790..057b5f2 057b5f2 Merge pull request #245 from picrin-scheme/heap-symbol c91f6cd [bugfix] build failure in debug mode b1849c4 pic_sym is not a pointer e353b07 s/pic_sym/pic_sym_ptr/g a11fb91 [bugfix] irep->name is missed to mark 1820a25 [bugfix] wrong type specified for cxt->syms b8d2b8e better error messages against invalid use of auxiliary syntax d9ade33 mark only interned symbols and some specisl uninterned symbols da2217b move symbol constants to pic_state 78b035b [bugfix] pic_intern must count up reference of the return value bbdc663 rename internal object ba01821 s/SYMBOL_P/SYMBOLP/g, s/PAIR_P/PAIRP/g 1af32d1 improve error message dd09fbf don't malloc in pic_interned_p 7f51070 turn on GC 7460e81 add gc on/off flag f3742db move symbol-related macros to symbol.h ded6759 remove pic_sym_value ec97d07 remove pic_symbol_value 28bd059 heap symbol seems working (with GC stopped) e0d6fe9 change pic_intern interface 6750693 remove pic_ungensym 4ea7d3c add irep->syms ede7a99 use dictionary for senv->map b3cb50c use dictionaries for temporary import table fc698b5 use dictionary for rec->data 1b814d4 use dictionary for lib->exports 7ae1af4 use dictionaries for pic->globals and pic->macros 74f9979 remove 'struct pic_macro'. define-syntax spec is changed. 86136c5 some procedures are moved to contrib/ da99761 gather all includes of standard headers into picrin.h 7df8d77 add dictionary-map and dictionary-for-each b625ff8 revert 48f0ec90. dicitonary is now symbol-to-object structure f7657d7 [prepare] dictionary is to be changed to have only symbols for its keys git-subtree-dir: extlib/benz git-subtree-split: 057b5f29110ab3f75513573d291ea18acb782357
2015-01-20 03:44:06 -05:00
binding = pic_car(pic, env);
Squashed 'extlib/benz/' changes from 414f790..057b5f2 057b5f2 Merge pull request #245 from picrin-scheme/heap-symbol c91f6cd [bugfix] build failure in debug mode b1849c4 pic_sym is not a pointer e353b07 s/pic_sym/pic_sym_ptr/g a11fb91 [bugfix] irep->name is missed to mark 1820a25 [bugfix] wrong type specified for cxt->syms b8d2b8e better error messages against invalid use of auxiliary syntax d9ade33 mark only interned symbols and some specisl uninterned symbols da2217b move symbol constants to pic_state 78b035b [bugfix] pic_intern must count up reference of the return value bbdc663 rename internal object ba01821 s/SYMBOL_P/SYMBOLP/g, s/PAIR_P/PAIRP/g 1af32d1 improve error message dd09fbf don't malloc in pic_interned_p 7f51070 turn on GC 7460e81 add gc on/off flag f3742db move symbol-related macros to symbol.h ded6759 remove pic_sym_value ec97d07 remove pic_symbol_value 28bd059 heap symbol seems working (with GC stopped) e0d6fe9 change pic_intern interface 6750693 remove pic_ungensym 4ea7d3c add irep->syms ede7a99 use dictionary for senv->map b3cb50c use dictionaries for temporary import table fc698b5 use dictionary for rec->data 1b814d4 use dictionary for lib->exports 7ae1af4 use dictionaries for pic->globals and pic->macros 74f9979 remove 'struct pic_macro'. define-syntax spec is changed. 86136c5 some procedures are moved to contrib/ da99761 gather all includes of standard headers into picrin.h 7df8d77 add dictionary-map and dictionary-for-each b625ff8 revert 48f0ec90. dicitonary is now symbol-to-object structure f7657d7 [prepare] dictionary is to be changed to have only symbols for its keys git-subtree-dir: extlib/benz git-subtree-split: 057b5f29110ab3f75513573d291ea18acb782357
2015-01-20 03:44:06 -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);
}
env = pic_cdr(pic, env);
2014-08-25 00:38:09 -04:00
}
return pic_false_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
var_call(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
struct pic_proc *self = pic_get_proc(pic);
pic_value val, tmp, box, conv;
int n;
2014-08-25 00:38:09 -04:00
n = pic_get_args(pic, "|oo", &val, &tmp);
2014-08-25 00:38:09 -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-08-25 00:38:09 -04:00
switch (n) {
case 0:
Squashed 'extlib/benz/' changes from 414f790..057b5f2 057b5f2 Merge pull request #245 from picrin-scheme/heap-symbol c91f6cd [bugfix] build failure in debug mode b1849c4 pic_sym is not a pointer e353b07 s/pic_sym/pic_sym_ptr/g a11fb91 [bugfix] irep->name is missed to mark 1820a25 [bugfix] wrong type specified for cxt->syms b8d2b8e better error messages against invalid use of auxiliary syntax d9ade33 mark only interned symbols and some specisl uninterned symbols da2217b move symbol constants to pic_state 78b035b [bugfix] pic_intern must count up reference of the return value bbdc663 rename internal object ba01821 s/SYMBOL_P/SYMBOLP/g, s/PAIR_P/PAIRP/g 1af32d1 improve error message dd09fbf don't malloc in pic_interned_p 7f51070 turn on GC 7460e81 add gc on/off flag f3742db move symbol-related macros to symbol.h ded6759 remove pic_sym_value ec97d07 remove pic_symbol_value 28bd059 heap symbol seems working (with GC stopped) e0d6fe9 change pic_intern interface 6750693 remove pic_ungensym 4ea7d3c add irep->syms ede7a99 use dictionary for senv->map b3cb50c use dictionaries for temporary import table fc698b5 use dictionary for rec->data 1b814d4 use dictionary for lib->exports 7ae1af4 use dictionaries for pic->globals and pic->macros 74f9979 remove 'struct pic_macro'. define-syntax spec is changed. 86136c5 some procedures are moved to contrib/ da99761 gather all includes of standard headers into picrin.h 7df8d77 add dictionary-map and dictionary-for-each b625ff8 revert 48f0ec90. dicitonary is now symbol-to-object structure f7657d7 [prepare] dictionary is to be changed to have only symbols for its keys git-subtree-dir: extlib/benz git-subtree-split: 057b5f29110ab3f75513573d291ea18acb782357
2015-01-20 03:44:06 -05:00
return pic_cdr(pic, box);
2014-08-25 00:38:09 -04:00
case 1:
2014-09-24 20:44:21 -04:00
conv = pic_attr_ref(pic, pic_obj_value(self), "@@converter");
if (pic_test(conv)) {
pic_assert_type(pic, conv, proc);
2014-08-25 00:38:09 -04:00
val = pic_apply1(pic, pic_proc_ptr(conv), val);
}
Squashed 'extlib/benz/' changes from 414f790..057b5f2 057b5f2 Merge pull request #245 from picrin-scheme/heap-symbol c91f6cd [bugfix] build failure in debug mode b1849c4 pic_sym is not a pointer e353b07 s/pic_sym/pic_sym_ptr/g a11fb91 [bugfix] irep->name is missed to mark 1820a25 [bugfix] wrong type specified for cxt->syms b8d2b8e better error messages against invalid use of auxiliary syntax d9ade33 mark only interned symbols and some specisl uninterned symbols da2217b move symbol constants to pic_state 78b035b [bugfix] pic_intern must count up reference of the return value bbdc663 rename internal object ba01821 s/SYMBOL_P/SYMBOLP/g, s/PAIR_P/PAIRP/g 1af32d1 improve error message dd09fbf don't malloc in pic_interned_p 7f51070 turn on GC 7460e81 add gc on/off flag f3742db move symbol-related macros to symbol.h ded6759 remove pic_sym_value ec97d07 remove pic_symbol_value 28bd059 heap symbol seems working (with GC stopped) e0d6fe9 change pic_intern interface 6750693 remove pic_ungensym 4ea7d3c add irep->syms ede7a99 use dictionary for senv->map b3cb50c use dictionaries for temporary import table fc698b5 use dictionary for rec->data 1b814d4 use dictionary for lib->exports 7ae1af4 use dictionaries for pic->globals and pic->macros 74f9979 remove 'struct pic_macro'. define-syntax spec is changed. 86136c5 some procedures are moved to contrib/ da99761 gather all includes of standard headers into picrin.h 7df8d77 add dictionary-map and dictionary-for-each b625ff8 revert 48f0ec90. dicitonary is now symbol-to-object structure f7657d7 [prepare] dictionary is to be changed to have only symbols for its keys git-subtree-dir: extlib/benz git-subtree-split: 057b5f29110ab3f75513573d291ea18acb782357
2015-01-20 03:44:06 -05:00
pic_set_cdr(pic, box, val);
2014-08-25 00:38:09 -04:00
return pic_none_value();
2014-08-25 00:38:09 -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");
if (pic_test(conv)) {
pic_assert_type(pic, conv, proc);
2014-08-25 00:38:09 -04:00
return pic_apply1(pic, pic_proc_ptr(conv), val);
} else {
return val;
}
}
PIC_UNREACHABLE();
2014-08-25 00:38:09 -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
{
struct pic_proc *var;
2014-08-25 00:38:09 -04:00
var = pic_make_proc(pic, var_call, "<var-call>");
Squashed 'extlib/benz/' changes from 414f790..057b5f2 057b5f2 Merge pull request #245 from picrin-scheme/heap-symbol c91f6cd [bugfix] build failure in debug mode b1849c4 pic_sym is not a pointer e353b07 s/pic_sym/pic_sym_ptr/g a11fb91 [bugfix] irep->name is missed to mark 1820a25 [bugfix] wrong type specified for cxt->syms b8d2b8e better error messages against invalid use of auxiliary syntax d9ade33 mark only interned symbols and some specisl uninterned symbols da2217b move symbol constants to pic_state 78b035b [bugfix] pic_intern must count up reference of the return value bbdc663 rename internal object ba01821 s/SYMBOL_P/SYMBOLP/g, s/PAIR_P/PAIRP/g 1af32d1 improve error message dd09fbf don't malloc in pic_interned_p 7f51070 turn on GC 7460e81 add gc on/off flag f3742db move symbol-related macros to symbol.h ded6759 remove pic_sym_value ec97d07 remove pic_symbol_value 28bd059 heap symbol seems working (with GC stopped) e0d6fe9 change pic_intern interface 6750693 remove pic_ungensym 4ea7d3c add irep->syms ede7a99 use dictionary for senv->map b3cb50c use dictionaries for temporary import table fc698b5 use dictionary for rec->data 1b814d4 use dictionary for lib->exports 7ae1af4 use dictionaries for pic->globals and pic->macros 74f9979 remove 'struct pic_macro'. define-syntax spec is changed. 86136c5 some procedures are moved to contrib/ da99761 gather all includes of standard headers into picrin.h 7df8d77 add dictionary-map and dictionary-for-each b625ff8 revert 48f0ec90. dicitonary is now symbol-to-object structure f7657d7 [prepare] dictionary is to be changed to have only symbols for its keys git-subtree-dir: extlib/benz git-subtree-split: 057b5f29110ab3f75513573d291ea18acb782357
2015-01-20 03:44:06 -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
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
{
struct pic_proc *conv = NULL;
pic_value init;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|l", &init, &conv);
2014-08-25 00:38:09 -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-08-31 22:37:52 -04:00
pic_defun(pic, "make-parameter", pic_var_make_parameter);
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
}