Пример #1
0
int node_compare(ast_node_t tok1, ast_node_t tok2) {
	if(tok1==tok2) {
		return 0;
	}
	if(isNil(tok1)) {
		return isNil(tok2)?0:-1;
	} else if(isNil(tok2)) {
		return 1;
	} else if(isAtom(tok1)) {
		return isPair(tok2)
			? 1
			: isAtom(tok2)
				? strcmp(node_compare_tag(Value(tok1)), node_compare_tag(Value(tok2)))
				: 0;
	} else if(isPair(tok1)) {
		if(isPair(tok2)) {
			int ret = node_compare(Car(tok1), Car(tok2));
			return ret?ret:node_compare(Cdr(tok1), Cdr(tok2));
		} else {
			return 1;
		}
	}

	return tok1>tok2?1:-1;
}
Пример #2
0
static void formatNode( OUTPUTFORMATTER* out, int aNestLevel, int aCtl,
        const std::string& aKey, CPTREE& aTree )
    throw( IO_ERROR )
{
    if( !isAtom( aTree ) )     // is a list, not an atom
    {
        int ctl = CTL_OMIT_NL;

        // aTree is list and its first child is a list
        if( aTree.size() && !isAtom( aTree.begin()->second ) && !aTree.data().size() )
            ctl = 0;

        out->Print( aNestLevel, "(%s%s", out->Quotes( aKey ).c_str(), ctl & CTL_OMIT_NL ? "" : "\n" );

        if( aTree.data().size() )   // only xml typically uses "data()", not sexpr.
        {
            out->Print( 0, " %s%s",
                out->Quotes( aTree.data() ).c_str(),
                aTree.size() ? "\n" : ""
                );
        }

        formatList( out, aNestLevel, aCtl, aTree );

        out->Print( 0, ")%s", aCtl & CTL_OMIT_NL ? "" : "\n" );
    }

    else            // is an atom, not a list
    {
        const char* atom = out->Quotes( aKey ).c_str();
        out->Print( 0, " %s", atom );
    }
}
Пример #3
0
RSExport RSArrayRef RSQueueCopyCoreQueueUnsafe(RSQueueRef queue) {
    if (!queue) return nil;
    __RSGenericValidInstance(queue, _RSQueueTypeID);
    if (isAtom(queue)) RSSpinLockLock(&queue->_lock);
    RSArrayRef results = RSRetain(queue->_queueCore);
    if (isAtom(queue)) RSSpinLockUnlock(&queue->_lock);
    return results;
}
Пример #4
0
RSExport RSArrayRef RSQueueCopyCoreQueueSafe(RSQueueRef queue) {
    if (!queue) return nil;
    __RSGenericValidInstance(queue, _RSQueueTypeID);
    if (isAtom(queue)) RSSpinLockLock(&queue->_lock);
    RSArrayRef results = RSCopy(RSAllocatorSystemDefault, queue->_queueCore);
    if (isAtom(queue)) RSSpinLockUnlock(&queue->_lock);
    return results;
}
Пример #5
0
RSExport RSIndex RSQueueGetCount(RSQueueRef queue)
{
    if (queue == nil) return 0;
    __RSGenericValidInstance(queue, _RSQueueTypeID);
    if (isAtom(queue)) RSSpinLockLock(&queue->_lock);
    RSIndex cnt = RSArrayGetCount(queue->_queueCore);
    if (isAtom(queue)) RSSpinLockUnlock(&queue->_lock);
    return cnt;
}
Пример #6
0
Query QueryTree::packToQuery() const
{
	// WARN TODO What if childrens are empty?
	std::string query;

	std::string delim;

	if (op == QueryOperator::Not) {
		return "! " + children[0]->packToQuery().getText();
	} 
		
	switch (op) {
		case QueryOperator::And: delim = " "; break;
		case QueryOperator::Or: delim = " | "; break;		
		default: break;
	}

	if (!isAtom()) {
		query = children[0]->packToQuery().getText();

		for (auto it = std::begin(children) + 1; it != std::end(children); ++it) {
			const auto& c = *it;
			query += delim + c->packToQuery().getText();
		}
	} else {
		return word;	
	}

	return query;
}
Пример #7
0
std::pair<CDBTRule::cards_type, int> CDBTRule::getType(const std::vector<int>& cards)
{
	if (isSingle(cards))
		return std::make_pair(type_singal, getWeight(cards[0]));
	else if (isPairs(cards))
		return std::make_pair(type_pairs, getWeightNoRedFive(cards[1]));
	else if (isthree(cards))
		return std::make_pair(type_three, getWeightNoRedFive(cards[2]));
	else if (isBoom(cards))
		return std::make_pair(type_boom, getWeightNoRedFive(cards[cards.size() - 1]));
	else if (isAtom(cards))
		return std::make_pair(type_atom, getWeight(cards[cards.size() - 1]));
	else if (isSister(cards)) {
		//带A
		if (getValue(cards[0]) == 0)
			return std::make_pair(type_sister, getWeightNoRedFive(cards[1]));
		return std::make_pair(type_sister, getWeightNoRedFive(cards[cards.size() - 1]));
	}
	else if (isPlane(cards)) {
		//带A
		if (getValue(cards[0]) == 0)
			return std::make_pair(type_plane, getWeightNoRedFive(cards[2]));
		return std::make_pair(type_plane, getWeightNoRedFive(cards[cards.size() - 1]));
	}
	else if (isThreetwo(cards)){
		//AAABB
		if (getValue(cards[1]) == getValue(cards[2])){
			return std::make_pair(type_three, getWeightNoRedFive(cards[2]));
		}
		//AABBB
		return std::make_pair(type_three, getWeightNoRedFive(cards[4]));
	}
	return std::make_pair(type_unknow, 0);
}
Пример #8
0
static void formatList( OUTPUTFORMATTER* out, int aNestLevel, int aCtl, CPTREE& aTree )

