diff --git a/src/builtins/basic.c b/src/builtins/basic.c index 5c46f1b..5e4dad0 100644 --- a/src/builtins/basic.c +++ b/src/builtins/basic.c @@ -51,6 +51,32 @@ static Object *bin_type(Runtime *runtime, Object **argv, unsigned int argc, Erro return (Object*) argv[0]->type; } + +static Object *bin_unicode(Runtime *runtime, Object **argv, unsigned int argc, Error *error) +{ + assert(argc == 1); + (void) runtime; + (void) error; + uint32_t ret = 0; + if(!Object_IsString(argv[0])){ + + Error_Report(error, 0, "Argument #%d is not a string", 1); + return NULL; + } + + const char *string; + int n; + string = Object_ToString(argv[0],&n,Runtime_GetHeap(runtime),error); + if (string == NULL) + return NULL; + + int k = utf8_sequence_to_utf32_codepoint(string,n,&ret); + + assert(k>=0); + + return Object_FromInt(ret,Runtime_GetHeap(runtime),error); +} + static Object *bin_count(Runtime *runtime, Object **argv, unsigned int argc, Error *error) { assert(argc == 1); @@ -231,9 +257,10 @@ const StaticMapSlot bins_basic[] = { { "strcat", SM_FUNCT, .as_funct = bin_strcat, .argc = -1 }, { "type", SM_FUNCT, .as_funct = bin_type, .argc = 1 }, + { "unicode", SM_FUNCT, .as_funct = bin_unicode, .argc = 1 }, { "print", SM_FUNCT, .as_funct = bin_print, .argc = -1 }, { "input", SM_FUNCT, .as_funct = bin_input, .argc = 0 }, { "count", SM_FUNCT, .as_funct = bin_count, .argc = 1 }, { "assert", SM_FUNCT, .as_funct = bin_assert, .argc = -1 }, { NULL, SM_END, {}, {} }, -}; \ No newline at end of file +};