Skip to content

Commit

Permalink
decided to split repeat to repeat/ui8Repeat/f64Repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
reginaldford committed Sep 3, 2024
1 parent 8f297c5 commit 03301f3
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 92 deletions.
167 changes: 107 additions & 60 deletions src/main/engine/sm_ast_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,71 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
return (sm_object *)new_list;
break;
}
case SM_REPEAT_EXPR: {
case SM_UI8_REPEAT_EXPR: {
sm_expr *obj =
(sm_expr *)eager_type_check2(sme, 0, SM_EXPR_TYPE, SM_ARRAY_TYPE, current_cx, sf);
if (obj->my_type == SM_ERR_TYPE)
return (sm_object *)obj;

sm_f64 *repeat_count_obj = (sm_f64 *)eager_type_check(sme, 1, SM_F64_TYPE, current_cx, sf);
if (!repeat_count_obj || repeat_count_obj->my_type == SM_ERR_TYPE)
return (sm_object *)repeat_count_obj;
uint32_t repeat_count = (uint32_t)repeat_count_obj->value;

if (repeat_count == 0)
return (sm_object *)sm_new_array(SM_UI8_TYPE, 0, NULL, sizeof(sm_space));

uint32_t total_size = 0;
sm_space *new_space = NULL;
ui8 *dst_data = NULL;

switch (obj->my_type) {
case SM_EXPR_TYPE: {
sm_expr *arr = (sm_expr *)obj;
total_size = arr->size * repeat_count;
new_space = sm_new_space(total_size);
dst_data = (ui8 *)(new_space + 1);
for (uint32_t i = 0; i < repeat_count; i++)
for (uint32_t j = 0; j < arr->size; j++)
dst_data[i * arr->size + j] = (ui8)(uintptr_t)sm_expr_get_arg(arr, j);
break;
}
case SM_ARRAY_TYPE: {
sm_array *arr = (sm_array *)obj;
total_size = arr->size * repeat_count;
new_space = sm_new_space(total_size);
dst_data = (ui8 *)(new_space + 1);
switch (arr->inner_type) {
case SM_UI8_TYPE: {
ui8 *src_data = sm_ui8_array_get_start(arr);
for (uint32_t i = 0; i < repeat_count; i++)
memcpy(dst_data + i * arr->size, src_data, arr->size);
break;
}
case SM_F64_TYPE: {
f64 *src_data = sm_f64_array_get_start(arr);
for (uint32_t i = 0; i < repeat_count; i++)
for (uint32_t j = 0; j < arr->size; j++)
dst_data[i * arr->size + j] = (ui8)src_data[j];
break;
}
default:
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidType", 11),
sm_new_string(29, "Unsupported array type for ui8 repeat"), NULL, 0, NULL);
}
break;
}
default:
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidType", 11), sm_new_string(26, "Invalid type for ui8 repeat"), NULL,
0, NULL);
}

return (sm_object *)sm_new_array(SM_UI8_TYPE, total_size, (sm_object *)new_space,
sizeof(sm_space));
}
case SM_F64_REPEAT_EXPR: {
sm_expr *obj =
(sm_expr *)eager_type_check2(sme, 0, SM_EXPR_TYPE, SM_ARRAY_TYPE, current_cx, sf);
if (!obj || obj->my_type == SM_ERR_TYPE)
Expand All @@ -570,83 +634,66 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
uint32_t repeat_count = (uint32_t)repeat_count_obj->value;

if (repeat_count == 0)
return (obj->my_type == SM_EXPR_TYPE)
? (sm_object *)sm_new_expr_n(((sm_expr *)obj)->op, 0, 0, NULL)
: (sm_object *)sm_new_array(((sm_array *)obj)->inner_type, 0, NULL,
sizeof(sm_space));
return (sm_object *)sm_new_array(SM_F64_TYPE, 0, NULL, sizeof(sm_space));

uint32_t total_size = 0;
sm_space *new_space = NULL;
f64 *dst_data = NULL;

switch (obj->my_type) {
case SM_EXPR_TYPE: {
sm_expr *arr = (sm_expr *)obj;
uint32_t new_size = arr->size * repeat_count;
sm_expr *result_expr = sm_new_expr_n(arr->op, new_size, new_size, NULL);
if (!result_expr)
sm_expr *arr = (sm_expr *)obj;
total_size = arr->size * repeat_count;
new_space = sm_new_space(total_size * sizeof(f64));
if (!new_space)
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("MemoryError", 10),
sm_new_string(44, "Failed to allocate memory for repeated tuple"), NULL, 0, NULL);

// Repeating the expression arguments
for (uint32_t i = 0; i < repeat_count; i++) {
for (uint32_t j = 0; j < arr->size; j++) {
sm_object *arg = sm_expr_get_arg(arr, j);
if (!arg)
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidMemoryAccess", 19),
sm_new_string(40, "Invalid memory access during repetition"), NULL, 0, NULL);
sm_expr_set_arg(result_expr, i * arr->size + j, arg);
}
}
return (sm_object *)result_expr;
sm_new_string(44, "Failed to allocate memory for f64 repeat from expr"), NULL, 0, NULL);
dst_data = (f64 *)(new_space + 1);
for (uint32_t i = 0; i < repeat_count; i++)
for (uint32_t j = 0; j < arr->size; j++)
dst_data[i * arr->size + j] = (f64)(uintptr_t)sm_expr_get_arg(arr, j);
break;
}
case SM_ARRAY_TYPE: {
sm_array *arr = (sm_array *)obj;
uint32_t element_size = (arr->inner_type == SM_UI8_TYPE) ? sizeof(ui8) : sizeof(f64);
uint32_t new_size = arr->size * repeat_count * element_size;
sm_space *new_space = sm_new_space(new_size);
sm_array *arr = (sm_array *)obj;
total_size = arr->size * repeat_count;
new_space = sm_new_space(total_size * sizeof(f64));
if (!new_space)
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("MemoryError", 10),
sm_new_string(44, "Failed to allocate memory for repeated array"), NULL, 0, NULL);

