Commit 481adea1 authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

prepare for parsing the body of a function

parent c26b13c0
#ifndef __CODE_H
#define __CODE_H
#include "desc.h"
#define CT_LIT 1
#define CT_VAR 2
#define CT_APP 3
struct CodeFlags
{
unsigned int type : 3;
unsigned int local_type : 3;
unsigned int arity : 4;
};
#define LIT_INT 1
#define LIT_REAL 2
#define LIT_CHAR 3
#define LIT_BOOL 4
#define LIT_STRING 5
struct CleanString
{
int length;
char characters[];
};
struct LitEntry
{
struct CodeFlags flags;
union
{
int _int;
double _real;
char _char;
int _bool;
struct CleanString _string;
};
};
#define VAR_ARG
#define VAR_LOCAL
#define VAR_FN
struct VarEntry
{
struct CodeFlags flags;
union
{
int index; // index on the stack
FunFlags* f;
};
};
struct AppEntry
{
struct CodeFlags flags;
struct VarEntry* var;
struct CodeFlags* args[];
};
#endif // __CODE_H
\ No newline at end of file
......@@ -6,6 +6,9 @@
#include "parse.h"
#include "mem.h"
#include "desc.h"
#include "code.h"
int parseInt(char** ptr, int* result)
{
char *end;
......@@ -206,15 +209,40 @@ int parseDef2(char** ptr)
if(!parseInt(ptr, &defSize)) return -1;
char* nextDef = *ptr + defSize;
// This is used to temporarily copy (converts clean string to C style) the function name for lookup
char name[MAX_IDENTIFIER_LENGTH];
// 2. Type char
char type = *(*ptr)++;
switch(type)
{
case 'F': // Normal function
{
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
copyNameAndForward(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;
break;
}
case 'C': // CAF
{
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
copyNameAndForward(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;
break;
}
......@@ -232,7 +260,7 @@ int parseDef2(char** ptr)
int parse(char** ptr, int length)
{
char **origptr = ptr;
char* origptr = *ptr;
char* endptr = *ptr + length;
int numDefs = 0;
......@@ -243,7 +271,7 @@ int parse(char** ptr, int length)
if(parseDef1(ptr) < 0) return -1;
}
ptr = origptr;
*ptr = origptr;
while(*ptr < endptr)
{
......
#ifndef __PARSE_H
#define __PARSE_H
#include "desc.h"
#define MAX_IDENTIFIER_LENGTH 256
int parse(char** ptr, int length);
......
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