Commit 60ce70a0 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

parse all the lines in phase 1

parent 93c55a3e
#include <stdio.h>
#include <string.h>
#include "mem.h"
#include "desc.h"
......@@ -9,10 +10,11 @@ int main()
{
init_mem();
int res;
char* stream = "40 R10 example._R2 1 9 example.a9 example.b";
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";
parseLine1(&stream);
int res = parse1(stream, strlen(stream));
printf("%d", res);
// add_fun("jimbo", (FunFlags*) "artist");
// char *tval = (char*) find_fun("jimbo");
......
......@@ -22,122 +22,133 @@ int parseInt(char** ptr, int* result)
}
}
int parseLine1(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)
{
int lineSize;
char* endptr = ptr + length;
int numDefs = 0;
// 1. Length of the line
if(!parseInt(ptr, &lineSize)) return 0;
char* nextLine = *ptr + lineSize;
// 2. Type char
char type = *(*ptr)++;
switch(type)
while(ptr < endptr)
{
numDefs++;
case 'F': // Normal function
{
// 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;
int defSize;
if(!parseInt(&ptr, &defSize)) return -1;
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;
}
char* nextDef = ptr + defSize;
case 'A': // ADT
// 2. Type char
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;
for(int i=0; i<conNum; i++)
case 'F': // Normal function
{
// 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;
ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_ADT;
memcpy(entry->name, *ptr, nameLength);
if(!parseInt(&ptr, &nameLength)) return -1;
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;
ptr += nameLength;
int arity;
if(!parseInt(ptr, &arity)) return 0;
if(!parseInt(&ptr, &arity)) return -1;
entry->flags.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0;
if(!parseInt(&ptr, &entry->strictness)) return -1;
add_desc(entry->name, (FunFlags*) entry);
}
break;
}
case 'R': // Record
{
// :: example._R = {example.a, !example.b}
// 40 R10 example._R2 1 9 example.a9 example.b
// set the continuation for stage 2
entry->body = ptr;
add_desc(entry->name, (FunFlags*) entry);
break;
}
int nameLength;
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);
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;
case 'A': // ADT
{
// :: example.A = example.A !a1 a2 | example.B
// 33 A2 9 example.A2 2 9 example.B0 0
entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
int conNum;
if(!parseInt(&ptr, &conNum)) return -1;
for(int i=0; i<conNum; i++)
{
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;
}
for(int i=0; i<arity; i++)
case 'R': // Record
{
int fieldNameLength;
if(!parseInt(ptr, &fieldNameLength)) return 0;
// :: example._R = {example.a, !example.b}
// 40 R10 example._R2 1 9 example.a9 example.b
entry->fields[i] = (char*) malloc(fieldNameLength);
int nameLength;
if(!parseInt(&ptr, &nameLength)) return -1;
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD;
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;
entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
memcpy(entry->fields[i], *ptr, fieldNameLength);
entry->fields[i][fieldNameLength] = '\0';
*ptr += fieldNameLength;
for(int i=0; i<arity; i++)
{
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);
break;
}
add_desc(entry->name, (FunFlags*) entry);
break;
default:
return -1;
}
default:
return 0;
ptr = nextDef;
}
*ptr = nextLine;
return 1;
}
\ No newline at end of file
return numDefs;
}
......@@ -5,6 +5,6 @@
//int parseInt(char** str, int* result);
int parseLine1(char** ptr);
int parse1(char* ptr, int length);
#endif // __PARSE_H
\ No newline at end of file
......@@ -59,6 +59,11 @@ sVar ctx var a
where
varName = unpackVar var
genDefs [] a = a
genDefs [f:fs] a = a <++ textSize fstr <++ " " <++ fstr <++ genDefs fs
where
fstr = toString (sFunc newContext f newAppender)
instance Appendable Literal
where
(<++) a (LString lit) = a <++ "S" <++ sText (toJSLiteral lit)
......@@ -70,11 +75,8 @@ where
Start world
# (Ok sapl, world) = readFile "example.sapl" world
# tokens = tokensWithPositions sapl
# (Ok ([f:fs], _)) = parse tokens
# fstr = toString (sFunc newContext f newAppender)
= (f, toString (textSize fstr) +++ " " +++ fstr, world)
# (Ok (fs, _)) = parse tokens
= (toString (genDefs fs newAppender), world)
\ No newline at end of file
Markdown is supported
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