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

_very_ basic string support, only allows passing around constant strings

parent 386d1cf6
......@@ -4,13 +4,22 @@
#include "thunk.h"
enum CodeType {
CT_VAR, CT_VAR_STRICT, CT_VAR_UNBOXED,
CT_VAR, // on heap
CT_VAR_STRICT, // on heap
CT_VAR_UNBOXED, // on heap or B stack
CT_APP_PRIM1,
CT_APP_PRIM2,
CT_APP_THUNK, CT_APP_DYN,
CT_APP_FUN, CT_APP_FUN1, CT_APP_FUN2, CT_APP_FUN_TR,
CT_SELECT_ADT, CT_SELECT_LIT, CT_IF, CT_LET,
CT_THUNK
CT_APP_THUNK, // ADT, record, or not saturated app
CT_APP_DYN, // function part is a variable
CT_APP_FUN,
CT_APP_FUN1,
CT_APP_FUN2,
CT_APP_FUN_TR, // tail recursive
CT_SELECT_ADT,
CT_SELECT_LIT,
CT_IF,
CT_LET,
CT_THUNK // constant, always fits the B stack
};
struct Code {
......
......@@ -104,6 +104,7 @@ void init_desc() {
__REAL__ = alloc_prim("REAL");
__STRING__ = alloc_prim("STRING");
__STRING_PTR__ = alloc_prim("STRING");
__ARRAY__ = alloc_prim("ARRAY");
}
......@@ -113,6 +114,7 @@ struct FunEntry* __CHAR__;
struct FunEntry* __REAL__;
struct FunEntry* __STRING__;
struct FunEntry* __STRING_PTR__;
struct FunEntry* __ARRAY__;
struct FunEntry* __FORWARD_PTR__;
......
......@@ -77,6 +77,8 @@ extern struct FunEntry* __CHAR__;
extern struct FunEntry* __REAL__;
extern struct FunEntry* __STRING__;
// For constants, always points to the code area
extern struct FunEntry* __STRING_PTR__;
extern struct FunEntry* __ARRAY__;
extern struct FunEntry* __FORWARD_PTR__;
......
......@@ -241,12 +241,7 @@ ThunkEntry* parseLit(char **ptr) {
// 1. Type char
char type = *(*ptr)++;
int strlen = 0;
if (type == 'S') {
if (!parseInt(ptr, &strlen)) return 0;
}
struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry) + strlen);
struct ThunkEntry* entry = (ThunkEntry*) alloc_code(sizeof (ThunkEntry));
entry->base.type = CT_THUNK;
switch (type) {
......@@ -281,8 +276,17 @@ ThunkEntry* parseLit(char **ptr) {
case 'S': // String
{
// TODO
return 0;
int strlen = 0;
if (!parseInt(ptr, &strlen)) return 0;
CleanString* strptr = (CleanString*) alloc_code(sizeof (CleanString) + strlen);
strptr->length = strlen;
memcpy(&strptr->chars, *ptr, strlen);
*ptr += strlen;
entry->thunk.desc = (Desc*) __STRING_PTR__;
entry->thunk._string_ptr = strptr;
break;
}
}
......
......@@ -71,6 +71,16 @@ void print(bool force) {
}
} else if ((FunEntry*) thunk->desc == __CHAR__) {
printf("%c", thunk->_char);
} else if ((FunEntry*) thunk->desc == __STRING_PTR__) {
for(int i=0; i< thunk->_string_ptr->length; i++)
{
printf("%c", thunk->_string_ptr->chars[i]);
}
} else if ((FunEntry*) thunk->desc == __STRING__) {
for(int i=0; i< thunk->_string.length; i++)
{
printf("%c", thunk->_string.chars[i]);
}
} else {
printf("print: unhandled BOXED LIT\n");
printDesc(thunk->desc);
......
......@@ -24,8 +24,8 @@ typedef struct __attribute__((packed)) Thunk {
double _real; // TODO: move "real" out of here, too long (at least on 32 bits)
char _char;
int _bool;
//struct CleanString* _string_ptr;
//struct CleanString _string;
struct CleanString* _string_ptr; // For CT_THUNK
struct CleanString _string;
Thunk* _args[];
};
} Thunk;
......
[Hello World!]
\ No newline at end of file
main = string.Start
string.Start = string.str
string.str = "Hello World!"
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