Exemplo n.º 1
0
HklrObject* hklr_search(HklString* key)
{
  assert(key != NULL);

  HklScope* scope = ((HklScope*) HKLR.scopes->tail->data);
  HklPair* pair = NULL;

  // check your scope first
  pair = hkl_hash_search(scope->locals, key);
  if (pair) return pair->value; 

  // now try upvals
  pair = hkl_hash_search(scope->upvals, key);
  if (pair) return pair->value;

  // Now try scopes above

  HklListNode* node = HKLR.scopes->tail;
  node = node->last;
  while(node != NULL)
  {
    scope = ((HklScope*) node->data);
    // check locals first then upvals
    pair = hkl_hash_search(scope->locals, key);
    if (!pair) pair = hkl_hash_search(scope->upvals, key);    

    // if you find the object 
    // make it an upval to my scope
    if (pair)
    {
      hklr_upval_insert(key, pair->value);
      return pair->value;
    }

    node = node->last;
  }

  // Finally try global scope
  pair = hkl_hash_search(HKLR.globals, key);
  if (pair)
  {
    hklr_upval_insert(key, pair->value);
    return pair->value;
  }

  // Didn't find it, make a nil object
  HklrObject* object = hklr_object_new(HKL_TYPE_NIL, HKL_FLAG_NONE);
  hklr_local_insert(key, object);
  return object;
}
Exemplo n.º 2
0
HklObject* hklr_search(HklString* key)
{
  assert(key != NULL);

  HklScope* scope = HKLR.scopes;
  HklPair* pair = NULL;

  // check your scope first
  pair = hkl_hash_search(scope->locals, key);
  if (pair) return pair->value; 

  // now try upvals
  pair = hkl_hash_search(scope->upvals, key);
  if (pair) return pair->value;

  // Now try scopes above
  scope = scope->prev;
  while(scope != NULL)
  {
    // check locals first then upvals
    pair = hkl_hash_search(scope->locals, key);
    if (!pair) pair = hkl_hash_search(scope->upvals, key);    

    // if you find the object 
    // make it an upval to my scope
    if (pair)
    {
      hklr_upval_insert(key, pair->value);
      return pair->value;
    }
    scope = scope->prev;
  }

  // Finally try global scope
  pair = hkl_hash_search(HKLR.globals, key);
  if (pair)
  {
    hklr_upval_insert(key, pair->value);
    return pair->value;
  }

  return NULL;
}