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

refactor to introduce phase 2

parent 60ce70a0
......@@ -12,7 +12,7 @@ int main()
char* stream = "40 R10 example._R2 1 9 example.a9 example.b46 F13 example.Start0 0 AF10 example._R2 LI1 LI2 29 F4 main0 0 VF13 example.Start";
int res = parse1(stream, strlen(stream));
int res = parse(&stream, strlen(stream));
printf("%d", res);
......
......@@ -22,27 +22,15 @@ int parseInt(char** ptr, int* result)
}
}
/*
* First phase parsing: creates description table and registers it in funname -> desc hashmap
* Body pointers are set to the body part of the input string
* Returns the number of parsed function definitions or -1 on failure
*/
int parse1(char* ptr, int length)
int parseDef1(char** ptr)
{
char* endptr = ptr + length;
int numDefs = 0;
while(ptr < endptr)
{
numDefs++;
int defSize;
if(!parseInt(&ptr, &defSize)) return -1;
if(!parseInt(ptr, &defSize)) return -1;
char* nextDef = ptr + defSize;
char* nextDef = *ptr + defSize;
// 2. Type char
char type = *(ptr++);
char type = *(*ptr)++;
switch(type)
{
......@@ -52,22 +40,22 @@ int parse1(char* ptr, int length)
// 37 F12 example.g_453 6 AF3 add2 VA0 VA1
int nameLength;
if(!parseInt(&ptr, &nameLength)) return -1;
if(!parseInt(ptr, &nameLength)) return 0;
FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1);
entry->flags.type = FT_FUN;
memcpy(entry->name, ptr, nameLength);
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
ptr += nameLength;
*ptr += nameLength;
int arity;
if(!parseInt(&ptr, &arity)) return -1;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
if(!parseInt(&ptr, &entry->strictness)) return -1;
if(!parseInt(ptr, &entry->strictness)) return 0;
// set the continuation for stage 2
entry->body = ptr;
entry->body = *ptr;
add_desc(entry->name, (FunFlags*) entry);
break;
......@@ -79,25 +67,25 @@ int parse1(char* ptr, int length)
// 33 A2 9 example.A2 2 9 example.B0 0
int conNum;
if(!parseInt(&ptr, &conNum)) return -1;
if(!parseInt(ptr, &conNum)) return 0;
for(int i=0; i<conNum; i++)
{
int nameLength;
if(!parseInt(&ptr, &nameLength)) return -1;
if(!parseInt(ptr, &nameLength)) return 0;
ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_ADT;
memcpy(entry->name, ptr, nameLength);
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
ptr += nameLength;
*ptr += nameLength;
int arity;
if(!parseInt(&ptr, &arity)) return -1;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
if(!parseInt(&ptr, &entry->strictness)) return -1;
if(!parseInt(ptr, &entry->strictness)) return 0;
add_desc(entry->name, (FunFlags*) entry);
}
......@@ -111,44 +99,97 @@ int parse1(char* ptr, int length)
// 40 R10 example._R2 1 9 example.a9 example.b
int nameLength;
if(!parseInt(&ptr, &nameLength)) return -1;
if(!parseInt(ptr, &nameLength)) return 0;
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD;
memcpy(entry->name, ptr, nameLength);
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
ptr += nameLength;
*ptr += nameLength;
int arity;
if(!parseInt(&ptr, &arity)) return -1;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
if(!parseInt(&ptr, &entry->strictness)) return -1;
if(!parseInt(ptr, &entry->strictness)) return 0;
entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
for(int i=0; i<arity; i++)
{
int fieldNameLength;
if(!parseInt(&ptr, &fieldNameLength)) return -1;
if(!parseInt(ptr, &fieldNameLength)) return 0;
entry->fields[i] = (char*) malloc(fieldNameLength);
memcpy(entry->fields[i], ptr, fieldNameLength);
memcpy(entry->fields[i], *ptr, fieldNameLength);
entry->fields[i][fieldNameLength] = '\0';
ptr += fieldNameLength;
*ptr += fieldNameLength;
}
add_desc(entry->name, (FunFlags*) entry);
break;
}
default:
return 0;
}
*ptr = nextDef;
return 1;
}
int parseDef2(char** ptr)
{
int defSize;
if(!parseInt(ptr, &defSize)) return -1;
char* nextDef = *ptr + defSize;
// 2. Type char
char type = *(*ptr)++;
switch(type)
{
case 'F': // Normal function
{
break;
}
case 'A':
case 'R':
break; // skip it, already fully parsed
default:
return -1;
}
ptr = nextDef;
*ptr = nextDef;
return 1;
}
int parse(char** ptr, int length)
{
char **origptr = ptr;
char* endptr = *ptr + length;
int numDefs = 0;
while(*ptr < endptr)
{
numDefs++;
if(parseDef1(ptr) < 0) return -1;
}
ptr = origptr;
while(*ptr < endptr)
{
if(parseDef2(ptr) < 0) return -1;
}
return numDefs;
}
......@@ -3,8 +3,6 @@
#include "desc.h"
//int parseInt(char** str, int* result);
int parse1(char* ptr, int length);
int parse(char** ptr, int length);
#endif // __PARSE_H
\ No newline at end of file
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