aboutsummaryrefslogtreecommitdiff
path: root/src/formatter/types.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-09-06 14:46:15 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-09-06 14:46:15 -0400
commitd8a48f64111f542f3afeb5d6e47ff092f9278d9f (patch)
tree07c364503025bb2a26edd7b26f1ba9e8d25340f6 /src/formatter/types.c
parent12345a85d9c7d7a56ddf323247a4bdf347021b73 (diff)
parent73246764f88f6f652316ee0c138a990d836698a7 (diff)
Merge branch 'main' into optional-list-indexing
Diffstat (limited to 'src/formatter/types.c')
-rw-r--r--src/formatter/types.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/formatter/types.c b/src/formatter/types.c
new file mode 100644
index 00000000..e52faf70
--- /dev/null
+++ b/src/formatter/types.c
@@ -0,0 +1,45 @@
+// Logic for formatting types
+
+#include "../ast.h"
+#include "../stdlib/datatypes.h"
+#include "../stdlib/stdlib.h"
+#include "../stdlib/text.h"
+#include "args.h"
+#include "formatter.h"
+
+Text_t format_type(type_ast_t *type) {
+ switch (type->tag) {
+ case VarTypeAST: return Text$from_str(Match(type, VarTypeAST)->name);
+ case PointerTypeAST: {
+ DeclareMatch(ptr, type, PointerTypeAST);
+ return Texts(ptr->is_stack ? Text("&") : Text("@"), format_type(ptr->pointed));
+ }
+ case ListTypeAST: {
+ return Texts("[", format_type(Match(type, ListTypeAST)->item), "]");
+ }
+ case SetTypeAST: {
+ return Texts("|", format_type(Match(type, SetTypeAST)->item), "|");
+ }
+ case TableTypeAST: {
+ DeclareMatch(table, type, TableTypeAST);
+ Text_t code = Texts("{", format_type(table->key), "=", format_type(table->value));
+ if (table->default_value) {
+ OptionalText_t val = format_inline_code(table->default_value, (Table_t){});
+ assert(val.length >= 0);
+ code = Texts(code, "; default=", val);
+ }
+ return Texts(code, "}");
+ }
+ case FunctionTypeAST: {
+ DeclareMatch(func, type, FunctionTypeAST);
+ Text_t code = Texts("func(", format_inline_args(func->args, (Table_t){}));
+ if (func->ret) code = Texts(code, func->args ? Text(" -> ") : Text("-> "), format_type(func->ret));
+ return Texts(code, ")");
+ }
+ case OptionalTypeAST: {
+ return Texts(format_type(Match(type, OptionalTypeAST)->type), "?");
+ }
+ case UnknownTypeAST:
+ default: fail("Invalid Type AST");
+ }
+}