aboutsummaryrefslogtreecommitdiff
path: root/src/formatter
diff options
context:
space:
mode:
Diffstat (limited to 'src/formatter')
-rw-r--r--src/formatter/args.c4
-rw-r--r--src/formatter/formatter.c7
-rw-r--r--src/formatter/types.c51
-rw-r--r--src/formatter/types.h3
4 files changed, 39 insertions, 26 deletions
diff --git a/src/formatter/args.c b/src/formatter/args.c
index 2bd115cd..6bd07b28 100644
--- a/src/formatter/args.c
+++ b/src/formatter/args.c
@@ -12,7 +12,7 @@ OptionalText_t format_inline_arg(arg_ast_t *arg, Table_t comments) {
if (range_has_comment(arg->start, arg->end, comments)) return NONE_TEXT;
if (arg->name == NULL && arg->value) return must(format_inline_code(arg->value, comments));
Text_t code = Text$from_str(arg->name);
- if (arg->type) code = Texts(code, ":", must(format_inline_type(arg->type, comments)));
+ if (arg->type) code = Texts(code, ":", must(format_type(arg->type)));
if (arg->value) code = Texts(code, " = ", must(format_inline_code(arg->value, comments)));
return code;
}
@@ -22,7 +22,7 @@ Text_t format_arg(arg_ast_t *arg, Table_t comments, Text_t indent) {
if (inline_arg.length >= 0 && inline_arg.length <= MAX_WIDTH) return inline_arg;
if (arg->name == NULL && arg->value) return format_code(arg->value, comments, indent);
Text_t code = Text$from_str(arg->name);
- if (arg->type) code = Texts(code, ":", format_type(arg->type, comments, indent));
+ if (arg->type) code = Texts(code, ":", format_type(arg->type));
if (arg->value) code = Texts(code, " = ", format_code(arg->value, comments, indent));
return code;
}
diff --git a/src/formatter/formatter.c b/src/formatter/formatter.c
index b0fad38a..f0a1c125 100644
--- a/src/formatter/formatter.c
+++ b/src/formatter/formatter.c
@@ -119,7 +119,7 @@ OptionalText_t format_inline_code(ast_t *ast, Table_t comments) {
/*inline*/ case Declare: {
DeclareMatch(decl, ast, Declare);
Text_t code = fmt_inline(decl->var, comments);
- if (decl->type) code = Texts(code, " : ", must(format_inline_type(decl->type, comments)));
+ if (decl->type) code = Texts(code, " : ", format_type(decl->type));
if (decl->value) code = Texts(code, decl->type ? Text(" = ") : Text(" := "), fmt_inline(decl->value, comments));
return code;
}
@@ -338,8 +338,7 @@ Text_t format_code(ast_t *ast, Table_t comments, Text_t indent) {
// ast_t *cache;
// bool is_inline;
Text_t code = Texts("func ", fmt(func->name, comments, indent), "(", format_args(func->args, comments, indent));
- if (func->ret_type)
- code = Texts(code, func->args ? Text(" -> ") : Text("-> "), format_type(func->ret_type, comments, indent));
+ if (func->ret_type) code = Texts(code, func->args ? Text(" -> ") : Text("-> "), format_type(func->ret_type));
if (func->cache) code = Texts(code, "; cache=", fmt(func->cache, comments, indent));
if (func->is_inline) code = Texts(code, "; inline");
code = Texts(code, ")\n", indent, single_indent, fmt(func->body, comments, Texts(indent, single_indent)));
@@ -419,7 +418,7 @@ Text_t format_code(ast_t *ast, Table_t comments, Text_t indent) {
/*multiline*/ case Declare: {
DeclareMatch(decl, ast, Declare);
Text_t code = fmt(decl->var, comments, indent);
- if (decl->type) code = Texts(code, " : ", format_type(decl->type, comments, indent));
+ if (decl->type) code = Texts(code, " : ", format_type(decl->type));
if (decl->value)
code = Texts(code, decl->type ? Text(" = ") : Text(" := "), fmt(decl->value, comments, indent));
return code;
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");
}
}
diff --git a/src/formatter/types.h b/src/formatter/types.h
index 1f0698b3..2571f880 100644
--- a/src/formatter/types.h
+++ b/src/formatter/types.h
@@ -5,5 +5,4 @@
#include "../ast.h"
#include "../stdlib/datatypes.h"
-OptionalText_t format_inline_type(type_ast_t *type, Table_t comments);
-Text_t format_type(type_ast_t *type, Table_t comments, Text_t indent);
+Text_t format_type(type_ast_t *type);