From 91b6746fe1315aa9c09936b69cea3892c04c11af Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 25 Aug 2025 23:53:03 -0400 Subject: Split out formatter into subfiles --- src/formatter/types.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/formatter/types.c (limited to 'src/formatter/types.c') diff --git a/src/formatter/types.c b/src/formatter/types.c new file mode 100644 index 00000000..282a299f --- /dev/null +++ b/src/formatter/types.c @@ -0,0 +1,30 @@ +// Logic for formatting types + +#include "../ast.h" +#include "../stdlib/datatypes.h" +#include "../stdlib/optionals.h" +#include "../stdlib/text.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; + 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); + } + } +} + +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); + } + } +} -- cgit v1.2.3 From 978835e3dd8dd59a1efaa869f2f603eb9eea5a3f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 25 Aug 2025 23:59:09 -0400 Subject: Split out utility functions --- src/formatter/types.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/formatter/types.c') diff --git a/src/formatter/types.c b/src/formatter/types.c index 282a299f..34128b0b 100644 --- a/src/formatter/types.c +++ b/src/formatter/types.c @@ -4,7 +4,7 @@ #include "../stdlib/datatypes.h" #include "../stdlib/optionals.h" #include "../stdlib/text.h" -#include "formatter.h" +#include "utils.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; -- cgit v1.2.3 From b5dee4f7ed1b8181df22a9824b7026a125ebbc53 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 26 Aug 2025 00:45:56 -0400 Subject: Actual type formatting --- src/formatter/types.c | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'src/formatter/types.c') 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"); } } -- cgit v1.2.3 From 794cffd4da6694d06d1a7c6f77c157e58354ac25 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 26 Aug 2025 00:46:31 -0400 Subject: Fix typo --- src/formatter/types.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/formatter/types.c') diff --git a/src/formatter/types.c b/src/formatter/types.c index 24171984..e52faf70 100644 --- a/src/formatter/types.c +++ b/src/formatter/types.c @@ -18,7 +18,7 @@ Text_t format_type(type_ast_t *type) { return Texts("[", format_type(Match(type, ListTypeAST)->item), "]"); } case SetTypeAST: { - return Texts("|", format_type(Match(type, ListTypeAST)->item), "|"); + return Texts("|", format_type(Match(type, SetTypeAST)->item), "|"); } case TableTypeAST: { DeclareMatch(table, type, TableTypeAST); -- cgit v1.2.3