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() ...@@ -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"; 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); printf("%d", res);
......
...@@ -22,133 +22,174 @@ int parseInt(char** ptr, int* result) ...@@ -22,133 +22,174 @@ int parseInt(char** ptr, int* result)
} }
} }
/* int parseDef1(char** ptr)
* 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)
{ {
char* endptr = ptr + length; int defSize;
int numDefs = 0; if(!parseInt(ptr, &defSize)) return -1;
char* nextDef = *ptr + defSize;
while(ptr < endptr) // 2. Type char
char type = *(*ptr)++;
switch(type)
{ {
numDefs++;
int defSize; case 'F': // Normal function
if(!parseInt(&ptr, &defSize)) return -1; {
// example.g_45 !a_0 !b_1 c_2 = add a_0 b_1
// 37 F12 example.g_453 6 AF3 add2 VA0 VA1
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
char* nextDef = ptr + defSize; FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1);
entry->flags.type = FT_FUN;
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0;
// set the continuation for stage 2
entry->body = *ptr;
add_desc(entry->name, (FunFlags*) entry);
break;
}
// 2. Type char case 'A': // ADT
char type = *(ptr++);
switch(type)
{ {
// :: example.A = example.A !a1 a2 | example.B
// 33 A2 9 example.A2 2 9 example.B0 0
int conNum;
if(!parseInt(ptr, &conNum)) return 0;
case 'F': // Normal function for(int i=0; i<conNum; i++)
{ {
// example.g_45 !a_0 !b_1 c_2 = add a_0 b_1
// 37 F12 example.g_453 6 AF3 add2 VA0 VA1
int nameLength; int nameLength;
if(!parseInt(&ptr, &nameLength)) return -1; if(!parseInt(ptr, &nameLength)) return 0;
FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1); ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_FUN; entry->flags.type = FT_ADT;
memcpy(entry->name, ptr, nameLength); memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0'; entry->name[nameLength] = '\0';
ptr += nameLength; *ptr += nameLength;
int arity; 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;
// set the continuation for stage 2
entry->body = ptr;
add_desc(entry->name, (FunFlags*) entry);
break;
}
case 'A': // ADT
{
// :: example.A = example.A !a1 a2 | example.B
// 33 A2 9 example.A2 2 9 example.B0 0
int conNum;
if(!parseInt(&ptr, &conNum)) return -1;
for(int i=0; i<conNum; i++) entry->flags.arity = arity;
{ if(!parseInt(ptr, &entry->strictness)) return 0;
int nameLength;
if(!parseInt(&ptr, &nameLength)) return -1;
ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_ADT;
memcpy(entry->name, ptr, nameLength);
entry->name[nameLength] = '\0';
ptr += nameLength;
int arity;
if(!parseInt(&ptr, &arity)) return -1;
entry->flags.arity = arity;
if(!parseInt(&ptr, &entry->strictness)) return -1;
add_desc(entry->name, (FunFlags*) entry);
}
break; add_desc(entry->name, (FunFlags*) entry);
} }
case 'R': // Record break;
{ }
// :: example._R = {example.a, !example.b}
// 40 R10 example._R2 1 9 example.a9 example.b case 'R': // Record
{
int nameLength; // :: example._R = {example.a, !example.b}
if(!parseInt(&ptr, &nameLength)) return -1; // 40 R10 example._R2 1 9 example.a9 example.b
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1); int nameLength;
entry->flags.type = FT_RECORD; if(!parseInt(ptr, &nameLength)) return 0;
memcpy(entry->name, ptr, nameLength); RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->name[nameLength] = '\0'; entry->flags.type = FT_RECORD;
ptr += nameLength;
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity; int arity;
if(!parseInt(&ptr, &arity)) return -1; if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity; 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); entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
for(int i=0; i<arity; i++)
{
int fieldNameLength;
if(!parseInt(ptr, &fieldNameLength)) return 0;
for(int i=0; i<arity; i++) entry->fields[i] = (char*) malloc(fieldNameLength);
{
int fieldNameLength;
if(!parseInt(&ptr, &fieldNameLength)) return -1;
entry->fields[i] = (char*) malloc(fieldNameLength);
memcpy(entry->fields[i], ptr, fieldNameLength);
entry->fields[i][fieldNameLength] = '\0';
ptr += fieldNameLength;
}
add_desc(entry->name, (FunFlags*) entry); memcpy(entry->fields[i], *ptr, fieldNameLength);
break; entry->fields[i][fieldNameLength] = '\0';
*ptr += fieldNameLength;
} }
default: add_desc(entry->name, (FunFlags*) entry);
return -1; 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;
} }
ptr = nextDef; case 'A':
case 'R':
break; // skip it, already fully parsed
default:
return -1;
}
*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; return numDefs;
} }
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
#include "desc.h" #include "desc.h"
//int parseInt(char** str, int* result); int parse(char** ptr, int length);
int parse1(char* ptr, int length);
#endif // __PARSE_H #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