Skip to content

Commit

Permalink
feat(libs): use sr in libs (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Feb 27, 2024
1 parent 08b86c9 commit 5f7bcb7
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 318 deletions.
248 changes: 117 additions & 131 deletions compiler/carpntr/build/program_code.c

Large diffs are not rendered by default.

245 changes: 116 additions & 129 deletions compiler/carpntr/main.yaka.c

Large diffs are not rendered by default.

23 changes: 7 additions & 16 deletions compiler/libs/libs/fileformats/ini.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,26 @@ def del_ini(object: Ini) -> None:
ccode """ini_destroy(nn__object)"""

@native
def from_str(ini_data: str) -> Ini:
def from_str(ini_data: sr) -> Ini:
# Parse INI file from given string data
ccode """ini_t* ini = ini_load(nn__ini_data);
yk__sdsfree(nn__ini_data);
ccode """ini_t* ini = ini_load(yk__bstr_get_reference(nn__ini_data));
return ini"""

@native
def get(object: Ini, section: str, property: str) -> str:
def get(object: Ini, section: sr, property: sr) -> str:
# Get a property in given section
# Empty string is returned if we cannot find the section
ccode """
int section = ini_find_section(nn__object, nn__section, yk__sdslen(nn__section));
ccode """
int section = ini_find_section(nn__object, yk__bstr_get_reference(nn__section), yk__bstr_len(nn__section));
if (section == INI_NOT_FOUND) {
yk__sdsfree(nn__section);
yk__sdsfree(nn__property);
return yk__sdsempty();
}
int prop ini_find_property(nn__object, section, nn__property, yk__sdslen(nn__property));
int prop ini_find_property(nn__object, yk__bstr_get_reference(section), yk__bstr_get_reference(nn__property), yk__bstr_len(nn__property));
if (prop == INI_NOT_FOUND) {
yk__sdsfree(nn__section);
yk__sdsfree(nn__property);
return yk__sdsempty();
}
char const* data = ini_property_value(nn__object), section, prop);
char const* data = ini_property_value(nn__object), yk__bstr_get_reference(section), prop);
if (data == NULL) {
yk__sdsfree(nn__section);
yk__sdsfree(nn__property);
return yk__sdsempty();
}
yk__sdsfree(nn__section);
yk__sdsfree(nn__property);
return yk__sdsnew(data)"""
55 changes: 22 additions & 33 deletions compiler/libs/libs/fileformats/toml.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,22 @@ def valid_array(x: TomlArray) -> bool:
ccode """(NULL != nn__x)"""

@native
def from_str(x: str) -> Table:
def from_str(x: sr) -> Table:
# Parse to a TOML Table from given string
ccode """char errbuf[200];
toml_table_t* t = toml_parse(nn__x, errbuf, sizeof(errbuf));
yk__sdsfree(nn__x);
toml_table_t* t = toml_parse(yk__bstr_get_reference(nn__x), errbuf, sizeof(errbuf));
return t"""

@native
def get_table(x: Table, name: str) -> Table:
def get_table(x: Table, name: sr) -> Table:
# Get a Table from given name
ccode """toml_table_t* t = toml_table_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_table_t* t = toml_table_in(nn__x, yk__bstr_get_reference(nn__name));
return t"""

@native
def get_array(x: Table, name: str) -> TomlArray:
def get_array(x: Table, name: sr) -> TomlArray:
# Get any array from table object
ccode """toml_array_t* t = toml_array_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_array_t* t = toml_array_in(nn__x, yk__bstr_get_reference(nn__name));
return t"""

@native
Expand All @@ -50,10 +47,9 @@ def array_len(x: TomlArray) -> int:
return toml_array_nelem(nn__x)"""

@native
def get_string(x: Table, name: str) -> str:
def get_string(x: Table, name: sr) -> str:
# Get a string from table or empty if fails
ccode """toml_datum_t v = toml_string_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_datum_t v = toml_string_in(nn__x, yk__bstr_get_reference(nn__name));
if (v.ok) {
yk__sds s = yk__sdsnew(v.u.s);
free(v.u.s);
Expand All @@ -62,53 +58,47 @@ def get_string(x: Table, name: str) -> str:
return yk__sdsempty()"""

@native
def get_string_default(x: Table, name: str, default: str) -> str:
def get_string_default(x: Table, name: sr, default: sr) -> str:
# Get a string from table or given default
ccode """toml_datum_t v = toml_string_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_datum_t v = toml_string_in(nn__x, yk__bstr_get_reference(nn__name));
if (v.ok) {
yk__sds s = yk__sdsnew(v.u.s);
free(v.u.s);
yk__sdsfree(nn__default);
return s;
}
return nn__default"""
return yk__bstr_copy_to_sds(nn__default)"""

@native
def get_bool(x: Table, name: str) -> bool:
def get_bool(x: Table, name: sr) -> bool:
# Get a bool from table or false if fails
ccode """toml_datum_t v = toml_bool_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_datum_t v = toml_bool_in(nn__x, yk__bstr_get_reference(nn__name));
if (v.ok) {
return (1 == v.u.b);
}
return false"""

@native
def get_bool_default(x: Table, name: str, default: bool) -> bool:
def get_bool_default(x: Table, name: sr, default: bool) -> bool:
# Get a bool from table or given default
ccode """toml_datum_t v = toml_bool_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_datum_t v = toml_bool_in(nn__x, yk__bstr_get_reference(nn__name));
if (v.ok) {
return (1 == v.u.b);
}
return nn__default"""

@native
def get_int(x: Table, name: str) -> int:
def get_int(x: Table, name: sr) -> int:
# Get an int from table or 0 if fails
ccode """toml_datum_t v = toml_int_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_datum_t v = toml_int_in(nn__x, yk__bstr_get_reference(nn__name));
if (v.ok) {
return v.u.i;
}
return 0"""

@native
def get_int_default(x: Table, name: str, default: int) -> int:
def get_int_default(x: Table, name: sr, default: int) -> int:
# Get an int from table or given default
ccode """toml_datum_t v = toml_int_in(nn__x, nn__name);
yk__sdsfree(nn__name);
ccode """toml_datum_t v = toml_int_in(nn__x, yk__bstr_get_reference(nn__name));
if (v.ok) {
return v.u.i;
}
Expand All @@ -129,16 +119,15 @@ def string_at(x: TomlArray, pos: int) -> str:
return yk__sdsempty()"""

@native
def string_at_default(x: TomlArray, pos: int, default: str) -> str:
def string_at_default(x: TomlArray, pos: int, default: sr) -> str:
# Get string from array at location or given default
ccode """toml_datum_t v = toml_string_at(nn__x, nn__pos);
if (v.ok) {
yk__sds s = yk__sdsnew(v.u.s);
free(v.u.s);
yk__sdsfree(nn__default);
return s;
}
return nn__default"""
return yk__bstr_copy_to_sds(nn__default)"""

@native
def bool_at(x: TomlArray, pos: int) -> bool:
Expand Down Expand Up @@ -178,7 +167,7 @@ def int_at_default(x: TomlArray, pos: int, default: int) -> int:

############ Convenient methods

def get_string_array(x: Table, name: str) -> Array[str]:
def get_string_array(x: Table, name: sr) -> Array[str]:
# Get an array as native string array
str_array: Array[str]
arr: TomlArray = get_array(x, name)
Expand Down
8 changes: 4 additions & 4 deletions compiler/libs/libs/os/path.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def executable(p: str) -> bool:
# Is an executable?
pass

def end_with_slash(a: str) -> bool:
def end_with_slash(a: sr) -> bool:
# Does the given string end with slash?
length: int = len(a)
if length < 1:
Expand All @@ -47,7 +47,7 @@ def end_with_slash(a: str) -> bool:
x: bool = chr == 47 or chr == 92
return x

def end_with_dot(a: str) -> bool:
def end_with_dot(a: sr) -> bool:
# Does the given string end with slash?
length: int = len(a)
if length < 1:
Expand All @@ -56,7 +56,7 @@ def end_with_dot(a: str) -> bool:
x: bool = chr == 46
return x

def join(a: str, b: str) -> str:
def join(a: sr, b: sr) -> str:
# Do a path join
if end_with_slash(a):
return a + b
Expand Down Expand Up @@ -99,7 +99,7 @@ def dirname(p: str) -> str:
length: int = len(p)
if length <= 2:
return p
# Ignore last character
# Ignore last character
if end_with_slash(p):
length = length - 1
x: c.CStr = strings.to_cstr(p)
Expand Down
11 changes: 6 additions & 5 deletions compiler/libs/libs/strings/utf8.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ class Utf8IterateState:
codepoint: int

@native
def new_iter(s: str) -> Utf8IterateState:
def new_iter(s: sr) -> Utf8IterateState:
# Create a new iterator from given string
ccode """struct utf8proc_iter_state* x = calloc(1, sizeof(struct utf8proc_iter_state));
if (x == NULL) return x;
x->str_original = (utf8proc_uint8_t *) nn__s;
x->str_position = (utf8proc_uint8_t *) nn__s;
x->length = yk__sdslen(nn__s);
const char* s = yk__bstr_get_reference(nn__s);
x->str_original = (utf8proc_uint8_t *) s;
x->str_position = (utf8proc_uint8_t *) s;
x->length = yk__bstr_len(nn__s);
x->step_size = 0;
x->codepoint = -1;
return x"""

@native
def del_iter(s: Utf8IterateState) -> None:
# Delete the iterator object after done
ccode """yk__sdsfree((yk__sds)nn__s->str_original);
ccode """if (nn__s == NULL) return;
free(nn__s)"""

@native
Expand Down

0 comments on commit 5f7bcb7

Please sign in to comment.