Пример #1
0
ATerm SSL_fdcopy(ATerm fdinA, ATerm fdoutA)
{
  int fdin, fdout;
  int n; 
  char buf[SSL_COPY_BUFSIZE];

  if(ATgetType(fdinA) != AT_INT || ATgetType(fdinA) != AT_INT)
    _fail(fdinA);

  fdin = ATgetInt((ATermInt)fdinA);
  fdout = ATgetInt((ATermInt)fdoutA);

  while( (n = read(fdin, buf, SSL_COPY_BUFSIZE)) > 0 )
    if(write(fdout, buf, n) != n)
      { 
	ATfprintf(stderr, "SSL_fdcopy: write error\n");
	_fail((ATerm) ATempty);
      }

  if(n < 0)
    {
      ATfprintf(stderr, "SSL_fdcopy: read error\n");
      _fail((ATerm) ATempty);
    }

  return (ATerm) ATempty;
}
Пример #2
0
int intCompare(const ATerm aVal1, const ATerm aVal2)
{
  int x, y;

  x = ATgetInt((ATermInt)aVal1); y = ATgetInt((ATermInt)aVal2);
  if(x < y) return -1;
  if(x > y) return 1;
  return 0;
}
Пример #3
0
ATermInt ATR_average(ATSet set) {
  int sum = ATgetInt(ATR_sum(set));
  int count = ATgetInt(ATR_cardinality(set));
  int avg = 0;
  if (count > 0) {
    avg = sum/count;
  }
  return ATmakeInt(avg);
}
Пример #4
0
static void writeInteger(A2PWriter writer, ATermInt integer){
	int intValue = ATgetInt(integer);
	
	writeByteToBuffer(writer->buffer, PDB_INTEGER_HEADER);
	
	printInteger(writer->buffer, intValue);
}
Пример #5
0
/* For all non-terminals which are to the right of the dot in |item|, add 
 * that non-terminal's items to the set of |items| if they do not already 
 * exist. Note that only items that do not cause a priority conflict are 
 * added to the predicted items. */
static ATermList predict(Item item, ATermList items) {
  int prodnr;
  PT_Symbol dotsymbol;
  ATermList prods;

  if (IT_isValidItem(item)) {
    dotsymbol = (PT_Symbol)IT_getDotSymbol(item);
    if (!PT_isSymbolEmpty(dotsymbol)) {
      prods = PGEN_getProductionNumbersOfRhsNonTerminal(dotsymbol);
      if (prods) {
        while (!ATisEmpty(prods)) {
          /*ATwarning("prods = %t\n", prods);*/
          prodnr = ATgetInt((ATermInt)ATgetFirst(prods));
          prods = ATgetNext(prods);
          if (!PGEN_isPriorityConflict(item, prodnr)) {
            Item newitem = IT_createItem(prodnr);
            items = ATinsert(items, IT_ItemToTerm(newitem));
          }
        }
      }
    }
  }

  return items;
}
Пример #6
0
ATerm SSL_setenv(ATerm name, ATerm value, ATerm overwrite)
{
  if(!t_is_string(name)) _fail(name);
  if(!t_is_string(value)) _fail(value);
  if(!ATisInt(overwrite)) _fail(overwrite);

  setenv(ATgetName(ATgetSymbol(name)), ATgetName(ATgetSymbol(value)), ATgetInt((ATermInt)overwrite));

  return (ATerm)ATempty;
}
Пример #7
0
ATRelation ATR_transitiveClosure(ATRelation relation) {
  int i;
  ATRelation cursor = relation;
  ATRelation closure = relation;
  for (i = 0; i < ATgetInt(ATR_cardinality(relation)) - 1; i++) {
    cursor = ATR_compose(cursor, relation);
    closure = ATR_union(closure, cursor);
  }
  return closure;
}
Пример #8
0
ATermInt ATR_sum(ATSet set) {
  ATIterator iter = ATR_getIterator(set);
  int n = 0;
  while (!ATR_isEmpty(iter)) {
    ATerm head = ATR_getHead(iter);
    check_type_is_int_else_abort(head, "ATR_sum");
    n += ATgetInt(((ATermInt)head));
    iter = ATR_getTail(iter);
  }
  return ATmakeInt(n);
}
Пример #9
0
ATermInt ATR_max(ATSet set) {
  ATIterator iter = ATR_getIterator(set);
  int max = 0;
  int n = 0;
  while (!ATR_isEmpty(iter)) {
    ATerm head = ATR_getHead(iter);
    check_type_is_int_else_abort(head, "ATR_max");
    n = ATgetInt(((ATermInt)head));
    max = max > n ? max : n;
    iter = ATR_getTail(iter);
  }
  return ATmakeInt(max);
}
Пример #10
0
/* The compare function creates a sorted tree, so an in-order walk should
 * return the same set of values in increasing order */