void *src_data, *dst_data = (char *)(new_space + 1); // Start of data after sm_space header
sm_new_string(44, "Failed to allocate memory for f64 repeat from array"), NULL, 0,
NULL);
dst_data = (f64 *)(new_space + 1);
switch (arr->inner_type) {
case SM_F64_TYPE:
src_data = sm_f64_array_get_start(arr); // Get pointer to actual f64 data
if (!src_data)
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidMemoryAccess", 19),
sm_new_string(27, "Invalid f64 array access"), NULL, 0, NULL);
case SM_UI8_TYPE: {
ui8 *src_data = sm_ui8_array_get_start(arr);
for (uint32_t i = 0; i < repeat_count; i++)
memcpy((char *)dst_data + i * arr->size * sizeof(f64), src_data,
arr->size * sizeof(f64));
for (uint32_t j = 0; j < arr->size; j++)
dst_data[i * arr->size + j] = (f64)src_data[j];
break;
case SM_UI8_TYPE:
src_data = sm_ui8_array_get_start(arr); // Get pointer to actual ui8 data
if (!src_data)
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidMemoryAccess", 19),
sm_new_string(27, "Invalid ui8 array access"), NULL, 0, NULL);
}
case SM_F64_TYPE: {
f64 *src_data = sm_f64_array_get_start(arr);
for (uint32_t i = 0; i < repeat_count; i++)
memcpy((char *)dst_data + i * arr->size * sizeof(ui8), src_data,
arr->size * sizeof(ui8));
memcpy(dst_data + i * arr->size, src_data, arr->size * sizeof(f64));
break;
default: {
sm_symbol *title = sm_new_symbol("arrayTypeUnknownError", 19);
sm_string *message =
sm_new_fstring_at(sms_heap, "Unsupported array inner type %i", (int)arr->inner_type);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
default:
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidType", 11),
sm_new_string(29, "Unsupported array type for f64 repeat"), NULL, 0, NULL);
}
return (sm_object *)sm_new_array(arr->inner_type, new_size / element_size,
(sm_object *)new_space, sizeof(sm_space));
}
default: {
sm_symbol *title = sm_new_symbol("invalidExpressionType", 19);
sm_string *message =
sm_new_fstring_at(sms_heap, "Invalid expression type %i", (int)obj->my_type);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
break;
}
default:
return (sm_object *)sm_new_error_from_strings(
sm_new_symbol("InvalidType", 11), sm_new_string(26, "Invalid type for f64 repeat"), NULL,
0, NULL);
}

