static void writeConstructor(A2PWriter writer, A2PType expected, ATermAppl constructor){ A2PConstructorType t = (A2PConstructorType) expected->theType; A2PTupleType children = ((A2PTupleType) t->children->theType); int nrOfChildren = typeArraySize(children->fieldTypes); ISIndexedSet sharedTypes = writer->typeSharingMap; int typeHash = hashType(expected); int constructorTypeId = ISget(sharedTypes, (void*) expected, typeHash); int arity = ATgetArity(ATgetAFun(constructor)); int i; if(arity != nrOfChildren){ fprintf(stderr, "Arity (%d) is unequal to the number of children (%d); term was:\n%s\n", arity, nrOfChildren, ATwriteToString((ATerm) constructor)); exit(1);} if(constructorTypeId == -1){ writeByteToBuffer(writer->buffer, PDB_CONSTRUCTOR_HEADER); doWriteType(writer, expected); ISstore(sharedTypes, (void*) expected, typeHash); }else{ writeByteToBuffer(writer->buffer, PDB_CONSTRUCTOR_HEADER | PDB_TYPE_SHARED_FLAG); printInteger(writer->buffer, constructorTypeId); } printInteger(writer->buffer, arity); for(i = 0; i < arity; i++){ doSerialize(writer, children->fieldTypes[i], ATgetArgument(constructor, i)); } }
static void writeNode(A2PWriter writer, A2PType expected, ATermAppl node){ AFun fun = ATgetAFun(node); int arity = ATgetArity(fun); char *name = ATgetName(fun); int i; unsigned int hash = hashString(name); int nodeNameId = ISstore(writer->nameSharingMap, (void*) name, hash); if(nodeNameId == -1){ int nameLength = dataArraySize(name); writeByteToBuffer(writer->buffer, PDB_NODE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); }else{ writeByteToBuffer(writer->buffer, PDB_NODE_HEADER | PDB_NAME_SHARED_FLAG); printInteger(writer->buffer, nodeNameId); } printInteger(writer->buffer, arity); for(i = 0; i < arity; i++){ doSerialize(writer, A2PvalueType(), ATgetArgument(node, i)); } }
static void writeSet(A2PWriter writer, A2PType expected, ATermList set){ A2PSetType setType = (A2PSetType) expected->theType; A2PType elementType = setType->elementType; ISIndexedSet sharedTypes = writer->typeSharingMap; int elementHash = hashType(elementType); int elementTypeId = ISget(sharedTypes, (void*) elementType, elementHash); int size = ATgetLength(set); ATermList next; if(elementTypeId == -1){ writeByteToBuffer(writer->buffer, PDB_SET_HEADER); doWriteType(writer, elementType); ISstore(sharedTypes, (void*) elementType, elementHash); }else{ writeByteToBuffer(writer->buffer, PDB_SET_HEADER | PDB_TYPE_SHARED_FLAG); printInteger(writer->buffer, elementTypeId); } printInteger(writer->buffer, size); next = set; while(!ATisEmpty(next)){ ATerm current = ATgetFirst(next); next = ATgetNext(next); doSerialize(writer, elementType, current); } }
static void writeRelation(A2PWriter writer, A2PType expected, ATermList relation){ A2PRelationType relationType = (A2PRelationType) expected->theType; A2PType tupleType = relationType->tupleType; ISIndexedSet sharedTypes = writer->typeSharingMap; int tupleHash = hashType(tupleType); int tupleTypeId = ISget(sharedTypes, (void*) tupleType, tupleHash); int size = ATgetLength(relation); ATermList next; if(tupleTypeId == -1){ writeByteToBuffer(writer->buffer, PDB_RELATION_HEADER); doWriteType(writer, tupleType); ISstore(sharedTypes, (void*) tupleType, tupleHash); }else{ writeByteToBuffer(writer->buffer, PDB_RELATION_HEADER | PDB_TYPE_SHARED_FLAG); printInteger(writer->buffer, tupleTypeId); } printInteger(writer->buffer, size); next = relation; while(!ATisEmpty(next)){ ATerm current = ATgetFirst(next); next = ATgetNext(next); doSerialize(writer, tupleType, current); } }
static void writeTupleType(A2PWriter writer, A2PType tupleType){ A2PTupleType t = (A2PTupleType) tupleType->theType; A2PType *fieldTypes = t->fieldTypes; char **fieldNames = t->fieldNames; int nrOfFields = typeArraySize(fieldTypes); int hasFieldNames = (fieldNames == NULL) ? 0 : 1; int i; if(hasFieldNames == 0){ writeByteToBuffer(writer->buffer, PDB_TUPLE_TYPE_HEADER); printInteger(writer->buffer, nrOfFields); for(i = 0; i < nrOfFields; i++){ writeType(writer, t->fieldTypes[i]); } }else{ writeByteToBuffer(writer->buffer, PDB_TUPLE_TYPE_HEADER | PDB_HAS_FIELD_NAMES); printInteger(writer->buffer, nrOfFields); for(i = 0; i < nrOfFields; i++){ char *fieldName = fieldNames[i]; int fieldNameLength = dataArraySize(fieldName); writeType(writer, t->fieldTypes[i]); printInteger(writer->buffer, fieldNameLength); writeDataToBuffer(writer->buffer, fieldName, fieldNameLength); } } }
static void writeAnnotatedNode(A2PWriter writer, A2PType expected, ATermAppl node, ATermList annotations){ A2PNodeType t = (A2PNodeType) expected->theType; AFun fun = ATgetAFun(node); int arity = ATgetArity(fun); char *name = ATgetName(fun); int nrOfAnnotations = ATgetLength(annotations); int i; ATerm annotationLabel; ATerm annotationValue; unsigned int hash = hashString(name); int nodeNameId = ISstore(writer->nameSharingMap, (void*) name, hash); if(nodeNameId == -1){ int nameLength = dataArraySize(name); writeByteToBuffer(writer->buffer, PDB_ANNOTATED_NODE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); }else{ writeByteToBuffer(writer->buffer, PDB_ANNOTATED_NODE_HEADER | PDB_NAME_SHARED_FLAG); printInteger(writer->buffer, nodeNameId); } printInteger(writer->buffer, arity); for(i = 0; i < arity; i++){ doSerialize(writer, A2PvalueType(), ATgetArgument(node, i)); } /* Annotations. */ if((nrOfAnnotations % 2) == 1){ fprintf(stderr, "Detected corrupt annotations (Unbalanced).\n"); exit(1); } printInteger(writer->buffer, nrOfAnnotations); do{ char *label; int labelLength; A2PType annotationType; annotationLabel = ATgetFirst(annotations); annotations = ATgetNext(annotations); annotationValue = ATgetFirst(annotations); annotations = ATgetNext(annotations); if(ATgetType(annotationLabel) != AT_APPL){ fprintf(stderr, "Detected corrupt annotation; label term is not a 'string'.\n"); exit(1); } label = ATgetName(ATgetAFun((ATermAppl) annotationLabel)); labelLength = dataArraySize(label); printInteger(writer->buffer, labelLength); writeDataToBuffer(writer->buffer, label, labelLength); annotationType = (A2PType) HTget(t->declaredAnnotations, (void*) label, hashString(label)); doSerialize(writer, annotationType, annotationValue); }while(!ATisEmpty(annotations)); }
static void writeAnnotatedConstructor(A2PWriter writer, A2PType expected, ATermAppl constructor, ATermList annotations){ A2PConstructorType t = (A2PConstructorType) expected->theType; ISIndexedSet sharedTypes = writer->typeSharingMap; int typeHash = hashType(expected); int constructorTypeId = ISget(sharedTypes, (void*) expected, typeHash); int arity = ATgetArity(ATgetAFun(constructor)); int nrOfAnnotations = ATgetLength(annotations); int i; ATerm annotationLabel; ATerm annotationValue; if(constructorTypeId == -1){ writeByteToBuffer(writer->buffer, PDB_ANNOTATED_CONSTRUCTOR_HEADER); doWriteType(writer, expected); ISstore(sharedTypes, (void*) expected, typeHash); }else{ writeByteToBuffer(writer->buffer, PDB_ANNOTATED_CONSTRUCTOR_HEADER | PDB_TYPE_SHARED_FLAG); printInteger(writer->buffer, constructorTypeId); } printInteger(writer->buffer, arity); for(i = 0; i < arity; i++){ doSerialize(writer, ((A2PTupleType) t->children->theType)->fieldTypes[i], ATgetArgument(constructor, i)); } /* Annotations. */ if((nrOfAnnotations % 2) == 1){ fprintf(stderr, "Detected corrupt annotations (Unbalanced).\n"); exit(1); } printInteger(writer->buffer, nrOfAnnotations); do{ char *label; int labelLength; A2PType annotationType; annotationLabel = ATgetFirst(annotations); annotations = ATgetNext(annotations); annotationValue = ATgetFirst(annotations); annotations = ATgetNext(annotations); if(ATgetType(annotationLabel) != AT_APPL){ fprintf(stderr, "Detected corrupt annotation; label term is not a 'string'.\n"); exit(1); } label = ATgetName(ATgetAFun((ATermAppl) annotationLabel)); labelLength = dataArraySize(label); printInteger(writer->buffer, labelLength); writeDataToBuffer(writer->buffer, label, labelLength); annotationType = (A2PType) HTget(t->declaredAnnotations, (void*) label, hashString(label)); doSerialize(writer, annotationType, annotationValue); }while(!ATisEmpty(annotations)); }
bool EVShieldBank::ledSetRGB(uint8_t R, uint8_t G, uint8_t B) { bool b; writeByteToBuffer(_i2c_buffer, R); writeByteToBuffer(_i2c_buffer+1,G); writeByteToBuffer(_i2c_buffer+2,B); b = writeRegisters(SH_RGB_LED, 3); delay(1); // required to avoid subsequent i2c errors. return b; }
static void writeInteger(A2PWriter writer, ATermInt integer){ int intValue = ATgetInt(integer); writeByteToBuffer(writer->buffer, PDB_INTEGER_HEADER); printInteger(writer->buffer, intValue); }
static void writeListType(A2PWriter writer, A2PType listType){ A2PListType t = (A2PListType) listType->theType; writeByteToBuffer(writer->buffer, PDB_LIST_TYPE_HEADER); writeType(writer, t->elementType); }
static void writeDouble(A2PWriter writer, ATermReal real){ double doubleValue = ATgetReal(real); writeByteToBuffer(writer->buffer, PDB_IEEE754_ENCODED_DOUBLE_HEADER); printDouble(writer->buffer, doubleValue); }
static void writeRelationType(A2PWriter writer, A2PType relationType){ A2PRelationType t = (A2PRelationType) relationType->theType; writeByteToBuffer(writer->buffer, PDB_RELATION_TYPE_HEADER); writeType(writer, t->tupleType); }
static void writeSetType(A2PWriter writer, A2PType setType){ A2PSetType t = (A2PSetType) setType->theType; writeByteToBuffer(writer->buffer, PDB_SET_TYPE_HEADER); writeType(writer, t->elementType); }
static void writeAnnotatedConstructorType(A2PWriter writer, A2PType constructorType){ A2PConstructorType t = (A2PConstructorType) constructorType->theType; char *name = t->name; int nameLength = dataArraySize(name); HTHashtable hashtable = t->declaredAnnotations; HTIterator iterator = HTcreateIterator(hashtable); int nrOfAnnotations = HTsize(hashtable); HTEntry *nextAnnotation; writeByteToBuffer(writer->buffer, PDB_ANNOTATED_CONSTRUCTOR_TYPE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); writeType(writer, t->children); writeType(writer, t->adt); printInteger(writer->buffer, nrOfAnnotations); while((nextAnnotation = HTgetNext(iterator)) != NULL){ char *label = (char*) nextAnnotation->key; int labelLength = dataArraySize(label); printInteger(writer->buffer, labelLength); writeDataToBuffer(writer->buffer, label, labelLength); writeType(writer, (A2PType) nextAnnotation->value); } }
static void writeMapType(A2PWriter writer, A2PType mapType){ A2PMapType t = (A2PMapType) mapType->theType; writeByteToBuffer(writer->buffer, PDB_MAP_TYPE_HEADER); writeType(writer, t->keyType); writeType(writer, t->valueType); }
static void writeString(A2PWriter writer, ATermAppl string){ char *stringValue = ATgetName(ATgetAFun(string)); int stringValueLength = dataArraySize(stringValue); writeByteToBuffer(writer->buffer, PDB_STRING_HEADER); printInteger(writer->buffer, stringValueLength); writeDataToBuffer(writer->buffer, stringValue, stringValueLength); }
static void writeBool(A2PWriter writer, ATermAppl boolean){ char *boolName = ATgetName(ATgetAFun(boolean)); writeByteToBuffer(writer->buffer, PDB_BOOL_HEADER); if(strncmp(boolName, "true", 4) == 0){ printInteger(writer->buffer, 1); }else{ printInteger(writer->buffer, 0); } }
static void writeParameterType(A2PWriter writer, A2PType parameterType){ A2PParameterType t = (A2PParameterType) parameterType->theType; char *name = t->name; int nameLength = dataArraySize(name); writeByteToBuffer(writer->buffer, PDB_PARAMETER_TYPE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); writeType(writer, t->bound); }
static void writeADTType(A2PWriter writer, A2PType adtType){ A2PAbstractDataType t = (A2PAbstractDataType) adtType->theType; char *name = t->name; int nameLength = dataArraySize(name); writeByteToBuffer(writer->buffer, PDB_ADT_TYPE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); writeType(writer, A2PvoidType()); }
static void writeType(A2PWriter writer, A2PType type){ ISIndexedSet sharedTypes = writer->typeSharingMap; int typeHash = hashType(type); int typeId = ISget(sharedTypes, type, typeHash); if(typeId != -1){ writeByteToBuffer(writer->buffer, PDB_SHARED_FLAG); printInteger(writer->buffer, typeId); return; } doWriteType(writer, type); ISstore(sharedTypes, type, typeHash); }
static void writeAliasType(A2PWriter writer, A2PType aliasType){ A2PAliasType t = (A2PAliasType) aliasType->theType; char *name = t->name; int nameLength = dataArraySize(name); writeByteToBuffer(writer->buffer, PDB_ALIAS_TYPE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); writeType(writer, t->aliased); writeType(writer, t->parametersTuple); }
static void writeConstructorType(A2PWriter writer, A2PType constructorType){ A2PConstructorType t = (A2PConstructorType) constructorType->theType; char *name = t->name; int nameLength = dataArraySize(name); writeByteToBuffer(writer->buffer, PDB_CONSTRUCTOR_TYPE_HEADER); printInteger(writer->buffer, nameLength); writeDataToBuffer(writer->buffer, name, nameLength); writeType(writer, t->children); writeType(writer, t->adt); }
static void writeMap(A2PWriter writer, A2PType expected, ATermList map){ A2PMapType mapType = (A2PMapType) expected->theType; ISIndexedSet sharedTypes = writer->typeSharingMap; int mapHash = hashType(expected); int mapTypeId = ISget(sharedTypes, (void*) expected, mapHash); int size = ATgetLength(map); ATermList next; if(size % 2 == 1){ fprintf(stderr, "Number of elements in the map is unbalanced.\n"); exit(1); } if(mapTypeId == -1){ writeByteToBuffer(writer->buffer, PDB_MAP_HEADER); doWriteType(writer, expected); ISstore(sharedTypes, (void*) expected, mapHash); }else{ writeByteToBuffer(writer->buffer, PDB_MAP_HEADER | PDB_TYPE_SHARED_FLAG); printInteger(writer->buffer, mapTypeId); } printInteger(writer->buffer, size >> 1); next = map; while(!ATisEmpty(next)){ ATerm key = ATgetFirst(map); ATerm value; next = ATgetNext(map); value = ATgetFirst(map); next = ATgetNext(map); doSerialize(writer, mapType->keyType, key); doSerialize(writer, mapType->valueType, value); } }
static void writeTuple(A2PWriter writer, A2PType expected, ATermAppl tuple){ A2PTupleType t = (A2PTupleType) expected->theType; A2PType *fieldTypes = t->fieldTypes; int numberOfFieldTypes = typeArraySize(fieldTypes); int arity = ATgetArity(ATgetAFun(tuple)); int i; if(numberOfFieldTypes != arity){ fprintf(stderr, "The number of children specified in the type is not equal to the arity of this tuple.\n"); exit(1); } writeByteToBuffer(writer->buffer, PDB_TUPLE_HEADER); printInteger(writer->buffer, arity); for(i = 0; i < arity; i++){ doSerialize(writer, fieldTypes[i], ATgetArgument(tuple, i)); } }
static void writeAnnotatedNodeType(A2PWriter writer, A2PType nodeType){ A2PNodeType t = (A2PNodeType) nodeType->theType; HTHashtable hashtable = t->declaredAnnotations; HTIterator iterator = HTcreateIterator(hashtable); int nrOfAnnotations = HTsize(hashtable); HTEntry *nextAnnotation; writeByteToBuffer(writer->buffer, PDB_ANNOTATED_NODE_TYPE_HEADER); printInteger(writer->buffer, nrOfAnnotations); while((nextAnnotation = HTgetNext(iterator)) != NULL){ char *label = (char*) nextAnnotation->key; int labelLength = dataArraySize(label); printInteger(writer->buffer, labelLength); writeDataToBuffer(writer->buffer, label, labelLength); writeType(writer, (A2PType) nextAnnotation->value); } }
static void writeSourceLocationType(A2PWriter writer){ writeByteToBuffer(writer->buffer, PDB_SOURCE_LOCATION_TYPE_HEADER); }
static void doSerialize(A2PWriter writer, A2PType expected, ATerm value){ DKISIndexedSet sharedValues = writer->valueSharingMap; int valueHash = hashValue(value); int valueId = DKISget(sharedValues, (void*) value, (void*) expected, valueHash); /* TODO: Fix sharing (check types). */ if(valueId != -1){ writeByteToBuffer(writer->buffer, PDB_SHARED_FLAG); printInteger(writer->buffer, valueId); return; } switch(expected->id){ case PDB_VALUE_TYPE_HEADER: serializeUntypedTerm(writer, value); break; case PDB_BOOL_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Boolean didn't have AT_APPL type.\n"); exit(1); } writeBool(writer, (ATermAppl) value); break; case PDB_INTEGER_TYPE_HEADER: if(ATgetType(value) != AT_INT){ fprintf(stderr, "Integer didn't have AT_INT type.\n"); exit(1); } writeInteger(writer, (ATermInt) value); break; case PDB_DOUBLE_TYPE_HEADER: if(ATgetType(value) != AT_REAL){ fprintf(stderr, "Double didn't have AT_REAL type.\n"); exit(1); } writeDouble(writer, (ATermReal) value); break; case PDB_STRING_TYPE_HEADER: if(ATgetType(value) != AT_APPL || ATisQuoted(ATgetAFun((ATermAppl) value)) == ATfalse){ fprintf(stderr, "String didn't have 'quoted' AT_APPL type.\n"); ATabort(""); exit(1); } writeString(writer, (ATermAppl) value); break; case PDB_SOURCE_LOCATION_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Source location didn't have AT_APPL type.\n"); exit(1); } writeSourceLocation(writer, (ATermAppl) value); break; case PDB_NODE_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Node didn't have AT_APPL type.\n"); exit(1); } { ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeNode(writer, expected, (ATermAppl) value); }else{ if(((A2PNodeType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Node term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedNode(writer, expected, (ATermAppl) value, annotations); } } break; case PDB_TUPLE_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Tuple didn't have AT_APPL type.\n"); exit(1); } writeTuple(writer, expected, (ATermAppl) value); break; case PDB_LIST_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "List didn't have AT_LIST type.\n"); exit(1); } writeList(writer, expected, (ATermList) value); break; case PDB_SET_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Set didn't have AT_LIST type.\n"); exit(1); } writeSet(writer, expected, (ATermList) value); break; case PDB_RELATION_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Relation didn't have AT_LIST type.\n"); exit(1); } writeRelation(writer, expected, (ATermList) value); break; case PDB_MAP_TYPE_HEADER: if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Map didn't have AT_LIST type.\n"); exit(1); } writeMap(writer, expected, (ATermList) value); break; case PDB_CONSTRUCTOR_TYPE_HEADER: if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Constructor didn't have AT_APPL type.\n"); exit(1); } { ATermList annotations = (ATermList) ATgetAnnotations(value); if(annotations == NULL){ writeConstructor(writer, expected, (ATermAppl) value); }else{ if(((A2PConstructorType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Constructor term has annotations, but none are declared.\n"); exit(1); } writeAnnotatedConstructor(writer, expected, (ATermAppl) value, annotations); } } break; case PDB_ADT_TYPE_HEADER: writeADT(writer, expected, value); break; default: fprintf(stderr, "Unserializable type: %d\n.", expected->id); exit(1); } DKISstore(sharedValues, (void*) value, (void*) expected, valueHash); }
static void writeDoubleType(A2PWriter writer){ writeByteToBuffer(writer->buffer, PDB_DOUBLE_TYPE_HEADER); }
static void writeStringType(A2PWriter writer){ writeByteToBuffer(writer->buffer, PDB_STRING_TYPE_HEADER); }
static void writeNodeType(A2PWriter writer, A2PType nodeType){ writeByteToBuffer(writer->buffer, PDB_NODE_TYPE_HEADER); }