aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-09 18:22:12 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-09 18:22:12 -0500
commit2b83ab279dbfb77cfd699d6da944c51c2353e64a (patch)
tree820f23b5c418e9d501a9fecf4fc84d2e26158cb2 /builtins
parent1b8f7307a9c9cef191f6277cea5f2d11ef0a5788 (diff)
Add langs to the language
Diffstat (limited to 'builtins')
-rw-r--r--builtins/functions.c8
-rw-r--r--builtins/text.c15
-rw-r--r--builtins/text.h6
-rw-r--r--builtins/types.h5
4 files changed, 16 insertions, 18 deletions
diff --git a/builtins/functions.c b/builtins/functions.c
index 9cf05fe0..815e8ff2 100644
--- a/builtins/functions.c
+++ b/builtins/functions.c
@@ -57,6 +57,7 @@ public uint32_t generic_hash(const void *obj, const TypeInfo *type)
{
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer__hash(obj, type);
+ case TextInfo: return Text__hash(obj);
case ArrayInfo: return Array__hash(obj, type);
case TableInfo: return Table_hash(obj, type);
case CustomInfo:
@@ -76,6 +77,7 @@ public int32_t generic_compare(const void *x, const void *y, const TypeInfo *typ
{
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer__compare(x, y, type);
+ case TextInfo: return Text__compare(x, y);
case ArrayInfo: return Array__compare(x, y, type);
case TableInfo: return Table_compare(x, y, type);
case CustomInfo:
@@ -84,10 +86,6 @@ public int32_t generic_compare(const void *x, const void *y, const TypeInfo *typ
return type->CustomInfo.compare(x, y, type);
default:
compare_data:
- {
- int diff = memcmp((void*)x, (void*)y, type->size);
- printf("GOT DIFF: %d\n", diff);
- }
return (int32_t)memcmp((void*)x, (void*)y, type->size);
}
}
@@ -96,6 +94,7 @@ public bool generic_equal(const void *x, const void *y, const TypeInfo *type)
{
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer__equal(x, y, type);
+ case TextInfo: return Text__equal(x, y);
case ArrayInfo: return Array__equal(x, y, type);
case TableInfo: return Table_equal(x, y, type);
case CustomInfo:
@@ -113,6 +112,7 @@ public CORD generic_as_text(const void *obj, bool colorize, const TypeInfo *type
switch (type->tag) {
case PointerInfo: return Pointer__as_text(obj, colorize, type);
case FunctionInfo: return Func__as_text(obj, colorize, type);
+ case TextInfo: return obj ? Text__quoted(*(CORD*)obj, colorize) :type->TextInfo.lang;
case ArrayInfo: return Array__as_text(obj, colorize, type);
case TableInfo: return Table_as_text(obj, colorize, type);
case TypeInfoInfo: return Type__as_text(obj, colorize, type);
diff --git a/builtins/text.c b/builtins/text.c
index 3f926001..f9eb6a27 100644
--- a/builtins/text.c
+++ b/builtins/text.c
@@ -88,7 +88,7 @@ public CORD Text__quoted(CORD str, bool colorize)
}
}
-public int Text__compare(CORD *x, CORD *y)
+public int Text__compare(const CORD *x, const CORD *y)
{
uint8_t *xx = (uint8_t*)CORD_to_const_char_star(*x);
uint8_t *yy = (uint8_t*)CORD_to_const_char_star(*y);
@@ -98,12 +98,12 @@ public int Text__compare(CORD *x, CORD *y)
return result;
}
-public bool Text__equal(CORD *x, CORD *y)
+public bool Text__equal(const CORD *x, const CORD *y)
{
return Text__compare(x, y) == 0;
}
-public uint32_t Text__hash(CORD *cord)
+public uint32_t Text__hash(const CORD *cord)
{
if (!*cord) return 0;
@@ -384,13 +384,8 @@ public array_t Text__character_names(CORD text)
public const TypeInfo Text = {
.size=sizeof(CORD),
.align=__alignof__(CORD),
- .tag=CustomInfo,
- .CustomInfo={
- .as_text=(void*)Text__as_text,
- .compare=(void*)Text__compare,
- .equal=(void*)Text__equal,
- .hash=(void*)Text__hash,
- },
+ .tag=TextInfo,
+ .TextInfo={.lang="Text"},
};
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/builtins/text.h b/builtins/text.h
index f2fc47cd..4c206cbb 100644
--- a/builtins/text.h
+++ b/builtins/text.h
@@ -16,9 +16,9 @@ typedef struct {
CORD Text__as_text(const void *str, bool colorize, const TypeInfo *info);
CORD Text__quoted(CORD str, bool colorize);
-int Text__compare(CORD *x, CORD *y);
-bool Text__equal(CORD *x, CORD *y);
-uint32_t Text__hash(CORD *cord);
+int Text__compare(const CORD *x, const CORD *y);
+bool Text__equal(const CORD *x, const CORD *y);
+uint32_t Text__hash(const CORD *cord);
CORD Text__upper(CORD str);
CORD Text__lower(CORD str);
CORD Text__title(CORD str);
diff --git a/builtins/types.h b/builtins/types.h
index 533ffb86..d26c86be 100644
--- a/builtins/types.h
+++ b/builtins/types.h
@@ -15,7 +15,7 @@ typedef CORD (*str_fn_t)(const void*, bool, const struct TypeInfo*);
typedef struct TypeInfo {
int64_t size, align;
struct { // Anonymous tagged union for convenience
- enum { CustomInfo, PointerInfo, ArrayInfo, TableInfo, FunctionInfo, TypeInfoInfo, OpaqueInfo, } tag;
+ enum { CustomInfo, PointerInfo, TextInfo, ArrayInfo, TableInfo, FunctionInfo, TypeInfoInfo, OpaqueInfo, } tag;
union {
struct {
equal_fn_t equal;
@@ -28,6 +28,9 @@ typedef struct TypeInfo {
const struct TypeInfo *pointed;
} PointerInfo;
struct {
+ const char *lang;
+ } TextInfo;
+ struct {
const struct TypeInfo *item;
} ArrayInfo;
struct {