return (sm_object *)sm_new_array(SM_F64_TYPE, total_size, (sm_object *)new_space,
sizeof(sm_space));
}
case SM_CAT_EXPR: {
sm_expr *list0 = (sm_expr *)eager_type_check(sme, 0, SM_EXPR_TYPE, current_cx, sf);
Expand Down Expand Up @@ -1137,7 +1184,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
case SM_UI8_TYPE: {
// Handle ui8 arrays
content_cstr = (char *)content_array->content + sizeof(sm_space);
content_size = content_array->size * sizeof(ui8); // Size in bytes
content_size = content_array->size; // Size in bytes
break;
}
case SM_F64_TYPE: {
Expand Down
56 changes: 33 additions & 23 deletions src/main/object/sm_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,32 @@ uint32_t sm_array_sprint(sm_array *a, char *buffer, bool fake) {
}

uint32_t sm_f64_array_contents_sprint(sm_array *array, char *buffer, bool fake) {
uint32_t cursor = 0;
uint32_t bufferCursor = 0;
uint32_t i = 0;
char fakeBuf[64]; // Temporary buffer used when fake is true
if (fake)
buffer = fakeBuf;
uint32_t cursor = 0;
uint32_t i = 0;

for (i = 0; i < array->size; i++) {
if (fake)
bufferCursor = 0; // Reset bufferCursor to 0 if we are faking
f64 value = sm_f64_array_get_bare(array, i);
// Write the value to the buffer, ensuring no overflow
bufferCursor = snprintf(&buffer[cursor], 25, "%.16g", value); // Write value to the buffer
if (bufferCursor >= 25)
bufferCursor = 24; // Safeguard against overflow from snprintf
cursor += bufferCursor;
// Estimate maximum buffer size needed for one float
char num_buf[36]; // Enough to hold any formatted float
int len = snprintf(num_buf, sizeof(num_buf), "%.16g", value);

if (!fake)
if (len > 0)
memcpy(&buffer[cursor], num_buf, len); // Copy the formatted string to the buffer
cursor += len;
// Add a comma if it's not the last element
if (i + 1 < array->size) {
if (!fake)
buffer[cursor] = ',';
cursor++;
}
}

// Null-terminate the buffer if not faking
if (!fake)
if (!fake) {
buffer[cursor] = '\0';
}

return cursor;
}

Expand All @@ -100,20 +101,29 @@ uint32_t sm_ui8_array_contents_sprint(sm_array *array, char *buffer, bool fake)
return 0;
uint32_t cursor = 0;
uint32_t i = 0;
char tmp[array->size];
if (fake)
buffer = tmp;
for (; i + 1 < array->size; i++) {
cursor += sprintf(&buffer[cursor], "%i", sm_ui8_array_get_bare(array, i));
for (i = 0; i < array->size; i++) {
uint8_t value = sm_ui8_array_get_bare(array, i);
// Calculate the length needed to print the integer
uint32_t len = (value == 0) ? 1 : (uint32_t)log10(value) + 1;
if (!fake)
buffer[cursor] = ',';
cursor++;
cursor += sprintf(&buffer[cursor], "%u", value);
else
cursor += len; // Only increment cursor by the length if faking

// Add a comma if it's not the last element
if (i + 1 < array->size) {
if (!fake)
buffer[cursor] = ',';
cursor++;
}
}
if (array->size > 0)
cursor += sprintf(&buffer[cursor], "%i", sm_ui8_array_get_bare(array, array->size - 1));
// Null-terminate the buffer if not faking
if (!fake)
buffer[cursor] = '\0';
return cursor;
}


uint32_t sm_array_contents_sprint(sm_array *a, char *buffer, bool fake) {
uint32_t cursor = 0;
switch (a->inner_type) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/object/sm_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ enum SM_EXPR_TYPE {
SM_PWD_EXPR,
SM_LET_EXPR,
SM_NEW_F64_EXPR,
SM_F64_REPEAT_EXPR,
SM_NEW_UI8_EXPR,
SM_UI8_REPEAT_EXPR,
SM_ASSIGN_EXPR,
SM_ASSIGN_DOT_EXPR,
SM_ASSIGN_LOCAL_EXPR,
Expand Down
3 changes: 2 additions & 1 deletion src/main/parser/sms.l
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exit {return EXIT;}
help {return HELP;}

f64 {return NEW_F64;}
f64Repeat {return F64_REPEAT;}

"_+" {return IPLUS;}
"_-" {return IMINUS;}
Expand Down Expand Up @@ -240,7 +241,6 @@ _gc {return GC;}
}

ui8 { return NEW_UI8; }

ui8\((-?[0-9]+)\) {
char *num_str = yytext + 4; // Skip "ui8("
// Find the position of the closing parenthesis
Expand All @@ -258,6 +258,7 @@ ui8\((-?[0-9]+)\) {
yylval.ui8 = number;
return UI8;
}
ui8Repeat { return UI8_REPEAT ; }


[\<\>\=] { return yytext[0]; }
Expand Down
4 changes: 4 additions & 0 deletions src/main/parser/sms.y
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ int parsing_fpath_len;
%token IS
%token DOT
%token <expr> NEW_F64
%token <expr> F64_REPEAT
%token <num> F64
%token <ui8> UI8
%token <ui8> NEW_UI8
%token <expr> UI8_REPEAT
%token <integer> INTEGER
%token <expr> IPLUS
%token <expr> IMINUS
Expand Down Expand Up @@ -361,6 +363,7 @@ EXPR : SELF { $$ = (sm_expr*)sm_new_self(); }
| F64 { $$ = (sm_expr*)sm_new_f64($1);}
| UI8 { $$ = (sm_expr*)sm_new_ui8($1);}
| NEW_UI8 '(' EXPR ')' {$$ = sm_new_expr(SM_NEW_UI8_EXPR, (sm_object *)$3, _note()); }
| UI8_REPEAT '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_UI8_REPEAT_EXPR, (sm_object *)$3, (sm_object*)$5, _note()); }
| INTEGER { $$ = (sm_expr*)sm_new_f64($1);}
| SYM INC { $$ = sm_new_expr(SM_INC_EXPR,(sm_object*)$1,_note()); }
| SYM DEC { $$ = sm_new_expr(SM_DEC_EXPR,(sm_object*)$1,_note()); }
Expand Down Expand Up @@ -473,6 +476,7 @@ EXPR : SELF { $$ = (sm_expr*)sm_new_self(); }
| XP_SETOP '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_XP_SET_OP_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| XP_OPSYM '(' EXPR ')' {$$ = sm_new_expr(SM_XP_OP_SYM_EXPR,(sm_object*)$3, _note());}
| NEW_F64 '(' EXPR ')' { $$ = sm_new_expr(SM_NEW_F64_EXPR, (sm_object*)$3 , _note()); }
| F64_REPEAT '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_F64_REPEAT_EXPR, (sm_object *)$3, (sm_object*)$5, _note()); }
| F64_ARRAY { }
| UI8_ARRAY { }
| FILE_PARSE '(' EXPR ')' {$$ = sm_new_expr(SM_FILE_PARSE_EXPR,(sm_object*)$3, _note());}
Expand Down
18 changes: 10 additions & 8 deletions src/main/sm_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ char *sm_global_fn_name(uint32_t which) {
"pwd", // SM_PWD_EXPR
"let", // SM_LET_EXPR
"f64", // SM_NEW_F64_EXPR
"f64Repeat", // SM_F64_REPEAT_EXPR
"ui8", // SM_NEW_UI8_EXPR
"ui8Repeat", // SM_UI8_REPEAT_EXPR
"=", // SM_ASSIGN_EXPR
"=", // SM_ASSIGN_DOT_EXPR
"=", // SM_ASSIGN_LOCAL_EXPR
Expand Down Expand Up @@ -269,14 +271,14 @@ char *sm_global_fn_name(uint32_t which) {
// Corresponding string length of the string that would come from the sm_global_fn_name(which)
uint32_t sm_global_fn_name_len(uint32_t which) {
static uint64_t response_len[] = {
8, 4, 4, 5, 4, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 3, 3,
3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 2, 3, 3, 4, 3, 3, 3, 4, 4, 3, 2, 2, 3, 6, 6,
5, 3, 7, 6, 4, 6, 8, 12, 5, 5, 4, 2, 2, 2, 1, 1, 2, 2, 5, 5, 5, 5, 5, 3, 5,
4, 5, 2, 11, 5, 5, 5, 8, 8, 12, 5, 4, 7, 6, 8, 6, 8, 5, 9, 11, 7, 8, 10, 8, 6,
6, 6, 12, 12, 12, 10, 5, 4, 4, 3, 6, 6, 4, 5, 5, 4, 3, 3, 2, 3, 3, 4, 7, 8, 11,
8, 11, 4, 7, 7, 7, 6, 6, 6, 6, 7, 8, 4, 8, 7, 9, 11, 8, 6, 9, 3, 6, 12, 13, 8,
9, 7, 4, 4, 5, 6, 6, 6, 11, 8, 8, 3, 5, 8, 10, 9, 7, 8, 1};
8, 4, 4, 5, 4, 2, 2, 3, 3, 3, 9, 3, 9, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 2, 3, 3, 4, 3, 3, 3, 4, 4, 3, 2, 2, 3,
6, 6, 5, 3, 7, 6, 4, 6, 8, 12, 5, 5, 4, 2, 2, 2, 1, 1, 2, 2, 5, 5, 5, 5, 5,
3, 5, 4, 5, 2, 11, 5, 5, 5, 8, 8, 12, 5, 4, 7, 6, 8, 6, 8, 5, 9, 11, 7, 8, 10,
8, 6, 6, 6, 12, 12, 12, 10, 5, 4, 4, 3, 6, 6, 4, 5, 5, 4, 3, 3, 2, 3, 3, 4, 7,
8, 11, 8, 11, 4, 7, 7, 7, 6, 6, 6, 6, 7, 8, 4, 8, 7, 9, 11, 8, 6, 9, 3, 6, 12,
13, 8, 9, 7, 4, 4, 5, 6, 6, 6, 11, 8, 8, 3, 5, 8, 10, 9, 7, 8, 1};


if (which >= sm_global_num_fns())
Expand Down

0 comments on commit 03301f3

Please sign in to comment.