{
    for( CITER it = aTree.begin();  it != aTree.end();  ++it )
    {
        // Processing a tree which was read in with xml_parser?
        if( it->first == "<xmlattr>" )
        {
            formatList( out, aNestLevel, aCtl | CTL_IN_ATTRS, it->second );
            continue;
        }

        int ctl = 0;

        if( isLast( aTree, it ) )   // is "it" the last one?
        {
            //if( !( aCtl & CTL_IN_ATTRS ) )
                ctl = CTL_OMIT_NL;
        }
        else if( isAtom( next( it )->second ) )
        {
            /* if( !( aCtl & CTL_IN_ATTRS ) ) */
                ctl = CTL_OMIT_NL;
        }

        formatNode( out, aNestLevel+1, ctl, it->first, it->second );
    }
}
Пример #9
0
bool CDBTRule::isthree(const std::vector<int>& cards)
{
	if (cards.size() != 3 || isAtom(cards) || isJoker(cards[0]))
		return false;

	if (getValue(cards[0]) == getValue(cards[1]) && getValue(cards[0]) == getValue(cards[2]))
		return true;
	return false;
}
Пример #10
0
	QByteArray toArray() const
	{
		int idx = _index; QByteArray a(_size, 0);
		if (isAtom())
			ei_decode_atom(_buf, &idx, a.data());
		else
			ei_decode_string(_buf, &idx, a.data());

		return a;
	}
