def functor(self, object): if object.isStruct(): functor = getFunctor(object) if functor == ATOM_FUNCTOR: return None elif functor == NULLARY_FUNCTOR: functor = object.getTerm(0).toString() return functor elif object.isList(): return None else: raise ConstructorValueException("Not a compound object")
def createVariable(self, start, end, string): name = str(string) if name.startswith("$$"): raise ConstructorValueException( "Cannot create an ICL variable equivalent to a captured variable" ) minusDollar = name[1:] stripped = minusDollar.lstrip( "_") # look for first character after $_* if stripped == "" or stripped[0] not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": return IclVar("_" + minusDollar) else: return IclVar(minusDollar)
def components(self, obj): if obj.isList(): return [x for x in obj.iterator()] elif obj.isStruct(): functor = getFunctor(obj) if functor == ATOM_FUNCTOR: return None elif functor == NULLARY_FUNCTOR: return () else: return [x for x in obj.iterator()] else: raise ConstructorValueException("Not a compound object")
def asString(self, obj): if obj.isStr(): return obj.toUnquotedString() elif obj.isVar(): name = iclVarName(obj) stripped = name.lstrip("_") if stripped == "" or stripped[ 0] not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": return '$' + name[1:] else: return '$' + name elif obj.isStruct(): functor = getFunctor(obj) args = [icl_to_value(x) for x in obj.iterator()] if functor == ATOM_FUNCTOR and obj.getNumChildren() == 1: return obj.getTerm(0).toUnquotedString() elif obj.isIclDataQ(): return dataq_to_string(obj, None)[1] else: raise ConstructorValueException( "Not a String, Variable, or Symbol")
def category(self, object): if object.isStr(): return self.STRING elif object.isInt(): return self.INTEGER elif object.isFloat(): return self.FLOAT elif object.isStruct(): functor = getFunctor(object) if functor == ATOM_FUNCTOR: return self.SYMBOL else: return self.STRUCTURE elif object.isList(): return self.LIST elif object.isVar(): return self.VARIABLE elif object.isIclDataQ(): return self.STRING else: raise ConstructorValueException("Not a valid object")
def asBigInteger(self, obj): raise ConstructorValueException("Cannot handle big integers")
def asInteger(self, obj): try: i = obj.toInt() return int(i) except: raise ConstructorValueException("Not a valid integer: %r" % obj)
def asFloat(self, obj): try: return obj.toFloat() except: raise ConstructorValueException("Not a Float: %r" % obj)
def createBigInteger(self, start, end, integer): raise ConstructorValueException("Cannot handle big integers")