Commit 567aa67a authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

array_create1 and array_update work

parent c89e7217
......@@ -260,9 +260,9 @@ void __string_select(int dst_idx)
target->_int = (char) chars[pos->_int];
}
Thunk* string_create(Thunk* target, int len)
Thunk* array_create(Thunk* target, int elementsize, int len, ArrayElementType type)
{
int newsize = sizeof (Desc) + sizeof (Array) + len;
int newsize = sizeof (Desc) + sizeof (Array) + len * elementsize;
if(target == NULL)
{
......@@ -277,14 +277,19 @@ Thunk* string_create(Thunk* target, int len)
}
target->desc = (Desc*) __ARRAY__;
target->_array.is_string = true;
target->_array.is_boxed = true;
target->_array.bytes_per_elem = 1;
target->_array.type = type;
target->_array.is_boxed = type != AET_OTHER;
target->_array.bytes_per_elem = elementsize;
target->_array.length = len;
return target;
}
Thunk* string_create(Thunk* target, int len)
{
return array_create(target, 1, len, AET_CHAR);
}
void __string_create1(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
......@@ -292,6 +297,34 @@ void __string_create1(int dst_idx)
set_return(dst_idx, string_create(target, len->_int));
}
void __array_create1(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
Thunk* len = arg(1);
set_return(dst_idx, array_create(target, sizeof(void*), len->_int, AET_OTHER));
}
void __array_create1_b_i(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
Thunk* len = arg(1);
set_return(dst_idx, array_create(target, sizeof(int), len->_int, AET_INT));
}
void __array_create1_b_b(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
Thunk* len = arg(1);
set_return(dst_idx, array_create(target, sizeof(char), len->_int, AET_BOOL));
}
void __array_create1_b_r(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
Thunk* len = arg(1);
set_return(dst_idx, array_create(target, sizeof(double), len->_int, AET_REAL));
}
void __string_create2(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
......@@ -336,6 +369,50 @@ void __string_update(int dst_idx)
set_return(dst_idx, target);
}
void __array_update(int dst_idx)
{
Thunk* arr = arg(3);
Thunk* idx = arg(2);
Thunk* elem = arg(1);
arr->_array._elems[idx->_int] = elem;
set_return(dst_idx, arr);
}
void __array_update_b_i(int dst_idx)
{
Thunk* arr = arg(3);
Thunk* idx = arg(2);
Thunk* elem = arg(1);
arr->_array._ints[idx->_int] = elem->_int;
set_return(dst_idx, arr);
}
void __array_update_b_b(int dst_idx)
{
Thunk* arr = arg(3);
Thunk* idx = arg(2);
Thunk* elem = arg(1);
arr->_array._bools[idx->_int] = (unsigned char) elem->_int;
set_return(dst_idx, arr);
}
void __array_update_b_r(int dst_idx)
{
Thunk* arr = arg(3);
Thunk* idx = arg(2);
Thunk* elem = arg(1);
arr->_array._reals[idx->_int] = elem->_real;
set_return(dst_idx, arr);
}
void __string_slice(int dst_idx)
{
Thunk* target = get_dst(dst_idx);
......@@ -611,6 +688,18 @@ void init_prim() {
add_prim(1, 0b000, 0, "S2R", &__S2R);
add_prim(1, 0b001, 0, "C2S", &__C2S);
add_prim(1, 0b001, 0, "array_create1", &__array_create1);
add_prim(1, 0b001, 0, "array_create1_lazy", &__array_create1);
add_prim(1, 0b001, 0, "array_create1_B_I", &__array_create1_b_i);
add_prim(1, 0b001, 0, "array_create1_B_B", &__array_create1_b_b);
add_prim(1, 0b001, 0, "array_create1_B_R", &__array_create1_b_r);
add_prim(3, 0b010, 0, "array_update", &__array_update);
add_prim(3, 0b010, 0, "array_update_lazy", &__array_update);
add_prim(3, 0b110, 0, "array_update_B_I", &__array_update_b_i);
add_prim(3, 0b110, 0, "array_update_B_B", &__array_update_b_b);
add_prim(3, 0b110, 0, "array_update_B_R", &__array_update_b_r);
add_prim(1, 0b000, 1, "string_size", &__string_size);
add_prim(2, 0b010, 1, "string_select", &__string_select);
add_prim(1, 0b001, 0, "string_create1", &__string_create1);
......
......@@ -86,26 +86,68 @@ void print(bool force) {
} else if ((FunEntry*) thunk->desc == __REAL__) {
printf("%G", thunk->_real);
} 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_PTR__) {
printf("\"");
for(int i=0; i< thunk->_string_ptr->length; i++)
{
printf("%c", thunk->_string_ptr->chars[i]);
}
printf("\"");
} else if ((FunEntry*) thunk->desc == __ARRAY__) {
if(thunk->_array.is_string)
if(thunk->_array.type == AET_CHAR)
{
printf("\"");
for(int i=0; i< thunk->_array.length; i++)
{
printf("%c", thunk->_array._chars[i]);
}
}
printf("\"");
}
else
{
printf("print: unhandled ARRAY type\n");
bool first = true;
printf("{");
for(int i=0; i< thunk->_array.length; i++)
{
if(!first)
{
printf(",");
}
else
{
first = false;
}
switch(thunk->_array.type)
{
case AET_BOOL:
if(thunk->_array._bools[i])
{
printf("True");
}
else
{
printf("False");
}
break;
case AET_CHAR:
printf("%c", thunk->_array._chars[i]);
break;
case AET_INT:
printf("%d", thunk->_array._ints[i]);
break;
case AET_REAL:
printf("%G", thunk->_array._reals[i]);
break;
case AET_OTHER:
push_a(thunk->_array._elems[i]);
print(true);
break;
}
}
printf("}");
}
} else {
printf("print: unhandled BOXED LIT\n");
......
......@@ -17,18 +17,29 @@ typedef struct __attribute__((packed)) CleanString {
char chars[];
} CleanString;
enum ArrayElementType {
AET_BOOL,
AET_INT,
AET_CHAR,
AET_REAL,
AET_OTHER
};
typedef struct __attribute__((packed)) Array {
struct
{
unsigned int is_string : 1;
ArrayElementType type : 3;
unsigned int is_boxed : 1;
unsigned int bytes_per_elem : 3;
unsigned int length : 27; // LIMITATION :)
unsigned int length : 25; // LIMITATION :)
};
union
{
char _chars[];
Thunk* _elems[]; //
int _ints[];
double _reals[];
unsigned char _bools[];
Thunk* _elems[];
};
} Array;
......
......@@ -262,5 +262,5 @@ line15 = ['voorouder(X1,X2) :- ouder(X1,X3),voorouder(X3,X2).']
line16 = ['ouder(siem,nel).']
line17 = ['ouder(cornelia,nel).']
//Start = parsetest 10
Start = msol 500
\ No newline at end of file
Start = parsetest 10
//Start = msol 500
\ No newline at end of file
[_Tuple6 ["123"] [{1,2,3}] [{True,False,True}] [{1.1,2.2,3.3}] [{[1],[2],[3]}] [{[1],[2],[3]}]]
\ No newline at end of file
main = test.Start
test.Start = _Tuple6 test.a test.b test.c test.d test.e test.f
test.f = array_update_lazy (array_update_lazy (array_update_lazy (array_create1_lazy 3) 2 3) 1 2) 0 1
test.e = array_update (array_update (array_update (array_create1 3) 2 3) 1 2) 0 1
test.d = array_update_B_R (array_update_B_R (array_update_B_R (array_create1_B_R 3) 2 3.3) 1 2.2) 0 1.1
test.c = array_update_B_B (array_update_B_B (array_update_B_B (array_create1_B_B 3) 2 True) 1 False) 0 True
test.b = array_update_B_I (array_update_B_I (array_update_B_I (array_create1_B_I 3) 2 3) 1 2) 0 1
test.a = string_update (string_update (string_update (string_create1 3) 2 '3') 1 '2') 0 '1'
[_Tuple2 [9] [nine]]
\ No newline at end of file
[_Tuple2 [9] ["nine"]]
\ No newline at end of file
[Hello World!]
\ No newline at end of file
["Hello World!"]
\ No newline at end of file
[Hello World!]
\ No newline at end of file
["Hello World!"]
\ No newline at end of file
[B]
\ No newline at end of file
["B"]
\ No newline at end of file
[_Tuple2 [B] [A]]
\ No newline at end of file
[_Tuple2 ["B"] ["A"]]
\ No newline at end of file
[World]
\ No newline at end of file
["World"]
\ No newline at end of file
[hello world!]
\ No newline at end of file
["hello 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