示例#1
0
int test_environment()
{
   symbol *sa = new_symbol("a");
   symbol *sb = new_symbol("b");
   symbol *sc = new_symbol("c");

   symbol *sx = new_symbol("x");
   symbol *sy = new_symbol("y");
   symbol *sz = new_symbol("z");

   symbol *sn = new_symbol("n");

   integer *i10 = new_integer(10);
   integer *i20 = new_integer(20);
   integer *i30 = new_integer(30);

   integer *i40 = new_integer(40);
   integer *i50 = new_integer(50);
   integer *i60 = new_integer(60);

   list *vara = cons(sa, cons(sb, cons(sc, NULL)));
   list *varx = cons(sx, cons(sy, cons(sz, NULL)));
   list *val10 = cons(i10, cons(i20, cons(i30, NULL)));
   list *val40 = cons(i40, cons(i50, cons(i60, NULL)));

   environment *env = NULL;

   env = extend_env(vara, val10, env);

   assert(generic_equal(lookup_var_val(sa, env), cons(sa, i10)));
   assert(generic_equal(lookup_var_val(sb, env), cons(sb, i20)));
   assert(generic_equal(lookup_var_val(sc, env), cons(sc, i30)));

   env = define_var_val(sx, i40, env);

   assert(generic_equal(lookup_var_val(sa, env), cons(sa, i10)));
   assert(generic_equal(lookup_var_val(sb, env), cons(sb, i20)));
   assert(generic_equal(lookup_var_val(sc, env), cons(sc, i30)));
   assert(generic_equal(lookup_var_val(sx, env), cons(sx, i40)));
   assert(generic_equal(lookup_var_val(sy, env), NULL));

   env = set_var_val(sx, i50, env);

   assert(generic_equal(lookup_var_val(sa, env), cons(sa, i10)));
   assert(generic_equal(lookup_var_val(sb, env), cons(sb, i20)));
   assert(generic_equal(lookup_var_val(sc, env), cons(sc, i30)));
   assert(generic_equal(lookup_var_val(sx, env), cons(sx, i50)));
   assert(generic_equal(lookup_var_val(sy, env), NULL));

   env = extend_env(varx, val40, env);
   assert(generic_equal(lookup_var_val(sa, env), cons(sa, i10)));
   assert(generic_equal(lookup_var_val(sb, env), cons(sb, i20)));
   assert(generic_equal(lookup_var_val(sc, env), cons(sc, i30)));
   assert(generic_equal(lookup_var_val(sx, env), cons(sx, i40)));
   assert(generic_equal(lookup_var_val(sy, env), cons(sy, i50)));
   assert(generic_equal(lookup_var_val(sz, env), cons(sz, i60)));
   assert(generic_equal(lookup_var_val(sn, env), NULL));

   return 1;
}
示例#2
0
文件: eval.c 项目: lienhua34/CSchemer
//one arg: exp
static cellpoint eval_assignment(void)
{
	//calls assignment_variable
	args_push(args_ref(1));
	reg = assignment_variable();
	stack_push(&vars_stack, reg);
	//compute assignment value
	args_push(args_ref(1));
	reg = assignment_value();
	args_push(a_false);
	args_push(reg);
	reg = eval();
	stack_push(&vars_stack, reg);
	//calls set_var_val
	args_push(current_env);
	args_push(stack_pop(&vars_stack));
	args_push(stack_pop(&vars_stack));
	set_var_val();
	
	args_pop(1);
	return make_symbol("ok");
}