Skip to content

Commit

Permalink
mass replace array with tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
reginaldford committed Jul 31, 2024
1 parent 104aeb2 commit 973c7fb
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 148 deletions.
16 changes: 9 additions & 7 deletions scripts/replace-all.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#backup your code before you do this
str='s/putln/putLn/g';
find ../src/ -name *.c -exec sed -i $str {} +
find ../src/ -name *.h -exec sed -i $str {} +
find ../src/ -name *.y -exec sed -i $str {} +
find ../src/ -name *.l -exec sed -i $str {} +
find ../sms_src/examples/ -name *.sms -exec sed -i $str {} +
#!/bin/bash
# Backup your code before you do this
str='s/"an array"/a tuple/g'
find ../src/ -name "*.c" -exec sed -i -e "$str" {} +
find ../src/ -name "*.h" -exec sed -i -e "$str" {} +
find ../src/ -name "*.y" -exec sed -i -e "$str" {} +
find ../src/ -name "*.l" -exec sed -i -e "$str" {} +
find ../sms_src/examples/ -name "*.sms" -exec sed -i -e "$str" {} +

2 changes: 1 addition & 1 deletion src/kernel_test/chapter_4.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void test0() {
printf("We survived.\n");
}

// Testing space arrays
// Testing space tuples
int chapter_4(int test) {
int current_test = 0;
TEST(test0());
Expand Down
2 changes: 1 addition & 1 deletion src/kernel_test/kernel_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "chapter_1.h" // Generating many objects and then collecting the garbage.
#include "chapter_2.h" // Verify some expression printing mechanics.
#include "chapter_3.h" // Checks that contexts can scope and that memory is managed correctly
// #include "chapter_4.h" // Testing space array system, for deallocated space
// #include "chapter_4.h" // Testing space tuple system, for deallocated space

#define CHAPTER(f) \
if (chosen_chapter == -1 || chosen_chapter == current_chapter) { \
Expand Down
62 changes: 31 additions & 31 deletions src/main/engine/sm_ast_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
int *time_array = (int *)timeinfo;
sm_expr *result = sm_new_expr_n(SM_ARRAY_EXPR, 9, 9, NULL);
int *time_tuple = (int *)timeinfo;
sm_expr *result = sm_new_expr_n(SM_TUPLE_EXPR, 9, 9, NULL);
for (int i = 0; i < 9; i++)
sm_expr_set_arg(result, i, (sm_object *)sm_new_double(time_array[i]));
sm_expr_set_arg(result, i, (sm_object *)sm_new_double(time_tuple[i]));
return (sm_object *)result;
break;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
status = WEXITSTATUS(status);
if (child_pid == -1)
status = 1;
return (sm_object *)sm_new_expr_2(SM_ARRAY_EXPR, (sm_object *)sm_new_double(child_pid),
return (sm_object *)sm_new_expr_2(SM_TUPLE_EXPR, (sm_object *)sm_new_double(child_pid),
(sm_object *)sm_new_double(status), NULL);
}
case SM_EXEC_EXPR: {
Expand Down Expand Up @@ -189,10 +189,10 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
// Create a new sm_string with the collected output and free the output data
sm_string *result_str = sm_new_string(total_size, output_data);
free(output_data);
// Return a size 2 array with return value and return string
// Return a size 2 tuple with return value and return string
sm_double *output_code = sm_new_double(return_code);
sm_expr *output =
sm_new_expr_2(SM_ARRAY_EXPR, (sm_object *)output_code, (sm_object *)result_str, NULL);
sm_new_expr_2(SM_TUPLE_EXPR, (sm_object *)output_code, (sm_object *)result_str, NULL);
return (sm_object *)output;
break;
}
Expand Down Expand Up @@ -254,8 +254,8 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
i++;
}
closedir(dir);
sm_expr *names_arr = sm_new_expr_n(SM_ARRAY_EXPR, i, i, NULL);
sm_expr *types_arr = sm_new_expr_n(SM_ARRAY_EXPR, i, i, NULL);
sm_expr *names_arr = sm_new_expr_n(SM_TUPLE_EXPR, i, i, NULL);
sm_expr *types_arr = sm_new_expr_n(SM_TUPLE_EXPR, i, i, NULL);
for (uint32_t names_i = 0; names_i < i; names_i++) {
sm_expr_set_arg(names_arr, names_i, (sm_object *)entry_names[names_i]);
if (entry_types[names_i] != 0)
Expand All @@ -264,7 +264,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
sm_expr_set_arg(types_arr, names_i, (sm_object *)sms_false);
}
sm_expr *result =
sm_new_expr_2(SM_ARRAY_EXPR, (sm_object *)names_arr, (sm_object *)types_arr, NULL);
sm_new_expr_2(SM_TUPLE_EXPR, (sm_object *)names_arr, (sm_object *)types_arr, NULL);
return (sm_object *)result;
break;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
case SM_TIME_EXPR: {
struct timeval t;
gettimeofday(&t, NULL);
sm_expr *result = sm_new_expr_n(SM_ARRAY_EXPR, 2, 2, NULL);
sm_expr *result = sm_new_expr_n(SM_TUPLE_EXPR, 2, 2, NULL);
sm_expr_set_arg(result, 0, (sm_object *)sm_new_double(t.tv_sec));
sm_expr_set_arg(result, 1, (sm_object *)sm_new_double(t.tv_usec));
return (sm_object *)result;
Expand Down Expand Up @@ -431,9 +431,9 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
if (num0->my_type != SM_DOUBLE_TYPE)
return (sm_object *)num0;
if (num0->value < 1)
return (sm_object *)sm_new_expr_0(SM_ARRAY_EXPR, NULL);
return (sm_object *)sm_new_expr_0(SM_TUPLE_EXPR, NULL);
sm_double *zero = sm_new_double(0);
sm_expr *output = sm_new_expr_n(SM_ARRAY_EXPR, (int)num0->value, (int)num0->value, NULL);
sm_expr *output = sm_new_expr_n(SM_TUPLE_EXPR, (int)num0->value, (int)num0->value, NULL);
for (int i = 0; i < num0->value; i++)
sm_expr_set_arg(output, i, (sm_object *)zero);
return (sm_object *)output;
Expand Down Expand Up @@ -461,7 +461,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
sms_heap, "Calling part with out of range length value: %i", (int)len->value);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
sm_expr *new_list = sm_new_expr_n(SM_ARRAY_EXPR, (int)len->value, (int)len->value, NULL);
sm_expr *new_list = sm_new_expr_n(SM_TUPLE_EXPR, (int)len->value, (int)len->value, NULL);
for (int i = 0; i < (int)len->value; i++) {
sm_object *element = sm_expr_get_arg(list0, (int)start->value + i);
sm_expr_set_arg(new_list, i, element);
Expand All @@ -479,13 +479,13 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
double repetitions = reps->value;
int original_size = list0->size;
int new_size = (int)(original_size * repetitions);
sm_expr *new_array = sm_new_expr_n(SM_ARRAY_EXPR, new_size, new_size, NULL);
sm_expr *new_tuple = sm_new_expr_n(SM_TUPLE_EXPR, new_size, new_size, NULL);
for (int i = 0; i < new_size; i++) {
int original_index = i % original_size;
sm_object *element = sm_expr_get_arg(list0, original_index);
sm_expr_set_arg(new_array, i, element);
sm_expr_set_arg(new_tuple, i, element);
}
return (sm_object *)new_array;
return (sm_object *)new_tuple;
break;
}
case SM_CAT_EXPR: {
Expand All @@ -498,16 +498,16 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
int size0 = list0->size;
int size1 = list1->size;
int new_size = size0 + size1;
sm_expr *new_array = sm_new_expr_n(SM_ARRAY_EXPR, new_size, new_size, NULL);
sm_expr *new_tuple = sm_new_expr_n(SM_TUPLE_EXPR, new_size, new_size, NULL);
for (int i = 0; i < size0; i++) {
sm_object *element = sm_expr_get_arg(list0, i);
sm_expr_set_arg(new_array, i, element);
sm_expr_set_arg(new_tuple, i, element);
}
for (int i = 0; i < size1; i++) {
sm_object *element = sm_expr_get_arg(list1, i);
sm_expr_set_arg(new_array, size0 + i, element);
sm_expr_set_arg(new_tuple, size0 + i, element);
}
return (sm_object *)new_array;
return (sm_object *)new_tuple;
break;
}
case SM_EXIT_EXPR: {
Expand Down Expand Up @@ -675,7 +675,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
if (cx->my_type != SM_CX_TYPE)
return (sm_object *)cx;
sm_expr *success =
sm_node_keys(cx->content, sm_new_stack_obj(32), sm_new_expr_n(SM_ARRAY_EXPR, 0, 0, NULL));
sm_node_keys(cx->content, sm_new_stack_obj(32), sm_new_expr_n(SM_TUPLE_EXPR, 0, 0, NULL));
if (success)
return (sm_object *)success;
return (sm_object *)sms_false;
Expand All @@ -688,7 +688,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
sm_new_string(50, "Passed something that is not a context to cxValues");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
sm_expr *success = sm_node_values(cx->content, sm_new_expr_n(SM_ARRAY_EXPR, 0, 0, NULL));
sm_expr *success = sm_node_values(cx->content, sm_new_expr_n(SM_TUPLE_EXPR, 0, 0, NULL));
if (success)
return (sm_object *)success;
return (sm_object *)sms_false;
Expand Down Expand Up @@ -734,7 +734,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
sm_symbol *title = sm_new_symbol("typeMismatch", 12);
sm_string *message =
sm_new_fstring_at(sms_heap,
"When using fnSetParams(<fn>,<params>), params must be an array of "
"When using fnSetParams(<fn>,<params>), params must be a tuple of "
"symbols. Parameter %i is not a symbol.",
i);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
Expand Down Expand Up @@ -1146,8 +1146,8 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
struct stat filestat;
sm_expr *output;
if (stat(fname_cstr, &filestat) == 0) {
// build an array with stat information.
output = sm_new_expr_n(SM_ARRAY_EXPR, 16, 16, NULL);
// build a tuple with stat information.
output = sm_new_expr_n(SM_TUPLE_EXPR, 16, 16, NULL);
sm_expr_set_arg(output, 0, (sm_object *)sm_new_double(filestat.st_dev));
sm_expr_set_arg(output, 1, (sm_object *)sm_new_double(filestat.st_ino));
sm_expr_set_arg(output, 2, (sm_object *)sm_new_double(filestat.st_mode));
Expand Down Expand Up @@ -1384,7 +1384,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
if (!expect_type(base_obj, SM_EXPR_TYPE)) {
sm_symbol *title = sm_new_symbol("typeMismatch", 12);
sm_string *message =
sm_new_string(59, "Trying to apply index op to something that is not an array.");
sm_new_string(59, "Trying to apply index op to something that is not a tuple.");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
arr = (sm_expr *)base_obj;
Expand All @@ -1396,8 +1396,8 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
uint32_t index = (int)index_double->value;
if (arr->size < index + 1 || index_double->value < 0) {
sm_string *message = sm_new_fstring_at(
sms_heap, "Index out of range: %i . Array size is %i", index, arr->size);
sm_symbol *title = sm_new_symbol("arrayIndexOutOfBounds", 21);
sms_heap, "Index out of range: %i . tuple size is %i", index, arr->size);
sm_symbol *title = sm_new_symbol("tupleIndexOutOfBounds", 21);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
return sm_expr_get_arg(arr, index);
Expand Down Expand Up @@ -1525,7 +1525,7 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
printf("Error: Index out of range: %i\n", (int)index->value);
sm_symbol *title = sm_new_symbol("indexOutOfBounds", 16);
sm_string *message =
sm_new_fstring_at(sms_heap, "Index %i is out of bounds of array of size %i",
sm_new_fstring_at(sms_heap, "Index %i is out of bounds of tuple of size %i",
(int)index->value, (int)arr->size);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
Expand Down Expand Up @@ -1775,8 +1775,8 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
}
return sm_engine_eval(sm_expr_get_arg(sme, 2), current_cx, sf);
}
case SM_ARRAY_EXPR: {
sm_expr *new_arr = sm_new_expr_n(SM_ARRAY_EXPR, sme->size, sme->size, NULL);
case SM_TUPLE_EXPR: {
sm_expr *new_arr = sm_new_expr_n(SM_TUPLE_EXPR, sme->size, sme->size, NULL);
for (uint32_t i = 0; i < sme->size; i++) {
sm_object *new_val = sm_engine_eval(sm_expr_get_arg(sme, i), current_cx, sf);
sm_expr_set_arg(new_arr, i, new_val);
Expand Down
10 changes: 5 additions & 5 deletions src/main/engine/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ sm_double *sm_str_find(sm_string *str, sm_string *to_find) {
}
}

// Return an array with all of the parts of str,
// Return a tuple with all of the parts of str,
// split up into substrings, where the instances of needle are separated
// So, strSplit("abc123abc123","c") == [ "ab" , "c", "123ab", "c" , "123" ]
// The needle in the result is the same instance as the one given in the input.
sm_expr *sm_str_split(sm_string *str, sm_string *needle) {
if (needle->size == 0 || str->size == 0)
return sm_new_expr(SM_ARRAY_EXPR, (sm_object *)sm_new_string(str->size, &str->content), NULL);
return sm_new_expr(SM_TUPLE_EXPR, (sm_object *)sm_new_string(str->size, &str->content), NULL);
uint32_t i, j, num_needles = 0;
uint32_t needles[str->size / needle->size + 1];
char *str_cstr = &(str->content);
Expand All @@ -39,7 +39,7 @@ sm_expr *sm_str_split(sm_string *str, sm_string *needle) {
}
// Shortcut for no needles
if (num_needles == 0)
return sm_new_expr(SM_ARRAY_EXPR, (sm_object *)sm_new_string(str->size, &str->content), NULL);
return sm_new_expr(SM_TUPLE_EXPR, (sm_object *)sm_new_string(str->size, &str->content), NULL);
// Count the number of adjacent needles
uint32_t num_adjacents = 0;
for (uint32_t i = 0; i + 1 < num_needles; i++) {
Expand All @@ -54,8 +54,8 @@ sm_expr *sm_str_split(sm_string *str, sm_string *needle) {
border_chunks++;
// This should be the exact capacity necessary to store the string parts
uint32_t capacity = num_needles * 2 - 1 + border_chunks - num_adjacents;
// Create and return the resulting array expression
sm_expr *result = sm_new_expr_n(SM_ARRAY_EXPR, 0, capacity, NULL);
// Create and return the resulting tuple expression
sm_expr *result = sm_new_expr_n(SM_TUPLE_EXPR, 0, capacity, NULL);
uint32_t current_needle = 0;
uint32_t current_result = 0;
i = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/main/engine/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

/// Return the index of the substring to_find within str, or return -1
sm_double *sm_str_find(sm_string *str, sm_string *to_find);
/// Returns an array with all portions of str, split by the substing, needle.
/// Returns a tuple with all portions of str, split by the substing, needle.
/// @example strSplit("abc123","1") => [ "abc" , "1" , "23"]
sm_expr *sm_str_split(sm_string *str, sm_string *needle);
18 changes: 9 additions & 9 deletions src/main/memory/sm_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ sm_heap *sm_new_heap(uint32_t capacity) {
}

// Check for adequate space size
// If it is acceptable, remove it from the space array and return the space, else return NULL
// If it is acceptable, remove it from the space tuple and return the space, else return NULL
sm_space *check_space(uint32_t size, uint32_t index) {
if (index + 1 <= sm_global_space_array(NULL)->size) {
if (index + 1 <= sm_global_space_tuple(NULL)->size) {
uint32_t space_size =
sm_get_space_array(sm_global_space_array(NULL))[index]->my_type - SM_SPACE_TYPE;
sm_get_space_tuple(sm_global_space_tuple(NULL))[index]->my_type - SM_SPACE_TYPE;
if (space_size >= size) {
sm_space *good_space = sm_get_space_array(sm_global_space_array(NULL))[index];
sm_space *good_space = sm_get_space_tuple(sm_global_space_tuple(NULL))[index];
return good_space;
}
return NULL;
}
return NULL;
}

// Binary search for space in a size ordered space array
sm_search_result find_space_within_bounds(sm_space_array *ssa, uint32_t size, uint32_t lower_limit,
// Binary search for space in a size ordered space tuple
sm_search_result find_space_within_bounds(sm_space_tuple *ssa, uint32_t size, uint32_t lower_limit,
uint32_t upper_limit) {
sm_space **space_array = sm_get_space_array(ssa);
sm_space **space_tuple = sm_get_space_tuple(ssa);
int comparison = 1;
uint32_t guess_point = (upper_limit + lower_limit) / 2.0;
while (lower_limit < upper_limit && comparison != 0) {
comparison = space_array[guess_point]->my_type - SM_SPACE_TYPE - size;
comparison = space_tuple[guess_point]->my_type - SM_SPACE_TYPE - size;
if (comparison == 0)
return (sm_search_result){.found = true, .index = guess_point};
else if (comparison > 0)
Expand All @@ -53,7 +53,7 @@ sm_search_result find_space_within_bounds(sm_space_array *ssa, uint32_t size, ui
lower_limit = guess_point + 1;
guess_point = (upper_limit + lower_limit) / 2.0;
}
comparison = space_array[guess_point]->my_type - SM_SPACE_TYPE - size;
comparison = space_tuple[guess_point]->my_type - SM_SPACE_TYPE - size;
if (comparison == 0)
return (sm_search_result){.found = true, .index = guess_point};
if (comparison < 0) {
Expand Down
Loading

0 comments on commit 973c7fb

Please sign in to comment.