aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-10-10 23:30:02 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-10-10 23:30:02 -0400
commit9bcef52ccfb99517dab0f28eeac8bd5380a44704 (patch)
treeb4658f89df3bbdf75df26ff1d4fdc7745cbcfb01
parent67702b2d77d8474c2a7fe7f1816f4eb9a0a98af1 (diff)
Convert types.{c,h}
-rw-r--r--compile.c12
-rw-r--r--environment.c2
-rw-r--r--types.c76
-rw-r--r--types.h3
4 files changed, 48 insertions, 45 deletions
diff --git a/compile.c b/compile.c
index d8cbf8b4..22416939 100644
--- a/compile.c
+++ b/compile.c
@@ -19,6 +19,8 @@
#include "structs.h"
#include "typecheck.h"
+#define type_to_cord(t) Text$as_c_string(type_to_text(t))
+
typedef ast_t* (*comprehension_body_t)(ast_t*, ast_t*);
static CORD compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool needs_incref);
@@ -72,7 +74,7 @@ static bool promote(env_t *env, CORD *code, type_t *actual, type_t *needed)
}
if ((actual->tag == IntType || actual->tag == BigIntType) && needed->tag == NumType) {
- *code = CORD_all(type_to_cord(actual), "_to_", type_to_cord(needed), "(", *code, ")");
+ *code = CORD_all(Text$as_c_string(type_to_text(actual)), "_to_", Text$as_c_string(type_to_text(needed)), "(", *code, ")");
return true;
}
@@ -885,11 +887,11 @@ CORD compile_statement(env_t *env, ast_t *ast)
CORD text = CORD_all("func ", Match(fndef->name, Var)->name, "(");
for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) {
- text = CORD_cat(text, type_to_cord(get_arg_ast_type(env, arg)));
+ text = CORD_cat(text, Text$as_c_string(type_to_text(get_arg_ast_type(env, arg))));
if (arg->next) text = CORD_cat(text, ", ");
}
if (ret_t && ret_t->tag != VoidType)
- text = CORD_all(text, ")->", type_to_cord(ret_t));
+ text = CORD_all(text, ")->", Text$as_c_string(type_to_text(ret_t)));
else
text = CORD_all(text, ")");
@@ -1474,8 +1476,8 @@ CORD expr_as_text(env_t *env, CORD expr, type_t *t, CORD color)
case CStringType: return CORD_asprintf("CString$as_text(stack(%r), %r, &CString$info)", expr, color);
case DateTimeType: return CORD_asprintf("DateTime$as_text(stack(%r), %r, &DateTime$info)", expr, color);
case BigIntType: case IntType: case ByteType: case NumType: {
- CORD name = type_to_cord(t);
- return CORD_asprintf("%r$as_text(stack(%r), %r, &%r$info)", name, expr, color, name);
+ Text_t name = type_to_text(t);
+ return CORD_asprintf("%r$as_text(stack(%r), %r, &%r$info)", Text$as_c_string(name), expr, color, name);
}
case TextType: {
return CORD_asprintf("Text$as_text(stack(%r), %r, %r)", expr, color, compile_type_info(env, t));
diff --git a/environment.c b/environment.c
index a6b68b91..2607ab95 100644
--- a/environment.c
+++ b/environment.c
@@ -605,7 +605,7 @@ binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name)
case TableType: return NULL;
case CStringType: case DateTimeType:
case BoolType: case IntType: case BigIntType: case NumType: {
- binding_t *b = get_binding(env, CORD_to_const_char_star(type_to_cord(cls_type)));
+ binding_t *b = get_binding(env, Text$as_c_string(type_to_text(cls_type)));
assert(b);
return get_binding(Match(b->type, TypeInfoType)->env, name);
}
diff --git a/types.c b/types.c
index ec1b66c7..5392c559 100644
--- a/types.c
+++ b/types.c
@@ -1,91 +1,91 @@
// Logic for handling type_t types
-#include <gc/cord.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdint.h>
#include <sys/param.h>
+#include "stdlib/datatypes.h"
#include "stdlib/integers.h"
#include "stdlib/tables.h"
+#include "stdlib/text.h"
#include "stdlib/util.h"
-#include "cordhelpers.h"
#include "types.h"
-CORD type_to_cord(type_t *t) {
+Text_t type_to_text(type_t *t) {
switch (t->tag) {
- case UnknownType: return "???";
- case AbortType: return "Abort";
+ case UnknownType: return Text("???");
+ case AbortType: return Text("Abort");
case ReturnType: {
type_t *ret = Match(t, ReturnType)->ret;
- return CORD_all("Return(", ret ? type_to_cord(ret) : "Void", ")");
- }
- case VoidType: return "Void";
- case MemoryType: return "Memory";
- case BoolType: return "Bool";
- case ByteType: return "Byte";
- case CStringType: return "CString";
- case DateTimeType: return "DateTime";
- case TextType: return Match(t, TextType)->lang ? Match(t, TextType)->lang : "Text";
- case BigIntType: return "Int";
- case IntType: return CORD_asprintf("Int%d", Match(t, IntType)->bits);
- case NumType: return Match(t, NumType)->bits == TYPE_NBITS32 ? "Num32" : "Num";
+ return Texts(Text("Return("), ret ? type_to_text(ret) : Text("Void"), Text(")"));
+ }
+ case VoidType: return Text("Void");
+ case MemoryType: return Text("Memory");
+ case BoolType: return Text("Bool");
+ case ByteType: return Text("Byte");
+ case CStringType: return Text("CString");
+ case DateTimeType: return Text("DateTime");
+ case TextType: return Match(t, TextType)->lang ? Text$from_str(Match(t, TextType)->lang) : Text("Text");
+ case BigIntType: return Text("Int");
+ case IntType: return Text$format("Int%d", Match(t, IntType)->bits);
+ case NumType: return Match(t, NumType)->bits == TYPE_NBITS32 ? Text("Num32") : Text("Num");
case ArrayType: {
auto array = Match(t, ArrayType);
- return CORD_asprintf("[%r]", type_to_cord(array->item_type));
+ return Texts(Text("["), type_to_text(array->item_type), Text("]"));
}
case ChannelType: {
auto array = Match(t, ChannelType);
- return CORD_asprintf("||%r", type_to_cord(array->item_type));
+ return Texts(Text("||"), type_to_text(array->item_type));
}
case TableType: {
auto table = Match(t, TableType);
- return CORD_asprintf("{%r:%r}", type_to_cord(table->key_type), type_to_cord(table->value_type));
+ return Texts(Text("{"), type_to_text(table->key_type), Text(":"), type_to_text(table->value_type), Text("}"));
}
case SetType: {
auto set = Match(t, SetType);
- return CORD_asprintf("{%r}", type_to_cord(set->item_type));
+ return Texts(Text("{"), type_to_text(set->item_type), Text("}"));
}
case ClosureType: {
- return type_to_cord(Match(t, ClosureType)->fn);
+ return type_to_text(Match(t, ClosureType)->fn);
}
case FunctionType: {
- CORD c = "func(";
+ Text_t text = Text("func(");
auto fn = Match(t, FunctionType);
for (arg_t *arg = fn->args; arg; arg = arg->next) {
- c = CORD_cat(c, type_to_cord(arg->type));
- if (arg->next) c = CORD_cat(c, ", ");
+ text = Texts(text, type_to_text(arg->type));
+ if (arg->next) text = Texts(text, Text(", "));
}
if (fn->ret && fn->ret->tag != VoidType)
- c = CORD_all(c, "->", type_to_cord(fn->ret));
- c = CORD_all(c, ")");
- return c;
+ text = Texts(text, Text("->"), type_to_text(fn->ret));
+ text = Texts(text, Text(")"));
+ return text;
}
case StructType: {
auto struct_ = Match(t, StructType);
- return struct_->name;
+ return Text$from_str(struct_->name);
}
case PointerType: {
auto ptr = Match(t, PointerType);
- CORD sigil = ptr->is_stack ? "&" : "@";
- return CORD_all(sigil, type_to_cord(ptr->pointed));
+ Text_t sigil = ptr->is_stack ? Text("&") : Text("@");
+ return Texts(sigil, type_to_text(ptr->pointed));
}
case EnumType: {
auto tagged = Match(t, EnumType);
- return tagged->name;
+ return Text$from_str(tagged->name);
}
case OptionalType: {
- return CORD_all(type_to_cord(Match(t, OptionalType)->type), "?");
+ return Texts(type_to_text(Match(t, OptionalType)->type), Text("?"));
}
case TypeInfoType: {
- return CORD_all("Type$info(", Match(t, TypeInfoType)->name, ")");
+ return Texts(Text("Type$info("), Text$from_str(Match(t, TypeInfoType)->name), Text(")"));
}
case ModuleType: {
- return CORD_all("Module(", Match(t, ModuleType)->name, ")");
+ return Texts(Text("Module("), Text$from_str(Match(t, ModuleType)->name), Text(")"));
}
default: {
raise(SIGABRT);
- return CORD_asprintf("Unknown type: %d", t->tag);
+ return Text$format("Unknown type: %d", t->tag);
}
}
}
@@ -104,14 +104,14 @@ int printf_type(FILE *stream, const struct printf_info *info, const void *const
(void)info;
type_t *t = *(type_t**)args[0];
if (!t) return fputs("(null)", stream);
- return CORD_put(type_to_cord(t), stream);
+ return Text$print(stream, type_to_text(t));
}
bool type_eq(type_t *a, type_t *b)
{
if (a == b) return true;
if (a->tag != b->tag) return false;
- return (CORD_cmp(type_to_cord(a), type_to_cord(b)) == 0);
+ return Text$equal_values(type_to_text(a), type_to_text(b));
}
bool type_is_a(type_t *t, type_t *req)
diff --git a/types.h b/types.h
index 029692ad..a0128285 100644
--- a/types.h
+++ b/types.h
@@ -7,6 +7,7 @@
#include "ast.h"
#include "stdlib/arrays.h"
+#include "stdlib/datatypes.h"
typedef struct type_s type_t;
@@ -135,7 +136,7 @@ struct type_s {
int printf_pointer_size(const struct printf_info *info, size_t n, int argtypes[n], int size[n]);
int printf_type(FILE *stream, const struct printf_info *info, const void *const args[]);
-CORD type_to_cord(type_t *t);
+Text_t type_to_text(type_t *t);
PUREFUNC bool type_eq(type_t *a, type_t *b);
PUREFUNC bool type_is_a(type_t *t, type_t *req);
type_t *type_or_type(type_t *a, type_t *b);