renamed unicode builtin to ord

This commit is contained in:
Francesco Cozzuto
2022-08-17 21:25:49 +02:00
parent 95392846d6
commit 7e0144faa3
3 changed files with 87 additions and 83 deletions
-80
View File
@@ -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 },
+81
View File
@@ -1,9 +1,88 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#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 },
};