Commit 5dd354ca authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

integer literals can be evaluated now

parent e20fb38f
#include <stdio.h>
#include <stdlib.h>
#include "code.h"
#include "thunk.h"
struct Thunk* exec(Code* expr)
{
switch(expr->type)
{
case CT_LIT:
switch(expr->local_type)
{
case LIT_INT:
return createI(((LitEntry*) expr)->_int);
break;
default:
printf("Exec: Unhandled LIT type");
exit(-1);
}
break;
default:
printf("Exec: Unhandled CODE type");
exit(-1);
}
}
......@@ -2,6 +2,7 @@
#define __CODE_H
#include "desc.h"
#include "thunk.h"
#define CT_LIT 1
#define CT_VAR 2
......@@ -56,8 +57,10 @@ struct VarEntry
struct AppEntry
{
struct Code base;
struct VarEntry* var;
struct VarEntry* var; // TODO: remove * here (embed VarEntry to save a space of one pointer)
struct Code* args[];
};
struct Thunk* exec(Code* expr);
#endif // __CODE_H
\ No newline at end of file
g++ -Wno-write-strings -Wno-pointer-arith main.c desc.c mem.c parse.c prim.c -o main
g++ -Wno-write-strings -Wno-pointer-arith main.c desc.c mem.c parse.c prim.c code.c thunk.c -o main
......@@ -12,6 +12,8 @@
#define FT_SLICE 6
#define FT_PRIM 7
// LIMITATION: maximum 32 arguments
struct Desc
{
unsigned int type : 3;
......@@ -25,7 +27,7 @@ struct FunEntry
union
{
char* parseCont;
Code* body;
struct Code* body;
};
char name[];
};
......
......@@ -6,6 +6,7 @@
#include "desc.h"
#include "prim.h"
#include "parse.h"
#include "code.h"
int main()
{
......@@ -13,12 +14,23 @@ int main()
init_desc();
init_prim();
char* stream = "40 R10 example._R2 1 9 example.a9 example.b37 F12 example.g_482 3 AF3 add2 VA0 VA1 39 C9 example.fAF12 example.g_482 LI1 LI2 55 F13 example.Start0 0 AF10 example._R2 LI1 VF9 example.f29 F4 main0 0 VF13 example.Start";
int res = parse(&stream, strlen(stream));
//char* funstream = "40 R10 example._R2 1 9 example.a9 example.b37 F12 example.g_482 3 AF3 add2 VA0 VA1 39 C9 example.fAF12 example.g_482 LI1 LI2 55 F13 example.Start0 0 AF10 example._R2 LI1 VF9 example.f29 F4 main0 0 VF13 example.Start";
char* funstream = "25 F13 example.Start0 0 LI1 29 F4 main0 0 VF13 example.Start";
printf("%d", res);
int nrfuns = parse(&funstream, strlen(funstream));
printf("Number of functions parsed: %d\n", nrfuns);
// TODO: put it into a special "expression" space, instead of "code"
//char *exprstream = "VF13 example.Start";
char *exprstream = "LI1 ";
Code* expr = parseTerm(&exprstream);
Thunk* res = exec(expr);
print(res);
// add_fun("jimbo", (Desc*) "artist");
// char *tval = (char*) find_fun("jimbo");
// printf ("shortcut tval for jimbo = %s\n", tval);
......
......@@ -4,11 +4,13 @@
int desc_alloc;
int code_alloc;
int heap_alloc;
void init_mem()
{
desc_alloc = 0;
code_alloc = 0;
heap_alloc = 1;
}
void* alloc_desc(int size)
......@@ -21,4 +23,10 @@ void* alloc_code(int size)
{
code_alloc += size;
return malloc(size);
}
void* alloc_heap(int size)
{
heap_alloc += size;
return malloc(size);
}
\ No newline at end of file
......@@ -9,4 +9,7 @@ void* alloc_desc(int size);
// TODO: inline
void* alloc_code(int size);
// TODO: inline
void* alloc_heap(int size);
#endif // __MEM_H
......@@ -338,7 +338,7 @@ AppEntry* parseApp(char **ptr)
Code* parseTerm(char **ptr)
{
// 1. Type char
char type = *(*ptr)++;
char type = *(*ptr)++;
switch(type)
{
......
#ifndef __PARSE_H
#define __PARSE_H
#include "code.h"
#define MAX_IDENTIFIER_LENGTH 256
int parse(char** ptr, int length);
Code* parseTerm(char **ptr);
#endif // __PARSE_H
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "thunk.h"
#include "desc.h"
#include "mem.h"
struct Thunk* createI(int i)
{
Thunk* thunk = (Thunk*) alloc_heap(sizeof(Thunk));
thunk->desc = (Desc*) __INT__;
thunk->_int = i;
return thunk;
}
void print(Thunk* thunk)
{
if((FunEntry*) thunk->desc == __INT__)
{
printf("%d", thunk->_int);
}
else
{
printf("print: unhandled DESC\n");
exit(-1);
}
}
\ No newline at end of file
#ifndef __THUNK_H
#define __THUNK_H
#include "desc.h"
#include "code.h"
struct Thunk
{
struct Desc* desc;
union
{
int _int;
double _real; // TODO: move "real" out of here, too long
char _char;
int _bool;
struct CleanString* _string_ptr;
//struct CleanString _string;
};
};
struct Thunk* createI(int i);
void print(Thunk* thunk);
#endif // __THUNK_H
\ 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