diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-26 00:45:56 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-26 00:45:56 -0400 |
| commit | b5dee4f7ed1b8181df22a9824b7026a125ebbc53 (patch) | |
| tree | 6e4aa12b2c17583a1495f1f1fd2f679766b5829a /src/formatter/types.c | |
| parent | f192834a6405d9a568782b637943588c6af7a8de (diff) | |
Actual type formatting
Diffstat (limited to 'src/formatter/types.c')
| -rw-r--r-- | src/formatter/types.c | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/src/formatter/types.c b/src/formatter/types.c index 34128b0b..24171984 100644 --- a/src/formatter/types.c +++ b/src/formatter/types.c @@ -2,29 +2,44 @@ #include "../ast.h" #include "../stdlib/datatypes.h" -#include "../stdlib/optionals.h" +#include "../stdlib/stdlib.h" #include "../stdlib/text.h" -#include "utils.h" +#include "args.h" +#include "formatter.h" -OptionalText_t format_inline_type(type_ast_t *type, Table_t comments) { - if (range_has_comment(type->start, type->end, comments)) return NONE_TEXT; +Text_t format_type(type_ast_t *type) { switch (type->tag) { - default: { - Text_t code = Text$from_strn(type->start, (int64_t)(type->end - type->start)); - if (Text$has(code, Text("\n"))) return NONE_TEXT; - return Text$replace(code, Text("\t"), single_indent); + 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), "]"); } -} - -Text_t format_type(type_ast_t *type, Table_t comments, Text_t indent) { - (void)comments, (void)indent; - switch (type->tag) { - default: { - OptionalText_t inline_type = format_inline_type(type, comments); - if (inline_type.length >= 0) return inline_type; - Text_t code = Text$from_strn(type->start, (int64_t)(type->end - type->start)); - return Text$replace(code, Text("\t"), single_indent); + case SetTypeAST: { + return Texts("|", format_type(Match(type, ListTypeAST)->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"); } } |
