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

Application/Literal/Variable nodes are parsed/created

parent 481adea1
......@@ -23,7 +23,7 @@ struct CodeFlags
struct CleanString
{
int length;
char characters[];
char chars[];
};
struct LitEntry
......@@ -39,9 +39,9 @@ struct LitEntry
};
};
#define VAR_ARG
#define VAR_LOCAL
#define VAR_FN
#define VAR_ARG 1
#define VAR_LOCAL 2
#define VAR_FN 3
struct VarEntry
{
......
......@@ -3,14 +3,22 @@
#include "mem.h"
int desc_alloc;
int code_alloc;
void init_mem()
{
desc_alloc = 0;
code_alloc = 0;
}
void* alloc_desc(int size)
{
desc_alloc += size;
return malloc(size);
}
void* alloc_code(int size)
{
code_alloc += size;
return malloc(size);
}
\ No newline at end of file
......@@ -6,4 +6,7 @@ void init_mem();
// TODO: inline
void* alloc_desc(int size);
// TODO: inline
void* alloc_code(int size);
#endif // __MEM_H
......@@ -25,7 +25,23 @@ int parseInt(char** ptr, int* result)
}
}
void copyNameAndForward(char* dest, char** source, int length)
int parseReal(char** ptr, double* result)
{
char *end;
*result = strtod(*ptr, &end);
if(*ptr == end)
{
return 0;
}
else
{
*ptr = end + 1;
return 1;
}
}
void copyStringAndForward(char* dest, char** source, int length)
{
memcpy(dest, *source, length);
dest[length] = '\0';
......@@ -108,7 +124,7 @@ int parseDef1(char** ptr)
entry->flags.type = FT_CAF;
entry->flags.arity = 0;
copyNameAndForward(entry->name, ptr, nameLength);
copyStringAndForward(entry->name, ptr, nameLength);
// set the continuation for stage 2
entry->body = *ptr;
......@@ -174,7 +190,7 @@ int parseDef1(char** ptr)
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD;
copyNameAndForward(entry->name, ptr, nameLength);
copyStringAndForward(entry->name, ptr, nameLength);
int arity;
if(!parseInt(ptr, &arity)) return 0;
......@@ -188,7 +204,7 @@ int parseDef1(char** ptr)
if(!parseInt(ptr, &fieldNameLength)) return 0;
entry->fields[i] = (char*) alloc_desc(fieldNameLength);
copyNameAndForward(entry->fields[i], ptr, fieldNameLength);
copyStringAndForward(entry->fields[i], ptr, fieldNameLength);
}
add_desc(entry->name, (FunFlags*) entry);
......@@ -203,6 +219,149 @@ int parseDef1(char** ptr)
return 1;
}
LitEntry* parseLit(char **ptr)
{
// 1. Type char
char type = *(*ptr)++;
int strlen = 0;
if(type == 'S')
{
if(!parseInt(ptr, &strlen)) return 0;
}
struct LitEntry* entry = (LitEntry*) alloc_code(sizeof(LitEntry) + strlen + 1);
entry->flags.type = CT_LIT;
switch(type)
{
case 'I': // Int
{
entry->flags.local_type = LIT_INT;
if(!parseInt(ptr, &entry->_int)) return 0;
break;
}
case 'C': // Char
{
entry->flags.local_type = LIT_CHAR;
entry->_char = *(*ptr)++;
break;
}
case 'R': // Real
{
entry->flags.local_type = LIT_REAL;
if(!parseReal(ptr, &entry->_real)) return 0;
break;
}
case '0': // Bool
case '1':
{
entry->flags.local_type = LIT_BOOL;
entry->_bool = type == '1';
break;
}
case 'S': // String
{
entry->flags.local_type = LIT_STRING;
entry->_string.length = strlen;
copyStringAndForward(entry->_string.chars, ptr, strlen);
}
}
return entry;
}
VarEntry* parseVar(char **ptr)
{
// 1. Type char
char type = *(*ptr)++;
struct VarEntry* entry = (VarEntry*) alloc_code(sizeof(VarEntry));
entry->flags.type = CT_VAR;
switch(type)
{
case 'A': // Argument
{
entry->flags.local_type = VAR_ARG;
if(!parseInt(ptr, &entry->index)) return 0;
break;
}
case 'L': // Local var
{
entry->flags.local_type = VAR_LOCAL;
if(!parseInt(ptr, &entry->index)) return 0;
break;
}
case 'F': // Function
{
entry->flags.local_type = VAR_FN;
char name[MAX_IDENTIFIER_LENGTH];
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
copyStringAndForward(name, ptr, nameLength);
entry->f = (FunFlags*) find_desc(name); // can fail
if(entry->f == 0)
{
printf("%s not found\n", name);
return 0;
}
break;
}
}
return entry;
}
CodeFlags* parseTerm(char **ptr);
AppEntry* parseApp(char **ptr)
{
VarEntry* var = parseVar(ptr);
int nrArgs;
if(!parseInt(ptr, &nrArgs)) return 0;
struct AppEntry* entry = (AppEntry*) alloc_code(sizeof(AppEntry) + sizeof(void*) * nrArgs);
entry->flags.type = CT_APP;
entry->var = var;
for(int i = 0; i < nrArgs; i++)
{
entry->args[i] = parseTerm(ptr);
if(entry->args[i] == 0) return 0;
}
return entry;
}
CodeFlags* parseTerm(char **ptr)
{
// 1. Type char
char type = *(*ptr)++;
switch(type)
{
case 'L': // Literal
return (CodeFlags*) parseLit(ptr);
case 'V': // Variable
return (CodeFlags*) parseVar(ptr);
case 'A': // Application
return (CodeFlags*) parseApp(ptr);
}
}
int parseDef2(char** ptr)
{
int defSize;
......@@ -223,12 +382,15 @@ int parseDef2(char** ptr)
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
copyNameAndForward(name, ptr, nameLength);
copyStringAndForward(name, ptr, nameLength);
FunEntry* entry = (FunEntry*) find_desc(name); // should not fail, just added to the map in the 1. phase
// read continuation
*ptr = (char*) entry->body;
// parse body
entry->body = parseTerm(ptr);
break;
}
......@@ -237,12 +399,15 @@ int parseDef2(char** ptr)
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
copyNameAndForward(name, ptr, nameLength);
copyStringAndForward(name, ptr, nameLength);
CAFEntry* entry = (CAFEntry*) find_desc(name); // should not fail, just added to the map in the 1. phase
// read continuation
*ptr = (char*) entry->body;
// parse body
entry->body = parseTerm(ptr);
break;
}
......
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