Commit 258b1d18 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

simplify parsing code

parent e45ed6d3
...@@ -22,6 +22,13 @@ int parseInt(char** ptr, int* result) ...@@ -22,6 +22,13 @@ int parseInt(char** ptr, int* result)
} }
} }
void copyName(char* dest, char** source, int length)
{
memcpy(dest, *source, length);
dest[length] = '\0';
*source += length;
}
int parseDef1(char** ptr) int parseDef1(char** ptr)
{ {
int defSize; int defSize;
...@@ -44,10 +51,8 @@ int parseDef1(char** ptr) ...@@ -44,10 +51,8 @@ int parseDef1(char** ptr)
FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1); FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1);
entry->flags.type = FT_FUN; entry->flags.type = FT_FUN;
memcpy(entry->name, *ptr, nameLength); copyName(entry->name, ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity; int arity;
if(!parseInt(ptr, &arity)) return 0; if(!parseInt(ptr, &arity)) return 0;
...@@ -76,14 +81,11 @@ int parseDef1(char** ptr) ...@@ -76,14 +81,11 @@ int parseDef1(char** ptr)
ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1); ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_ADT; entry->flags.type = FT_ADT;
memcpy(entry->name, *ptr, nameLength); copyName(entry->name, ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity; int arity;
if(!parseInt(ptr, &arity)) return 0; if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity; entry->flags.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0; if(!parseInt(ptr, &entry->strictness)) return 0;
...@@ -103,10 +105,8 @@ int parseDef1(char** ptr) ...@@ -103,10 +105,8 @@ int parseDef1(char** ptr)
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1); RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD; entry->flags.type = FT_RECORD;
memcpy(entry->name, *ptr, nameLength); copyName(entry->name, ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity; int arity;
if(!parseInt(ptr, &arity)) return 0; if(!parseInt(ptr, &arity)) return 0;
...@@ -114,17 +114,13 @@ int parseDef1(char** ptr) ...@@ -114,17 +114,13 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &entry->strictness)) return 0; if(!parseInt(ptr, &entry->strictness)) return 0;
entry->fields = (char**) alloc_desc(sizeof(char*) * arity); entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
for(int i=0; i<arity; i++) for(int i=0; i<arity; i++)
{ {
int fieldNameLength; int fieldNameLength;
if(!parseInt(ptr, &fieldNameLength)) return 0; if(!parseInt(ptr, &fieldNameLength)) return 0;
entry->fields[i] = (char*) malloc(fieldNameLength); entry->fields[i] = (char*) alloc_desc(fieldNameLength);
copyName(entry->fields[i], ptr, fieldNameLength);
memcpy(entry->fields[i], *ptr, fieldNameLength);
entry->fields[i][fieldNameLength] = '\0';
*ptr += fieldNameLength;
} }
add_desc(entry->name, (FunFlags*) entry); 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