Пример #11
0
SExpr& SExpr::operator=(const SExpr& other) {
  d_sexprType = other.d_sexprType;
  d_integerValue = other.d_integerValue;
  d_rationalValue = other.d_rationalValue;
  d_stringValue = other.d_stringValue;

  if(d_children == NULL && other.d_children == NULL){
    // Do nothing.
  } else if(d_children == NULL){
    d_children = new SExprVector(*other.d_children);
  } else if(other.d_children == NULL){
    delete d_children;
    d_children = NULL;
  } else {
    (*d_children) = other.getChildren();
  }
  Assert( isAtom() == other.isAtom() );
  Assert( (d_children == NULL) == isAtom() );
  return *this;
}
Пример #12
0
SExpr::SExpr(const SExpr& other) :
    d_sexprType(other.d_sexprType),
    d_integerValue(other.d_integerValue),
    d_rationalValue(other.d_rationalValue),
    d_stringValue(other.d_stringValue),
    d_children(NULL)
{
  d_children = (other.d_children == NULL) ? NULL : new SExprVector(*other.d_children);
  // d_children being NULL is equivalent to the node being an atom.
  Assert( (d_children == NULL) == isAtom() );
}
Пример #13
0
bool CDBTRule::isBoom(const std::vector<int>& cards)
{
	if (cards.size() < 4 || cards.size() > 16 || isAtom(cards) || isJoker(cards[0]))
		return false;

	int val = getValue(cards[0]);
	for (size_t i = 1; i < cards.size(); i++) {
		if (getValue(cards[i]) != val)
			return false;
	}
	return true;
}
Пример #14
0
void destroy_list (Node *n)
{
    
    if (isNil(n))
	;
    else if (isAtom(n)){
	free(atomVal(n)); /* free the atom here */
        free_node(n);
    } else {
	destroy_list(car(n));
	destroy_list(cdr(n));
	free_node(n);
    }
}
Пример #15
0
void ast_ser_list(const ast_node_t ast,int(*func)(int,void*),void*param) {
	/* FIXME shouldn't happen */
	if(isAtom(ast)) {
		ast_serialize(ast,func,param);
		return;
	}

	if(getCar(ast)) {
		ast_serialize(getCar(ast),func,param);
	}
	if(getCdr(ast)) {
		func(' ',param);
		ast_ser_list(getCdr(ast),func,param);
	}
}
Пример #16
0
RSInline void __RSQueueAddToObject(RSQueueRef queue, RSTypeRef obj)
{
    BOOL shouldLocked = isAtom(queue);
    if (shouldLocked) RSSpinLockLock(&queue->_lock);
    if (queue->_capacity == 0)
    {
        RSArrayAddObject(queue->_queueCore, obj);
        if (shouldLocked) RSSpinLockUnlock(&queue->_lock);
        return;
    }
    RSIndex cnt = RSArrayGetCount(queue->_queueCore);
    if (cnt < queue->_capacity)
        RSArrayAddObject(queue->_queueCore, obj);
    else
        __RSCLog(RSLogLevelNotice, "RSQueue %p is full!\n", queue); 
    if (shouldLocked) RSSpinLockUnlock(&queue->_lock);
}
Пример #17
0
RSInline RSTypeRef __RSQueueGetObjectFromQueue(RSQueueRef queue, BOOL remove)
{
    BOOL shouldLocked = isAtom(queue);
    if (shouldLocked) RSSpinLockLock(&queue->_lock);
    RSIndex cnt = RSArrayGetCount(queue->_queueCore);
    if (cnt == 0)
    {
        if (shouldLocked) RSSpinLockUnlock(&queue->_lock);
        return nil;
    }
    RSTypeRef obj = RSArrayObjectAtIndex(queue->_queueCore, 0);
    if (remove)
    {
        RSRetain(obj);
        RSArrayRemoveObjectAtIndex(queue->_queueCore, 0);
    }
    if (shouldLocked) RSSpinLockUnlock(&queue->_lock);
    return obj;
}
Пример #18
0
std::string QueryTree::toString() const
{
	if (isAtom()) {
		return word;
	}

	std::string operation;
	switch (op) {
		case QueryOperator::Not: operation = "not"; break;
		case QueryOperator::And: operation = "or"; break;
		case QueryOperator::Or: operation = "and"; break;		
		default: break;
	}

	std::string query = "(" + operation;
	for (const auto& c: children) {
		query += " " + c->toString();
	}
	query += ")";
	return query;
}
Пример #19
0
std::string SExpr::getValue() const {
  PrettyCheckArgument(isAtom(), this);
  switch (d_sexprType) {
    case SEXPR_INTEGER:
      return d_integerValue.toString();
    case SEXPR_RATIONAL: {
      // We choose to represent rationals as decimal strings rather than
      // "numerator/denominator."  Perhaps an additional SEXPR_DECIMAL
      // could be added if we need both styles, even if it's backed by
      // the same Rational object.
      std::stringstream ss;
      ss << std::fixed << d_rationalValue.getDouble();
      return ss.str();
    }
    case SEXPR_STRING:
    case SEXPR_KEYWORD:
      return d_stringValue;
    case SEXPR_NOT_ATOM:
      return std::string();
  }
  return std::string();
}
Пример #20
0
void ast_serialize(const ast_node_t ast,int(*func)(int,void*),void*param) {
	char*srcptr;
	/* if ast is nil, output '()' */
	/*inside_lisp = 1;*/
	if(ast==PRODUCTION_OK_BUT_EMPTY) {
		func('E', param);
		func('M', param);
		func('P', param);
		func('T', param);
		func('Y', param);
		func(0, param);
		return;
	}
	if(!ast) {
		func('(',param);
		func(')',param);
	/* if ast is pair, serialize list */
	} else if(isPair(ast)) {
		func('(',param);
		if(ast->node_flags&IS_FOREST) {
		func('@',param);
		func(' ',param);
		}
		ast_ser_list(ast,func,param);
		func(')',param);
	/* if ast is atom, output atom */
	} else if(isAtom(ast)) {
		srcptr=regstr(getAtom(ast));
		while(*srcptr!=0) {
			escape_chr(&srcptr,func,param, _LISP);
		}
/*		*output+=strlen(getAtom(ast));*/
	}
	func('\0',param);
	/*inside_lisp = 0;*/
}
Пример #21
0
const std::vector<SExpr>& SExpr::getChildren() const {
  PrettyCheckArgument( !isAtom(), this );
  Assert( d_children != NULL );
  return *d_children;
}
Пример #22
0
static inline int
is_key(word w)
{ return isAtom(w) || isTaggedInt(w);
}
Пример #23
0
int
isReservedSymbol(word w)
{ return isAtom(w) && atomValue(w)->type == &reserved_symbol;
}
Пример #24
0
void scanXDBinputText(xformDatabase* xdb, FILE *inf, FILE *outf,
	    abrMkAtomProc mkAtom, void *atomstuff, char *cch) {
   transformData* xform = NULL;
   char *s = NULL;
   char saynull[] = "NULL:identity transformation:";

   while(readRecord(inf, XFworkBuf, MAX_XF_REC_LEN) >= 0) {
      if (isAtom(XFworkBuf) || isHet(XFworkBuf)) {
	 if (! xform) { /* make someplace to hang these atoms */
	    xform = newTransformation(saynull);
	    appendTransformation(xdb, xform);
	 }
	 if (! xform) {
	    errmsg("could not create Identity transformation - Atom skipped");
	 }
	 else if (isPseudoAtom(XFworkBuf)) { /* ignore pseudo atoms */ }
	 else {
	    appendAtomRec(xform, XFworkBuf, mkAtom, atomstuff);
	 }
      }
      else {
	 s = XFworkBuf + strspn(XFworkBuf, " \t\n\r");
	 if (!strncasecmp(s, "BONDROT",7)
	  || !strncasecmp(s, "ROT",  3)
	  || !strncasecmp(s, "TRANS",5)
	  || !strncasecmp(s, "SAVE", 4)   || (s[0] == '(')
	  || !strncasecmp(s, "RESTORE",7) || (s[0] == ')') ) {
	    xform = newTransformation(s);
	    appendTransformation(xdb, xform);
	 }
	 else if (!strncasecmp(s, "GO", 2)) {
	    addGoToRecord(xdb, s);
	 }
	 else if (!strncasecmp(s, "COS", 3)
	       || !strncasecmp(s, "POLY",   4)
	       || !strncasecmp(s, "CONST",  5) ) {
	    if (xform) {
	       appendBiasFunction(xform, s);
	    }
	    else {
	       warn("no transformation to attach bias function:");
	       warn(s);
	    }
	 }
	 else if (s[0] == '#' || s[0] == '\0') {
	       /* comments are ok - we do nothing */
	 }
	 else if (s[0] == '@') { /* include file */
	    FILE *if2 = NULL;
	    if2 = fopen(s+1, "r");
	    if (if2) { /* recursive call */
	       fprintf(outf, "%s%s\n", cch, s);

	       scanXDBinputText(xdb, if2, outf, mkAtom, atomstuff, cch);
	       fclose(if2);
	    }
	    else {
	       warn("could not open include file:");
	       warn(s+1);
	    }
	 }
	 else {
	    warn("unknown record type:");
	    warn(s);
	 }
      }
   }
}