Skip to content

Commit

Permalink
aspl: add the string.replaceMany method
Browse files Browse the repository at this point in the history
  • Loading branch information
Wertzui123 committed Jul 31, 2024
1 parent 4e58728 commit 822e592
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions runtime/ailinterpreter/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ void aspl_ailinterpreter_initialize_builtin_methods(ASPL_AILI_EnvironmentContext
string_replace->parameters.parameters[1] = (ASPL_AILI_Parameter){ .name = "replace", .expected_types = (ASPL_AILI_TypeList) {.types = (char* []) { "string" }, .size = 1 }, .optional = 0 };
hashmap_str_to_voidptr_hashmap_set(string_methods, "replace", string_replace);

ASPL_AILI_BuiltinMethod* string_replaceMany = ASPL_MALLOC(sizeof(ASPL_AILI_BuiltinMethod));
string_replaceMany->parameters = (ASPL_AILI_ParameterList){ .parameters = ASPL_MALLOC(sizeof(ASPL_AILI_Parameter) * 2), .size = 2 };
string_replaceMany->parameters.parameters[0] = (ASPL_AILI_Parameter){ .name = "dictionary", .expected_types = (ASPL_AILI_TypeList) {.types = (char* []) { "map<string, string>" }, .size = 1 }, .optional = 0 };
hashmap_str_to_voidptr_hashmap_set(string_methods, "replaceMany", string_replaceMany);

ASPL_AILI_BuiltinMethod* string_startsWith = ASPL_MALLOC(sizeof(ASPL_AILI_BuiltinMethod));
string_startsWith->parameters = (ASPL_AILI_ParameterList){ .parameters = ASPL_MALLOC(sizeof(ASPL_AILI_Parameter) * 1), .size = 1 };
string_startsWith->parameters.parameters[0] = (ASPL_AILI_Parameter){ .name = "substring", .expected_types = (ASPL_AILI_TypeList) {.types = (char* []) { "string" }, .size = 1 }, .optional = 0 };
Expand Down
19 changes: 19 additions & 0 deletions stdlib/aspl/compiler/backend/stringcode/c/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -3640,6 +3640,24 @@ ASPL_OBJECT_TYPE aspl_method_string_replace_wrapper(ASPL_OBJECT_TYPE* obj, ASPL_
return aspl_method_string_replace(obj, arguments[0], arguments[1]);
}

ASPL_OBJECT_TYPE aspl_method_string_replaceMany(ASPL_OBJECT_TYPE* obj, ASPL_OBJECT_TYPE* dictionary)
{
// TODO: Use a more efficient implementation
ASPL_OBJECT_TYPE s = *obj;
for (int i = 0; i < ASPL_ACCESS(*dictionary).value.map->hashmap->len; i++)
{
ASPL_OBJECT_TYPE key = ASPL_HASHMAP_UNWRAP(ASPL_ACCESS(*dictionary).value.map->hashmap->pairs[i]->key);
ASPL_OBJECT_TYPE value = ASPL_HASHMAP_UNWRAP(ASPL_ACCESS(*dictionary).value.map->hashmap->pairs[i]->value);
s = aspl_method_string_replace(C_REFERENCE(s), C_REFERENCE(key), C_REFERENCE(value));
}
return s;
}

ASPL_OBJECT_TYPE aspl_method_string_replaceMany_wrapper(ASPL_OBJECT_TYPE* obj, ASPL_OBJECT_TYPE* arguments[])
{
return aspl_method_string_replaceMany(obj, arguments[0]);
}

ASPL_OBJECT_TYPE aspl_method_string_startsWith(ASPL_OBJECT_TYPE* obj, ASPL_OBJECT_TYPE* prefix)
{
ASPL_OBJECT_TYPE objA = (ASPL_OBJECT_TYPE)*obj;
Expand Down Expand Up @@ -4223,6 +4241,7 @@ void aspl_setup_builtin_method_pointers()
aspl_object_method_init("string", "toLower", aspl_method_string_toLower_wrapper);
aspl_object_method_init("string", "toUpper", aspl_method_string_toUpper_wrapper);
aspl_object_method_init("string", "replace", aspl_method_string_replace_wrapper);
aspl_object_method_init("string", "replaceMany", aspl_method_string_replaceMany_wrapper);
aspl_object_method_init("string", "startsWith", aspl_method_string_startsWith_wrapper);
aspl_object_method_init("string", "endsWith", aspl_method_string_endsWith_wrapper);
aspl_object_method_init("string", "contains", aspl_method_string_contains_wrapper);
Expand Down
1 change: 1 addition & 0 deletions stdlib/aspl/parser/methods/Method.aspl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Method {
new InternalMethod(Type:fromString("string"), "toLower", [], new Types([Type:fromString("string")]), []).register(null)
new InternalMethod(Type:fromString("string"), "toUpper", [], new Types([Type:fromString("string")]), []).register(null)
new InternalMethod(Type:fromString("string"), "replace", [new Parameter("search", new Types([Type:fromString("string")])), new Parameter("replace", new Types([Type:fromString("string")]))], new Types([Type:fromString("string")]), []).register(null)
new InternalMethod(Type:fromString("string"), "replaceMany", [new Parameter("dictionary", new Types([Type:fromString("map<string, string>")]))], new Types([Type:fromString("string")]), []).register(null)
new InternalMethod(Type:fromString("string"), "startsWith", [new Parameter("substring", new Types([Type:fromString("string")]))], new Types([Type:fromString("boolean")]), []).register(null)
new InternalMethod(Type:fromString("string"), "endsWith", [new Parameter("substring", new Types([Type:fromString("string")]))], new Types([Type:fromString("boolean")]), []).register(null)
new InternalMethod(Type:fromString("string"), "contains", [new Parameter("substring", new Types([Type:fromString("string")]))], new Types([Type:fromString("boolean")]), []).register(null)
Expand Down

0 comments on commit 822e592

Please sign in to comment.