Commit c26b13c0 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

generate SliceEntries for ADTEntries/FunEntries

parent e66b7d23
......@@ -22,6 +22,7 @@ struct FunEntry
char name[];
};
// an array of these is just before an ADTEntry/FunEntry (as many as arity)
struct SliceEntry
{
struct FunFlags flags;
......
......@@ -22,11 +22,22 @@ int parseInt(char** ptr, int* result)
}
}
void copyName(char* dest, char** source, int length)
void copyNameAndForward(char* dest, char** source, int length)
{
memcpy(dest, *source, length);
dest[length] = '\0';
*source += length;
*source += length;
}
void genSlices(void* dest, void* forward_ptr, int arity)
{
for(int i=0; i<arity; i++)
{
SliceEntry* slice = (SliceEntry*) dest + sizeof(SliceEntry) * i;
slice->flags.type = FT_SLICE;
slice->flags.arity = i;
slice->forward_ptr = forward_ptr;
}
}
int parseDef1(char** ptr)
......@@ -46,21 +57,37 @@ int parseDef1(char** ptr)
// example.g_45 !a_0 !b_1 c_2 = add a_0 b_1
// 37 F12 example.g_453 6 AF3 add2 VA0 VA1
// 1. field: length of the name
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
// 2. field: characters of the name (postpone copying)
char* namePtr = *ptr;
*ptr += nameLength;
FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1);
entry->flags.type = FT_FUN;
copyName(entry->name, ptr, nameLength);
// 3. field: arity
int arity;
if(!parseInt(ptr, &arity)) return 0;
// before the FunEntry there are "arity" number of SliceEntries
void* entry_base = alloc_desc(sizeof(SliceEntry) * arity + sizeof(FunEntry) + nameLength + 1);
FunEntry* entry = (FunEntry*) (entry_base + sizeof(SliceEntry) * arity);
entry->flags.type = FT_FUN;
entry->flags.arity = arity;
// now the name can be copied into the FunEntry
memcpy(entry->name, namePtr, nameLength);
entry->name[nameLength] = '\0';
// 4. field: strictness bits
if(!parseInt(ptr, &entry->strictness)) return 0;
// set the continuation for stage 2
entry->body = *ptr;
// generate slices. avoid function call if arity is zero
if(arity>0) genSlices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
break;
......@@ -78,7 +105,7 @@ int parseDef1(char** ptr)
entry->flags.type = FT_CAF;
entry->flags.arity = 0;
copyName(entry->name, ptr, nameLength);
copyNameAndForward(entry->name, ptr, nameLength);
// set the continuation for stage 2
entry->body = *ptr;
......@@ -92,25 +119,42 @@ int parseDef1(char** ptr)
// :: example.A = example.A !a1 a2 | example.B
// 33 A2 9 example.A2 2 9 example.B0 0
// 1. field: the number of constructors
int conNum;
if(!parseInt(ptr, &conNum)) return 0;
for(int i=0; i<conNum; i++)
{
// 1. field: length of the name
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_ADT;
copyName(entry->name, ptr, nameLength);
// 2. field: characters of the name (postpone copying)
char* namePtr = *ptr;
*ptr += nameLength;
// 3. field: arity
int arity;
if(!parseInt(ptr, &arity)) return 0;
if(!parseInt(ptr, &arity)) return 0;
// before the FunEntry there are "arity" number of SliceEntries
void* entry_base = alloc_desc(sizeof(SliceEntry) * arity + sizeof(ADTEntry) + nameLength + 1);
ADTEntry* entry = (ADTEntry*) (entry_base + sizeof(SliceEntry) * arity);
entry->flags.type = FT_ADT;
entry->flags.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0;
add_desc(entry->name, (FunFlags*) entry);
// now the name can be copied into the ADTEntry
memcpy(entry->name, namePtr, nameLength);
entry->name[nameLength] = '\0';
// 4. field: strictness bits
if(!parseInt(ptr, &entry->strictness)) return 0;
// generate slices. avoid function call if arity is zero
if(arity>0) genSlices(entry_base, entry, arity);
add_desc(entry->name, (FunFlags*) entry);
}
break;
......@@ -127,7 +171,7 @@ int parseDef1(char** ptr)
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD;
copyName(entry->name, ptr, nameLength);
copyNameAndForward(entry->name, ptr, nameLength);
int arity;
if(!parseInt(ptr, &arity)) return 0;
......@@ -141,7 +185,7 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &fieldNameLength)) return 0;
entry->fields[i] = (char*) alloc_desc(fieldNameLength);
copyName(entry->fields[i], ptr, fieldNameLength);
copyNameAndForward(entry->fields[i], ptr, fieldNameLength);
}
add_desc(entry->name, (FunFlags*) entry);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment