<commit_before>/**
 * See Copyright Notice in picrin.h
 */

#include "picrin.h"

static pic_value
var_lookup(pic_state *pic, pic_value var)
{
  pic_value val, env, binding;

  val = pic_ref(pic, pic->PICRIN_BASE, "current-dynamic-environment");
  if (pic_eq_p(val, var)) {
    return pic_false_value();
  }

  env = pic_apply0(pic, pic_proc_ptr(val));
  while (! pic_nil_p(env)) {
    binding = pic_car(pic, env);

    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);
  }

  return pic_false_value();
}

static pic_value
var_call(pic_state *pic)
{
  struct pic_proc *self = pic_get_proc(pic);
  pic_value val, tmp, box, conv;
  int n;

  n = pic_get_args(pic, "|oo", &val, &tmp);

  box = var_lookup(pic, pic_obj_value(self));
  if (! pic_test(box)) {
    box = pic_attr_ref(pic, pic_obj_value(self), "@@box");
  }

  switch (n) {
  case 0:
    return pic_cdr(pic, box);

  case 1:
    conv = pic_attr_ref(pic, pic_obj_value(self), "@@converter");
    if (pic_test(conv)) {
      pic_assert_type(pic, conv, proc);

      val = pic_apply1(pic, pic_proc_ptr(conv), val);
    }
    pic_set_cdr(pic, box, val);

    return pic_none_value();

  case 2:
    assert(pic_false_p(tmp));

    conv = pic_attr_ref(pic, pic_obj_value(self), "@@converter");
    if (pic_test(conv)) {
      pic_assert_type(pic, conv, proc);

      return pic_apply1(pic, pic_proc_ptr(conv), val);
    } else {
      return val;
    }
  }
  PIC_UNREACHABLE();
}

struct pic_proc *
pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv)
{
  struct pic_proc *var;

  var = pic_make_proc(pic, var_call, "<var-call>");
  pic_attr_set(pic, pic_obj_value(var), "@@box", pic_cons(pic, pic_false_value(), init));
  pic_attr_set(pic, pic_obj_value(var), "@@converter", conv ? pic_obj_value(conv) : pic_false_value());

  return var;
}

static pic_value
pic_var_make_parameter(pic_state *pic)
{
  struct pic_proc *conv = NULL;
  pic_value init;
<commit_msg>use proc_env in var implementation<commit_after>/**
 * See Copyright Notice in picrin.h
 */

#include "picrin.h"

static pic_value
var_lookup(pic_state *pic, struct pic_proc *var)
{
  pic_value val, env, binding;
  pic_value key = pic_obj_value(var);

  val = pic_ref(pic, pic->PICRIN_BASE, "current-dynamic-environment");
  if (pic_eq_p(val, key)) {
    return pic_false_value();
  }

  env = pic_apply0(pic, pic_proc_ptr(val));
  while (! pic_nil_p(env)) {
    binding = pic_car(pic, env);

    while (! pic_nil_p(binding)) {
      if (pic_eq_p(pic_caar(pic, binding), key)) {
        return pic_car(pic, binding);
      }
      binding = pic_cdr(pic, binding);
    }
    env = pic_cdr(pic, env);
  }

  return pic_false_value();
}

static pic_value
var_call(pic_state *pic)
{
  struct pic_proc *self = pic_get_proc(pic);
  pic_value val, tmp, box, conv;
  int n;

  n = pic_get_args(pic, "|oo", &val, &tmp);

  box = var_lookup(pic, self);
  if (! pic_test(box)) {
    box = pic_proc_env_ref(pic, self, "box");
  }

  switch (n) {
  case 0:
    return pic_cdr(pic, box);

  case 1:
    conv = pic_proc_env_ref(pic, self, "conv");
    if (pic_test(conv)) {
      pic_assert_type(pic, conv, proc);

      val = pic_apply1(pic, pic_proc_ptr(conv), val);
    }
    pic_set_cdr(pic, box, val);

    return pic_none_value();

  case 2:
    assert(pic_false_p(tmp));

    conv = pic_proc_env_ref(pic, self, "conv");
    if (pic_test(conv)) {
      pic_assert_type(pic, conv, proc);

      return pic_apply1(pic, pic_proc_ptr(conv), val);
    } else {
      return val;
    }
  }
  PIC_UNREACHABLE();
}

struct pic_proc *
pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv)
{
  struct pic_proc *var;

  var = pic_make_proc(pic, var_call, "<var-call>");
  pic_proc_env_set(pic, var, "box", pic_cons(pic, pic_false_value(), init));
  pic_proc_env_set(pic, var, "conv", conv ? pic_obj_value(conv) : pic_false_value());

  return var;
}

static pic_value
pic_var_make_parameter(pic_state *pic)
{
  struct pic_proc *conv = NULL;
  pic_value init;

{'fraction_non_alphanumeric': 0.14534461910519952, 'fraction_numerical': 0.002902055622732769, 'mean_word_length': 3.7981438515081205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4465792', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1625, 'n_words': 344}