Skip to content

Commit

Permalink
refactor(libs.io,runtime): use sr instead of str, update runtime c co…
Browse files Browse the repository at this point in the history
…de to use sr
  • Loading branch information
JaDogg committed Apr 27, 2024
1 parent b95cb3a commit da30559
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 121 deletions.
118 changes: 51 additions & 67 deletions compiler/carpntr/build/program_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ void yy__console_red(struct yk__bstr);
void yy__console_green(struct yk__bstr);
void yy__console_yellow(struct yk__bstr);
void yy__console_cyan(struct yk__bstr);
yk__sds yy__io_readfile(yk__sds);
bool yy__io_writefile(yk__sds, yk__sds);
yk__sds yy__io_readfile(struct yk__bstr);
bool yy__io_writefile(struct yk__bstr, struct yk__bstr);
yy__toml_Table yy__toml_from_str(struct yk__bstr);
yy__toml_Table yy__toml_get_table(yy__toml_Table, struct yk__bstr);
yy__toml_TomlArray yy__toml_get_array(yy__toml_Table, struct yk__bstr);
Expand All @@ -165,22 +165,22 @@ yk__sds yy__path_remove_extension(yk__sds);
yk__sds yy__array_join(yk__sds*, yk__sds);
yk__sds* yy__array_extend(yk__sds*, yk__sds*);
int32_t yy__strings_get_cstr(yy__c_CStr, int32_t);
int32_t yy__strings_get(yk__sds, int32_t);
bool yy__strings_contains(yk__sds, yk__sds);
int32_t yy__strings_ord(yk__sds);
bool yy__strings_startswith(yk__sds, yk__sds);
yk__sds* yy__strings_split(yk__sds, yk__sds);
int32_t yy__strings_find_char(yk__sds, int32_t);
int32_t yy__strings_get(struct yk__bstr, int32_t);
bool yy__strings_contains(struct yk__bstr, struct yk__bstr);
int32_t yy__strings_ord(struct yk__bstr);
bool yy__strings_startswith(struct yk__bstr, struct yk__bstr);
yk__sds* yy__strings_split(struct yk__bstr, struct yk__bstr);
int32_t yy__strings_find_char(struct yk__bstr, int32_t);
yk__sds yy__strings_from_cstr(yy__c_CStr);
yk__sds yy__strings_from_cstrlen(yy__c_CStr, int32_t);
yk__sds yy__strings_from_cstrlen_after(yy__c_CStr, int32_t, int32_t);
yy__c_CStr yy__strings_null_cstr();
yy__c_CStr yy__strings_to_cstr(yk__sds);
void yy__strings_del_cstr(yy__c_CStr);
void yy__strings_del_str(yy__c_CStr);
yk__sds yy__strings_cut_from(yk__sds, int32_t);
yk__sds yy__strings_mid(yk__sds, int32_t, int32_t);
bool yy__strings_endswith(yk__sds, yk__sds);
yk__sds yy__strings_cut_from(struct yk__bstr, int32_t);
yk__sds yy__strings_mid(struct yk__bstr, int32_t, int32_t);
bool yy__strings_endswith(struct yk__bstr, struct yk__bstr);
yk__sds yy__os_exe_path();
yk__sds yy__os_cwd();
yy__os_ProcessResult yy__os_run(yk__sds*);
Expand Down Expand Up @@ -1066,7 +1066,7 @@ bool yy__building_is_target_macos(yk__sds yy__building_target_to_check)
yk__sdsfree(yy__building_target_to_check);
return t__12;
}
bool t__13 = yy__strings_contains(yk__sdsdup(yy__building_target_to_check), yk__sdsnewlen("macos", 5));
bool t__13 = yy__strings_contains(yk__bstr_h(yy__building_target_to_check), yk__bstr_s("macos", 5));
yk__sdsfree(yy__building_target_to_check);
return t__13;
}
Expand All @@ -1079,7 +1079,7 @@ bool yy__building_is_target_windows(yk__sds yy__building_target_to_check)
yk__sdsfree(yy__building_target_to_check);
return t__14;
}
bool t__15 = yy__strings_contains(yk__sdsdup(yy__building_target_to_check), yk__sdsnewlen("windows", 7));
bool t__15 = yy__strings_contains(yk__bstr_h(yy__building_target_to_check), yk__bstr_s("windows", 7));
yk__sdsfree(yy__building_target_to_check);
return t__15;
}
Expand Down Expand Up @@ -1241,7 +1241,7 @@ void yy__building_cleanup_buildables(struct yy__building_BObject** yy__building_
}
bool yy__building_keep_ray_objects(yk__sds yy__building_element, bool yy__building_negate)
{
bool yy__building_obj = yy__strings_endswith(yk__sdsdup(yy__building_element), yk__sdsnewlen(".o", 2));
bool yy__building_obj = yy__strings_endswith(yk__bstr_h(yy__building_element), yk__bstr_s(".o", 2));
if (yy__building_negate)
{
bool t__24 = !yy__building_obj;
Expand Down Expand Up @@ -1825,7 +1825,7 @@ int32_t yy__building_build(struct yy__configuration_Config* yy__building_c, yk__
yk__sds t__83 = yy__path_join(yk__bstr_h(yy__building_build_path), yk__bstr_s("program_code.c", 14));
yk__sds yy__building_code_path = yk__sdsdup(t__83);
yk__sds t__84 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code));
if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__84))))
if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__84))))
{
yy__console_red(yk__bstr_s("Failed to write file:", 21));
yk__printlnstr(yy__building_code_path);
Expand Down Expand Up @@ -1924,7 +1924,7 @@ int32_t yy__building_build_simple(struct yy__configuration_Config* yy__building_
yy__building_target = yk__sdsnewlen("web", 3);
}
yk__sds t__96 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code));
if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__96))))
if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__96))))
{
yy__console_red(yk__bstr_s("Failed to write file:", 21));
yk__printlnstr(yy__building_code_path);
Expand Down Expand Up @@ -2466,7 +2466,7 @@ struct yy__configuration_CCode* yy__configuration_inject_c_code_defaults(struct
struct yy__configuration_Config* yy__configuration_load_runtime_features(struct yy__configuration_Config* yy__configuration_c, yk__sds yy__configuration_code, bool yy__configuration_silent)
{
yk__sds yy__configuration_header = yk__sdsnewlen("// YK:" , 6);
bool yy__configuration_has_requirements = yy__strings_startswith(yk__sdsdup(yy__configuration_code), yk__sdsdup(yy__configuration_header));
bool yy__configuration_has_requirements = yy__strings_startswith(yk__bstr_h(yy__configuration_code), yk__bstr_h(yy__configuration_header));
if (!yy__configuration_has_requirements)
{
if (!yy__configuration_silent)
Expand All @@ -2481,7 +2481,7 @@ struct yy__configuration_Config* yy__configuration_load_runtime_features(struct
yk__sdsfree(yy__configuration_code);
return t__62;
}
int32_t yy__configuration_until = yy__strings_find_char(yk__sdsdup(yy__configuration_code), yy__strings_ord(yk__sdsnewlen("#", 1)));
int32_t yy__configuration_until = yy__strings_find_char(yk__bstr_h(yy__configuration_code), yy__strings_ord(yk__bstr_s("#", 1)));
if (yy__configuration_until == INT32_C(-1))
{
yk__arrput(yy__configuration_c->yy__configuration_errors, yk__sdsnewlen("Failed to extract runtime features. Cannot find # in compiled code first line.", 78));
Expand All @@ -2490,7 +2490,7 @@ struct yy__configuration_Config* yy__configuration_load_runtime_features(struct
yk__sdsfree(yy__configuration_code);
return t__63;
}
yk__sds t__64 = yy__strings_mid(yk__sdsdup(yy__configuration_code), yk__sdslen(yy__configuration_header), (yy__configuration_until - yk__sdslen(yy__configuration_header)));
yk__sds t__64 = yy__strings_mid(yk__bstr_h(yy__configuration_code), yk__sdslen(yy__configuration_header), (yy__configuration_until - yk__sdslen(yy__configuration_header)));
yk__sds yy__configuration_temp_features = yk__sdsdup(t__64);
if (!yy__configuration_silent)
{
Expand All @@ -2499,7 +2499,7 @@ struct yy__configuration_Config* yy__configuration_load_runtime_features(struct
yy__console_green(yk__bstr_h(yy__configuration_temp_features));
yk__printstr("\n");
}
yk__sds* yy__configuration_features = yy__strings_split(yk__sdsdup(yy__configuration_temp_features), yk__sdsnewlen(",", 1));
yk__sds* yy__configuration_features = yy__strings_split(yk__bstr_h(yy__configuration_temp_features), yk__bstr_s(",", 1));
int32_t yy__configuration_x = INT32_C(0);
int32_t yy__configuration_length = yk__arrlen(yy__configuration_features);
while (true)
Expand Down Expand Up @@ -2679,7 +2679,7 @@ struct yy__configuration_Config* yy__configuration_load_config()
yk__sds t__80 = yy__os_cwd();
yk__sds t__81 = yy__path_join(yk__bstr_h(t__80), yk__bstr_s("yaksha.toml", 11));
yk__sds yy__configuration_configf = yk__sdsdup(t__81);
yk__sds t__82 = yy__io_readfile(yk__sdsdup(yy__configuration_configf));
yk__sds t__82 = yy__io_readfile(yk__bstr_h(yy__configuration_configf));
yk__sds yy__configuration_config_text = yk__sdsdup(t__82);
yy__toml_Table yy__configuration_conf = yy__toml_from_str(yk__bstr_h(yy__configuration_config_text));
if (!(yy__toml_valid_table(yy__configuration_conf)))
Expand Down Expand Up @@ -3026,8 +3026,8 @@ void yy__console_cyan(struct yk__bstr yy__console_x)
yy__console_color_print(INT32_C(7), yy__console_x);
return;
}
yk__sds yy__io_readfile(yk__sds nn__fname) { return yk__io_readfile(nn__fname); }
bool yy__io_writefile(yk__sds nn__fname, yk__sds nn__data) { return yk__io_writefile(nn__fname, nn__data); }
yk__sds yy__io_readfile(struct yk__bstr nn__fname) { return yk__io_readfile(nn__fname); }
bool yy__io_writefile(struct yk__bstr nn__fname, struct yk__bstr nn__data) { return yk__io_writefile(nn__fname, nn__data); }
yy__toml_Table yy__toml_from_str(struct yk__bstr nn__x)
{
char errbuf[200];
Expand Down Expand Up @@ -3310,39 +3310,31 @@ int32_t yy__strings_get_cstr(yy__c_CStr nn__s, int32_t nn__pos)
{
return (int32_t)nn__s[nn__pos];
}
int32_t yy__strings_get(yk__sds nn__s, int32_t nn__pos)
int32_t yy__strings_get(struct yk__bstr nn__s, int32_t nn__pos)
{
int32_t x = (int32_t)nn__s[nn__pos];
yk__sdsfree(nn__s);
int32_t x = (int32_t)(yk__bstr_get_reference(nn__s))[nn__pos];
return x;
}
bool yy__strings_contains(yk__sds nn__haystack, yk__sds nn__needle)
bool yy__strings_contains(struct yk__bstr nn__haystack, struct yk__bstr nn__needle)
{
bool val = strstr(nn__haystack, nn__needle) != NULL;
yk__sdsfree(nn__haystack);
yk__sdsfree(nn__needle);
bool val = strstr(yk__bstr_get_reference(nn__haystack), yk__bstr_get_reference(nn__needle)) != NULL;
return val;
}
int32_t yy__strings_ord(yk__sds yy__strings_s)
int32_t yy__strings_ord(struct yk__bstr yy__strings_s)
{
int32_t t__0 = yy__strings_get(yk__sdsdup(yy__strings_s), INT32_C(0));
yk__sdsfree(yy__strings_s);
int32_t t__0 = yy__strings_get(yy__strings_s, INT32_C(0));
return t__0;
}
bool yy__strings_startswith(yk__sds nn__a, yk__sds nn__b)
bool yy__strings_startswith(struct yk__bstr nn__a, struct yk__bstr nn__b)
{
bool x = (strstr(nn__a, nn__b) != NULL);
yk__sdsfree(nn__a);
yk__sdsfree(nn__b);
bool x = (strstr(yk__bstr_get_reference(nn__a), yk__bstr_get_reference(nn__b)) != NULL);
return x;
}
yk__sds* yy__strings_split(yk__sds nn__value, yk__sds nn__sep)
yk__sds* yy__strings_split(struct yk__bstr nn__value, struct yk__bstr nn__sep)
{
int count;
yk__sds* result = yk__sdssplitlen(nn__value, yk__sdslen(nn__value),
nn__sep, yk__sdslen(nn__sep), &count);
yk__sdsfree(nn__value);
yk__sdsfree(nn__sep);
yk__sds* result = yk__sdssplitlen(yk__bstr_get_reference(nn__value), yk__bstr_len(nn__value),
yk__bstr_get_reference(nn__sep), yk__bstr_len(nn__sep), &count);
if (NULL == result) {
return NULL;
}
Expand All @@ -3353,17 +3345,17 @@ yk__sds* yy__strings_split(yk__sds nn__value, yk__sds nn__sep)
free(result); // free array itself.
return newarr;
}
int32_t yy__strings_find_char(yk__sds nn__value, int32_t nn__ch)
int32_t yy__strings_find_char(struct yk__bstr nn__value, int32_t nn__ch)
{
int position = -1;
int length = (int)yk__sdslen(nn__value);
int length = (int)yk__bstr_len(nn__value);
const char* reference = yk__bstr_get_reference(nn__value);
for (int i = 0; i < length; i++) {
if (nn__value[i] == nn__ch) {
if (reference[i] == nn__ch) {
position = i;
break;
}
}
yk__sdsfree(nn__value);
return position;
}
yk__sds yy__strings_from_cstr(yy__c_CStr nn__a)
Expand Down Expand Up @@ -3395,43 +3387,35 @@ void yy__strings_del_str(yy__c_CStr nn__a)
if (NULL == nn__a) return;
yk__sdsfree(nn__a);
}
yk__sds yy__strings_cut_from(yk__sds nn__a, int32_t nn__position)
yk__sds yy__strings_cut_from(struct yk__bstr nn__a, int32_t nn__position)
{
if (nn__a == NULL || yk__sdslen(nn__a) < nn__position) {
yk__sdsfree(nn__a);
if (yk__bstr_len(nn__a) < nn__position) {
return yk__sdsempty();
}
yk__sds s = yk__sdsnewlen(nn__a + nn__position, yk__sdslen(nn__a) - nn__position);
yk__sdsfree(nn__a);
yk__sds s = yk__sdsnewlen(yk__bstr_get_reference(nn__a) + nn__position, yk__bstr_len(nn__a) - nn__position);
return s;
}
yk__sds yy__strings_mid(yk__sds nn__a, int32_t nn__position, int32_t nn__number)
yk__sds yy__strings_mid(struct yk__bstr nn__a, int32_t nn__position, int32_t nn__number)
{
if (nn__a == NULL || nn__number < 1 || yk__sdslen(nn__a) < (nn__position + nn__number)) {
yk__sdsfree(nn__a);
if (nn__number < 1 || yk__bstr_len(nn__a) < (nn__position + nn__number)) {
return yk__sdsempty();
}
yk__sds s = yk__sdsnewlen(nn__a + nn__position, nn__number);
yk__sdsfree(nn__a);
yk__sds s = yk__sdsnewlen(yk__bstr_get_reference(nn__a) + nn__position, nn__number);
return s;
}
bool yy__strings_endswith(yk__sds yy__strings_a, yk__sds yy__strings_b)
bool yy__strings_endswith(struct yk__bstr yy__strings_a, struct yk__bstr yy__strings_b)
{
if (yk__sdslen(yy__strings_b) > yk__sdslen(yy__strings_a))
if (yk__bstr_len(yy__strings_b) > yk__bstr_len(yy__strings_a))
{
yk__sdsfree(yy__strings_b);
yk__sdsfree(yy__strings_a);
return false;
}
int32_t yy__strings_pos = (yk__sdslen(yy__strings_a) - yk__sdslen(yy__strings_b));
yk__sds t__1 = yy__strings_cut_from(yk__sdsdup(yy__strings_a), yy__strings_pos);
int32_t yy__strings_pos = (yk__bstr_len(yy__strings_a) - yk__bstr_len(yy__strings_b));
yk__sds t__1 = yy__strings_cut_from(yy__strings_a, yy__strings_pos);
yk__sds yy__strings_cut_a = yk__sdsdup(t__1);
bool yy__strings_result = (yk__sdscmp(yy__strings_cut_a , yy__strings_b) == 0);
bool yy__strings_result = (yk__cmp_sds_bstr(yy__strings_cut_a, yy__strings_b) == 0);
bool t__2 = yy__strings_result;
yk__sdsfree(yy__strings_cut_a);
yk__sdsfree(t__1);
yk__sdsfree(yy__strings_b);
yk__sdsfree(yy__strings_a);
return t__2;
}
yk__sds yy__os_exe_path()
Expand Down Expand Up @@ -3498,7 +3482,7 @@ yk__sds yy__os_which(yk__sds yy__os_binary)
yk__sdsfree(t__2);
yk__sdsfree(t__1);
}
yk__sds* yy__os_paths = yy__strings_split(yk__sdsdup(yy__os_env), yk__sdsdup(yy__os_sep));
yk__sds* yy__os_paths = yy__strings_split(yk__bstr_h(yy__os_env), yk__bstr_h(yy__os_sep));
int32_t yy__os_length = yk__arrlen(yy__os_paths);
while (true)
{
Expand Down Expand Up @@ -3824,7 +3808,7 @@ int32_t yy__build_from_config(struct yy__configuration_Config* yy__config, bool
}
else
{
if (yy__strings_startswith(yk__sdsdup(yy__result->output), yk__sdsnewlen("// YK", 5)))
if (yy__strings_startswith(yk__bstr_h(yy__result->output), yk__bstr_s("// YK", 5)))
{
if (yy__simple)
{
Expand Down
14 changes: 7 additions & 7 deletions compiler/carpntr/main.yaka.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ void yy__console_red(struct yk__bstr);
void yy__console_green(struct yk__bstr);
void yy__console_yellow(struct yk__bstr);
void yy__console_cyan(struct yk__bstr);
yk__sds yy__io_readfile(yk__sds);
bool yy__io_writefile(yk__sds, yk__sds);
yk__sds yy__io_readfile(struct yk__bstr);
bool yy__io_writefile(struct yk__bstr, struct yk__bstr);
yy__toml_Table yy__toml_from_str(struct yk__bstr);
yy__toml_Table yy__toml_get_table(yy__toml_Table, struct yk__bstr);
yy__toml_TomlArray yy__toml_get_array(yy__toml_Table, struct yk__bstr);
Expand Down Expand Up @@ -1815,7 +1815,7 @@ int32_t yy__building_build(struct yy__configuration_Config* yy__building_c, yk__
yk__sds t__83 = yy__path_join(yk__bstr_h(yy__building_build_path), yk__bstr_s("program_code.c", 14));
yk__sds yy__building_code_path = yk__sdsdup(t__83);
yk__sds t__84 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code));
if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__84))))
if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__84))))
{
yy__console_red(yk__bstr_s("Failed to write file:", 21));
yk__printlnstr(yy__building_code_path);
Expand Down Expand Up @@ -1914,7 +1914,7 @@ int32_t yy__building_build_simple(struct yy__configuration_Config* yy__building_
yy__building_target = yk__sdsnewlen("web", 3);
}
yk__sds t__96 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code));
if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__96))))
if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__96))))
{
yy__console_red(yk__bstr_s("Failed to write file:", 21));
yk__printlnstr(yy__building_code_path);
Expand Down Expand Up @@ -2669,7 +2669,7 @@ struct yy__configuration_Config* yy__configuration_load_config()
yk__sds t__80 = yy__os_cwd();
yk__sds t__81 = yy__path_join(yk__bstr_h(t__80), yk__bstr_s("yaksha.toml", 11));
yk__sds yy__configuration_configf = yk__sdsdup(t__81);
yk__sds t__82 = yy__io_readfile(yk__sdsdup(yy__configuration_configf));
yk__sds t__82 = yy__io_readfile(yk__bstr_h(yy__configuration_configf));
yk__sds yy__configuration_config_text = yk__sdsdup(t__82);
yy__toml_Table yy__configuration_conf = yy__toml_from_str(yk__bstr_h(yy__configuration_config_text));
if (!(yy__toml_valid_table(yy__configuration_conf)))
Expand Down Expand Up @@ -3016,8 +3016,8 @@ void yy__console_cyan(struct yk__bstr yy__console_x)
yy__console_color_print(INT32_C(7), yy__console_x);
return;
}
yk__sds yy__io_readfile(yk__sds nn__fname) { return yk__io_readfile(nn__fname); }
bool yy__io_writefile(yk__sds nn__fname, yk__sds nn__data) { return yk__io_writefile(nn__fname, nn__data); }
yk__sds yy__io_readfile(struct yk__bstr nn__fname) { return yk__io_readfile(nn__fname); }
bool yy__io_writefile(struct yk__bstr nn__fname, struct yk__bstr nn__data) { return yk__io_writefile(nn__fname, nn__data); }
yy__toml_Table yy__toml_from_str(struct yk__bstr nn__x)
{
char errbuf[200];
Expand Down
8 changes: 4 additions & 4 deletions compiler/libs/libs/io.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def valid(stream: Ptr[File]) -> bool:
pass

@nativedefine("yk__io_fopen")
def fopen(fname: str, mode: str) -> Ptr[File]:
def fopen(fname: sr, mode: sr) -> Ptr[File]:
# Open a file
pass

Expand Down Expand Up @@ -144,17 +144,17 @@ def munlock(address: AnyPtr, length: c.Size) -> int:
pass

@native("yk__io_readfile")
def readfile(fname: str) -> str:
def readfile(fname: sr) -> str:
# Read a file to as string, on error returns empty
pass

@native("yk__io_writefile")
def writefile(fname: str, data: str) -> bool:
def writefile(fname: sr, data: sr) -> bool:
# Write a file
pass

@nativedefine("yk__io_open")
def open(path: str, mode: int) -> c.CInt:
def open(path: sr, mode: int) -> c.CInt:
pass

@nativedefine("yk__io_close")
Expand Down
Loading

0 comments on commit da30559

Please sign in to comment.