Ejemplo n.º 1
0
Archivo: integer.c Proyecto: onmsr/slim
/*
 * (S, N):
 *
 * N is bound to a number with the string representation S.
 */
void integer_of_string(LmnReactCxt *rc,
                       LmnMembrane *mem,
                       LmnAtom a0, LmnLinkAttr t0,
                       LmnAtom a1, LmnLinkAttr t1)
{
  long n;
  char *t;
  const char *s = (const char *)lmn_string_c_str(LMN_STRING(a0));
  t = NULL;
  n = strtol(s, &t, 10);
  if (t == NULL || s == t) {
    LmnSAtom a = lmn_mem_newatom(mem, lmn_functor_intern(ANONYMOUS,
                                                         lmn_intern("fail"),
                                                         1));
    lmn_mem_newlink(mem,
                    a1, t1, LMN_ATTR_GET_VALUE(t1),
                    LMN_ATOM(a), LMN_ATTR_MAKE_LINK(0), 0);
  } else { /* 変換できた */
    lmn_mem_newlink(mem,
                    a1, t1, LMN_ATTR_GET_VALUE(t1),
                    n, LMN_INT_ATTR, 0);
    lmn_mem_push_atom(mem, n, LMN_INT_ATTR);
  }

  lmn_mem_delete_atom(mem, a0, t0);
}
Ejemplo n.º 2
0
Archivo: integer.c Proyecto: onmsr/slim
/*
 * (N, H):
 *
 * H is bound to a random number between 0 and N-1.
 */
void integer_rand(LmnReactCxt *rc,
                  LmnMembrane *mem,
                  LmnAtom a0, LmnLinkAttr t0,
                  LmnAtom a1, LmnLinkAttr t1)
{
  LmnWord n = rand() % a0;

  lmn_mem_newlink(mem,
                  a1, LMN_ATTR_MAKE_LINK(0), LMN_ATTR_GET_VALUE(t1),
                  n, LMN_INT_ATTR, 0);
  lmn_mem_push_atom(mem, n, LMN_INT_ATTR);

  lmn_mem_delete_atom(mem, a0, t0);
}
Ejemplo n.º 3
0
Archivo: nlmem.c Proyecto: lmntal/slim
void nlmem_copy(LmnReactCxtRef rc,
                LmnMembraneRef mem,
                LmnAtomRef a0, LmnLinkAttr t0,
                LmnAtomRef a1, LmnLinkAttr t1,
                LmnAtomRef a2, LmnLinkAttr t2)
{
  LmnMembraneRef org_mem, trg_mem;
  ProcessTableRef atom_map;
  lmn_interned_str copy_tag_name;
  LmnFunctor copy_tag_func;

  copy_tag_name = LMN_FUNCTOR_NAME_ID(LMN_SATOM_GET_FUNCTOR((LmnSymbolAtomRef)a1));
  copy_tag_func = lmn_functor_intern(ANONYMOUS, copy_tag_name, 3);
  org_mem = LMN_PROXY_GET_MEM(LMN_SATOM_GET_LINK((LmnSymbolAtomRef)a0, 0));
  trg_mem = lmn_mem_make();
  atom_map = lmn_mem_copy_cells(trg_mem, org_mem);
  lmn_mem_add_child_mem(mem, trg_mem);

  {
    AtomListEntryRef ent = lmn_mem_get_atomlist(org_mem, LMN_IN_PROXY_FUNCTOR);

    if (ent) {
      LmnSymbolAtomRef org_in, org_out, trg_in, trg_out;
      LmnSymbolAtomRef tag_atom;
      LmnWord t = 0;

      EACH_ATOM(org_in, ent, ({
        /* タグアトムを作り、リンクの接続を行う */
        proc_tbl_get_by_atom(atom_map, org_in, &t);
        trg_in = LMN_SATOM(t);
        org_out = LMN_SATOM(LMN_SATOM_GET_LINK(org_in, 0));
        trg_out = lmn_mem_newatom(mem, LMN_OUT_PROXY_FUNCTOR);
        lmn_newlink_in_symbols(trg_in, 0, trg_out, 0);
        tag_atom = lmn_mem_newatom(mem, copy_tag_func);
        lmn_relink_symbols(tag_atom, 2, org_out, 1);
        lmn_newlink_in_symbols(tag_atom, 0, org_out, 1);
        lmn_newlink_in_symbols(tag_atom, 1, trg_out, 1);
      }));
    }

    proc_tbl_free(atom_map);
    lmn_mem_delete_atom(mem, a1, t1);
    /* 第一引数に接続されたタグアトムと第三引数を接続する */
    lmn_mem_newlink(mem,
                    a2, t2, LMN_ATTR_GET_VALUE(t2),
                    LMN_SATOM_GET_LINK((LmnSymbolAtomRef)a0, 1), LMN_SATOM_GET_ATTR((LmnSymbolAtomRef)a0, 1),
                    2);
  }
Ejemplo n.º 4
0
Archivo: integer.c Proyecto: onmsr/slim
/**
 * ($start, $end, $g)
 * where
 *  start, end = integer
 *  g = ground
 *
 * Creates a (multi)set $g[$a], $g[$a+1], ..., $g[$b].
 */
void integer_set(LmnReactCxt *rc,
                 LmnMembrane *mem,
                 LmnAtom a0, LmnLinkAttr t0,
                 LmnAtom a1, LmnLinkAttr t1,
                 LmnAtom a2, LmnLinkAttr t2)
{
  Vector *srcvec;
  int i, j, n;
  int start, end;

  start  = (int)a0;
  end    = (int)a1;
  srcvec = vec_make(16);
  vec_push(srcvec, (LmnWord)LinkObj_make(a2, t2));

  for (i = 0, n = start; n <= end; i++, n++) {
    Vector *dstlovec;
    ProcessTbl atommap;
    LinkObj l;

    lmn_mem_copy_ground(mem, srcvec, &dstlovec, &atommap);

    l = (LinkObj)vec_get(dstlovec, 0);
    lmn_mem_newlink(mem, n, LMN_INT_ATTR, 0,
                    l->ap, t2, LMN_ATTR_GET_VALUE(l->pos));
    lmn_mem_push_atom(mem, n, LMN_INT_ATTR);

    for (j = 0; j < vec_num(dstlovec); j++) LMN_FREE(vec_get(dstlovec, j));
    vec_free(dstlovec);
    proc_tbl_free(atommap);
  }

  lmn_mem_delete_atom(mem, a0, t0);
  lmn_mem_delete_atom(mem, a1, t1);

  lmn_mem_delete_ground(mem, srcvec);

  for (i = 0; i < vec_num(srcvec); i++) LMN_FREE(vec_get(srcvec, i));
  vec_free(srcvec);
}