void test_walk(ATermBBTree tree, const int *pValues)
{
  ATermBBTreeIterator iter;
  int i;

  iter = ATbbtreeIteratorInit(tree);
  
  while(!ATbbtreeIteratorAtEnd(iter) && *pValues) {
    i = ATgetInt((ATermInt)ATbbtreeIteratorValue(iter));
    assert(i == *pValues);
    iter = ATbbtreeIteratorAdvance(iter); ++pValues;
  }
  /* they should both end at the same time */
  assert(ATbbtreeIteratorAtEnd(iter));
  assert(! *pValues);
}
Пример #11
0
ATermList filter_multiples(int n, ATermList numbers)
{
  int i, nr, len = ATgetLength(numbers);
  ATerm el;

  for(i=0; i<len; i++) {
    el = ATelementAt(numbers, i);
    nr = ATgetInt((ATermInt)el);
    if(nr % n == 0) {
      len--;
      numbers = ATremoveElementAt(numbers, i);
    }
  }

  return numbers;
}
Пример #12
0
ATermInt ATR_min(ATSet set) {
  ATIterator iter = ATR_getIterator(set);
  int min = INT_MAX;
  int n = 0;
  while (!ATR_isEmpty(iter)) {
    ATerm head = ATR_getHead(iter);
    check_type_is_int_else_abort(head, "ATR_min");
    n = ATgetInt(((ATermInt)head));
    min = min < n ? min : n;
    iter = ATR_getTail(iter);
  }
  if (min == INT_MAX) {
    min = 0;
  }
  return ATmakeInt(min);
}
Пример #13
0
int HFdecodeIndex(BitStream *fp, HFtree *tree, long *index){
   Bit bit;
   struct HFnode *current;
   ATerm term;


   current=tree->codes;
   while(current!=NULL){

      if(current->high==NULL && current->low==NULL){
         term=current->term;
         if(term==ESCAPE_SEQUENCE){
/* OEPS
            HFupdate(tree,current);
*/
            if(LZreadInt(fp,&tree->buffer,index)){
               term=(ATerm)ATmakeInt(*index);
               current=HFadd(tree,term);
               HFupdate(tree,current);
               return *index!=NO_INT;
            } else {
               return 0;
            }
         } else {
            HFupdate(tree,current);
            *index=ATgetInt((ATermInt)term);
            return *index!=NO_INT;
         }
      } else {
         if(BSreadBit(fp, &bit)==1){
            if(bit==0){
              current = current->low;
            } else {
              current = current->high;
            }
         } else {
            return 0;
         }
      }
   }

   return 1;
}
Пример #14
0
ATermList filter_non_primes(ATermList numbers)
{
  ATermList primes = ATmakeList0();

  /* Skip 1, we dont want to filter that! */
  numbers = ATgetNext(numbers);

  while(!ATisEmpty(numbers)) {
    /* The first number must be prime. remove it from numbers. */
    ATerm prime = ATgetFirst(numbers);

    /* Remove all multiples of n, because they cannot be prime! */
    numbers = filter_multiples(ATgetInt((ATermInt)prime), numbers);
    /*ATprintf("%d numbers.\n", ATgetLength(numbers));*/

    /* Now add n to the list of primes */
    primes = ATappend(primes, prime);
  }

  return (ATermList)ATmake("[1,<list>]", primes);
}
Пример #15
0
static ATerm substitute_rec(ATerm t, ATermSubst sigma, ATbool *nonempty) {
  /* invariants: 
     - Subst contains pairs (r , r^sigma)
     - *nonempty iff Subst is not empty 
  */
  ATerm s;
  if (ATisVariable(t)) {
    s=ATstackGet(sigma,ATgetInt((ATermInt)t));
    return (s ? s : t);
  }
  else if (*nonempty && (s=ATtableGet(Subst,t))) 
    return s;
  else {
    Symbol sym = ATgetSymbol(t);
    int i,n=ATgetArity(sym);
    ATerm* args = (ATerm*)alloca(n*sizeof(ATerm));
    for (i=0;i<n;i++)
      args[i]=substitute_rec(ATgetArgument(t,i),sigma,nonempty);
    s = (ATerm)ATmakeApplArray(sym,args);
    *nonempty=ATtrue;
    ATtablePut(Subst,t,s);
    return s;
} }
Пример #16
0
unsigned int calc_hash(ATerm t)
{
  unsigned int hnr = 0;

  switch(ATgetType(t)) {
    case AT_APPL:
      {
	ATermAppl appl = (ATermAppl)t;
	AFun sym = ATgetAFun(appl);
	int i, arity = ATgetArity(sym);
	hnr = AT_hashSymbol(ATgetName(sym), arity);

	for(i=0; i<arity; i++) {
	  hnr = hnr * MAGIC_HASH_CONST_APPL + calc_hash(ATgetArgument(appl, i));
	}
      }
      break;

    case AT_INT:
      hnr = ATgetInt((ATermInt)t);
      break;

    case AT_LIST:
      {
	ATermList list = (ATermList)t;
	hnr = 123;
	while(!ATisEmpty(list)) {
	  hnr = hnr * MAGIC_HASH_CONST_LIST + 
	    calc_hash(ATgetFirst(list));
	  list = ATgetNext(list);
	}
      }
      break;
  }

  return hnr;
}
Пример #17
0
static char unfold_solution(ATermSubst sigma) {
  /* - makes assigned, solution and equivalence empty
     - doesn't alter equivalence, except shortening find paths
     - returns 1: unifiable; sigma contains solution (except when NULL)
     - returns 0: not unifiable; sigma is empty
     Note: unfolding is also needed if sigma=NULL to detect loops.
  */
  int i;
  ATerm t;
  char unifiable=1;
  static char first_call=1;
  if (first_call) {
    first_call = 0;
    loop_detection=ATindexedSetCreate(64,50);
    solution = ATtableCreate(64,50);
  }
  assert(ATisEmpty(ATtableKeys(solution)));
  for (i=ATstackDepth(assigned)-1;i>=0;i--) {
    ATerm x = ATstackPop(assigned);
    assert(ATisEmpty(ATindexedSetElements(loop_detection)));
    t = unfold_rec(find(x));
    if (t && sigma) 
      ATstackSet(sigma,ATgetInt((ATermInt)x),t);
    else {
      unifiable=0;
      break;
  } }
  ATtableReset(solution);
  ATtableReset(equivalence);
  if (unifiable)
    return ATtrue;
  else {
    ATstackReset(assigned);
    if (sigma) ATstackReset(sigma);
    return ATfalse;
} }
Пример #18
0
static void term2buf(ATerm t)
{
    ATerm annos = AT_getAnnotations(t);
    if(annos != NULL) {
        char2buf('{');
    }

    switch(ATgetType(t)) {
    case AT_INT:
        wprintf("%d", ATgetInt((ATermInt)t));
        break;
    case AT_REAL:
        wprintf("%f", ATgetReal((ATermReal)t));
        break;
    case AT_APPL:
    {
        int cur_arg, arity;
        ATermAppl appl = (ATermAppl)t;
        AFun sym = ATgetSymbol(appl);

        if(ATisQuoted(sym))
            qstr2buf(ATgetName(sym));
        else
            str2buf(ATgetName(sym));

        arity = ATgetArity(sym);
        if(arity > 0) {
            char2buf('(');

            for(cur_arg=0; cur_arg<arity; cur_arg++) {
                term2buf(ATgetArgument(appl, cur_arg));
                if(cur_arg < (arity-1))
                    char2buf(',');
            }
            char2buf(')');
        }
    }
    break;
    case AT_LIST:
    {
        ATermList l = (ATermList)t;
        char2buf('{');
        while(!ATisEmpty(l)) {
            ATerm el = ATgetFirst(l);
            l = ATgetNext(l);
            term2buf(el);
            if(!ATisEmpty(l))
                char2buf(' ');
        }
        char2buf('}');
    }
    break;

    case AT_PLACEHOLDER:
    {
        char2buf('<');
        term2buf(ATgetPlaceholder((ATermPlaceholder)t));
        char2buf('>');
    }
    break;

    case AT_BLOB:
        ATerror("blobs are not supported by tcltk-adapter!\n");

    default:
        ATabort("illegal term type!\n");
    }

    if(annos != NULL) {
        char2buf(' ');
        term2buf(annos);
        char2buf('}');
    }
}
Пример #19
0
int SG_Batch (int argc, char **argv)
{
  ATerm      parse_tree;

  parse_tree = SGparseFileUsingTable(program_name, 
                                     parse_table_name, 
                                     start_symbol,
                                     input_file_name, 
                                     output_file_name);
  if (!SG_OUTPUT) {
    return 0;
  }

  if (!parse_tree) {
    ATwarning("%s: error in %s: unexpected error\n",
              program_name, input_file_name);
    return 2;
  }

  if (SGisParseError(parse_tree)) {
    ATermList errlist;
    ATerm     errcode;
    AFun      err;
    int      c, line, col;

    errlist = (ATermList) ATgetArgument((ATermAppl) parse_tree, 0);
    errcode = ATgetArgument((ATermAppl) parse_tree, 1);
    c    = ATgetInt((ATermInt) ATgetArgument(ATelementAt(errlist, 0), 0));
    line = ATgetInt((ATermInt) ATgetArgument(ATelementAt(errlist, 1), 0));
    col  = ATgetInt((ATermInt) ATgetArgument(ATelementAt(errlist, 2), 0));
    err  = ATgetAFun(errcode);

    if (err == SG_EOF_Error_AFun) {
      ATwarning("%s: error in %s, line %d, col %d: end of file unexpected\n",
                program_name, input_file_name, line, col);
    }
    else if (err == SG_Plain_Error_AFun) {
      if (isprint(c)) {
        ATwarning("%s: error in %s, line %d, col %d: character `%c' (\\x%2.2x)"
                  " unexpected\n",
                  program_name, input_file_name, line, col, c, c);
      }
      else {
        ATwarning("%s: error in %s, line %d, col %d: character \\x%2.2x"
                  " unexpected\n",
                  program_name, input_file_name, line, col, c);
      }
    } 
    else if (err == SG_Cycle_Error_AFun) {
      ATwarning("%s: error in %s, line %d, col %d: cycle detected, productions: %t\n",
                program_name, 
                input_file_name, 
                line, 
                col, 
                ATgetArgument(errcode, 0));
    }
    else if (err == SG_Amb_Error_AFun) {
      int ambiescount = ATgetInt((ATermInt) ATgetArgument(errcode,0));
      ATwarning("%s: error in %s, line %d, col %d: cannot represent %d ambiguit%s\n",
                program_name, 
                input_file_name, 
                line, 
                col, 
                ambiescount,
                (ambiescount > 1) ? "ies" : "y" );
    }
    else if (err == SG_Too_Many_Ambiguities_Error_AFun) {
      ATwarning("%s: error in %s, line %d, col %d: too many ambiguities\n",
                program_name, input_file_name, line, col);
    }
    else {
      ATwarning("%s: error in %s, line %d, col %d: unknown error\n",
                program_name, input_file_name, line, col);
    }
    return 1;
  }
  else if(!SGisParseTree(parse_tree)) {
    ATwarning("%s: error: neither parse tree nor parse error for %s\n",
              program_name, input_file_name);
    return 1;
  }

  IF_VERBOSE(
    int  nrambs;
    char *sort;

    nrambs = SGnrAmb(SG_NR_ASK);
    sort   = SGsort(SG_GET, NULL);
    ATwarning("%s: %s parsed %s as sort %s, with %d ambiguit%s\n",
              program_name, parse_table_name, input_file_name,
              sort ? sort : "[undetermined]",
              nrambs, (nrambs==1)?"y":"ies");
  );