diff --git a/docs/05_Built-Ins.md b/docs/05_Built-Ins.md index 49ba0a6..b06ce62 100644 --- a/docs/05_Built-Ins.md +++ b/docs/05_Built-Ins.md @@ -8,18 +8,20 @@ None print import -unicode type -unicode -chr count input assert error + string.cat +string.ord +string.chr + buffer.new buffer.sliceUp buffer.toString + math.PI math.E math.floor @@ -36,6 +38,7 @@ math.log math.log10 math.pow math.sqrt + files.READ files.WRITE files.APPEND diff --git a/src/lib/builtins/basic.c b/src/lib/builtins/basic.c index dbdb831..b48baa4 100644 --- a/src/lib/builtins/basic.c +++ b/src/lib/builtins/basic.c @@ -37,7 +37,6 @@ #include "files.h" #include "string.h" #include "buffer.h" -#include "../utils/utf8.h" #include "../utils/defs.h" #include "../objects/objects.h" #include "../runtime/runtime.h" @@ -187,83 +186,6 @@ static int bin_type(Runtime *runtime, Object **argv, unsigned int argc, Object * return 1; } -static int bin_unicode(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error) -{ - UNUSED(runtime); - UNUSED(error); - UNUSED(argc); - ASSERT(argc == 1); - - uint32_t ret = 0; - - if(!Object_IsString(argv[0])) - { - Error_Report(error, 0, "Argument #%d is not a string", 1); - return -1; - } - - const char *string; - int n; - string = Object_ToString(argv[0],&n,Runtime_GetHeap(runtime),error); - if (string == NULL) - return -1; - - if(n == 0) - { - Error_Report(error, 0, "Argument #%d is an empty string", 1); - return -1; - } - - - int k = utf8_sequence_to_utf32_codepoint(string,n,&ret); - UNUSED(k); - ASSERT(k >= 0); - - Object *temp = Object_FromInt(ret,Runtime_GetHeap(runtime),error); - - if(temp == NULL) - return -1; - - rets[0] = temp; - return 1; -} - -static int bin_chr(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error) -{ - UNUSED(argc); - ASSERT(argc == 1); - - if(!Object_IsInt(argv[0])) - { - Error_Report(error, 0, "Argument #%d is not an integer", 1); - return -1; - } - - char buff[32]; - - int value = Object_ToInt(argv[0],error); - - if(error->occurred) - return -1; - - - int k = utf8_sequence_from_utf32_codepoint(buff,sizeof(buff),value); - - if(k<0) - { - Error_Report(error, 0, "Argument #%d is not valid utf-32", 1); - return -1; - } - - Object *temp = Object_FromString(buff,k,Runtime_GetHeap(runtime),error); - - if(temp == NULL) - return -1; - - rets[0] = temp; - return 1; -} - static int bin_count(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error) { UNUSED(argc); @@ -399,8 +321,6 @@ StaticMapSlot bins_basic[] = { { "import", SM_FUNCT, .as_funct = bin_import, .argc = 1, }, { "type", SM_FUNCT, .as_funct = bin_type, .argc = 1 }, - { "unicode", SM_FUNCT, .as_funct = bin_unicode, .argc = 1 }, - { "chr", SM_FUNCT, .as_funct = bin_chr, .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 }, diff --git a/src/lib/builtins/string.c b/src/lib/builtins/string.c index d47f63e..693c6a6 100644 --- a/src/lib/builtins/string.c +++ b/src/lib/builtins/string.c @@ -1,9 +1,88 @@ #include #include +#include #include "string.h" #include "../utils/defs.h" +#include "../utils/utf8.h" #include "../runtime/runtime.h" +static int bin_ord(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error) +{ + UNUSED(runtime); + UNUSED(error); + UNUSED(argc); + ASSERT(argc == 1); + + uint32_t ret = 0; + + if(!Object_IsString(argv[0])) + { + Error_Report(error, 0, "Argument #%d is not a string", 1); + return -1; + } + + const char *string; + int n; + string = Object_ToString(argv[0],&n,Runtime_GetHeap(runtime),error); + if (string == NULL) + return -1; + + if(n == 0) + { + Error_Report(error, 0, "Argument #%d is an empty string", 1); + return -1; + } + + + int k = utf8_sequence_to_utf32_codepoint(string,n,&ret); + UNUSED(k); + ASSERT(k >= 0); + + Object *temp = Object_FromInt(ret,Runtime_GetHeap(runtime),error); + + if(temp == NULL) + return -1; + + rets[0] = temp; + return 1; +} + +static int bin_chr(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error) +{ + UNUSED(argc); + ASSERT(argc == 1); + + if(!Object_IsInt(argv[0])) + { + Error_Report(error, 0, "Argument #%d is not an integer", 1); + return -1; + } + + char buff[32]; + + int value = Object_ToInt(argv[0],error); + + if(error->occurred) + return -1; + + + int k = utf8_sequence_from_utf32_codepoint(buff,sizeof(buff),value); + + if(k<0) + { + Error_Report(error, 0, "Argument #%d is not valid utf-32", 1); + return -1; + } + + Object *temp = Object_FromString(buff,k,Runtime_GetHeap(runtime),error); + + if(temp == NULL) + return -1; + + rets[0] = temp; + return 1; +} + static int bin_cat(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Error *error) { unsigned int total_count = 0; @@ -67,5 +146,7 @@ done: } StaticMapSlot bins_string[] = { + { "ord", SM_FUNCT, .as_funct = bin_ord, .argc = 1 }, + { "chr", SM_FUNCT, .as_funct = bin_chr, .argc = 1 }, { "cat", SM_FUNCT, .as_funct = bin_cat, .argc = -1 }, };