diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-26 14:39:11 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-26 14:39:11 -0400 |
| commit | d25d5642392f93623d1eb4d11156d293fe6df546 (patch) | |
| tree | b14e5422d459a62ec086e06d23a81f93c478bd60 /src/formatter | |
| parent | 4715487a4be9f03ad58cb3199e95573e262f6387 (diff) | |
Formatting for min/max and cleanup for ints/nums
Diffstat (limited to 'src/formatter')
| -rw-r--r-- | src/formatter/formatter.c | 37 | ||||
| -rw-r--r-- | src/formatter/utils.h | 1 |
2 files changed, 32 insertions, 6 deletions
diff --git a/src/formatter/formatter.c b/src/formatter/formatter.c index 96bdf632..a48a1f33 100644 --- a/src/formatter/formatter.c +++ b/src/formatter/formatter.c @@ -216,6 +216,7 @@ OptionalText_t format_inline_code(ast_t *ast, Table_t comments) { return Texts(code, quote); } /*inline*/ case TextLiteral: { fail("Something went wrong, we shouldn't be formatting text literals directly"); } + /*inline*/ case Path: { return Texts("(", Text$from_str(Match(ast, Path)->path), ")"); } /*inline*/ case Stop: { const char *target = Match(ast, Stop)->target; return target ? Texts("stop ", Text$from_str(target)) : Text("stop"); @@ -224,15 +225,27 @@ OptionalText_t format_inline_code(ast_t *ast, Table_t comments) { const char *target = Match(ast, Skip)->target; return target ? Texts("skip ", Text$from_str(target)) : Text("skip"); } + /*inline*/ case Min: + /*inline*/ case Max: { + Text_t lhs = fmt_inline(ast->tag == Min ? Match(ast, Min)->lhs : Match(ast, Max)->lhs, comments); + Text_t rhs = fmt_inline(ast->tag == Min ? Match(ast, Min)->rhs : Match(ast, Max)->rhs, comments); + ast_t *key = ast->tag == Min ? Match(ast, Min)->key : Match(ast, Max)->key; + return Texts(lhs, key ? fmt_inline(key, comments) : (ast->tag == Min ? Text(" _min_ ") : Text(" _max_ ")), rhs); + } /*inline*/ case None: + return Text("none"); /*inline*/ case Bool: - /*inline*/ case Int: - /*inline*/ case Num: - /*inline*/ case Var: { - Text_t code = Text$from_strn(ast->start, (int64_t)(ast->end - ast->start)); - if (Text$has(code, Text("\n"))) return NONE_TEXT; - return code; + return Match(ast, Bool)->b ? Text("yes") : Text("no"); + /*inline*/ case Int: { + OptionalText_t source = ast_source(ast); + return source.length > 0 ? source : Text$from_str(Match(ast, Int)->str); + } + /*inline*/ case Num: { + OptionalText_t source = ast_source(ast); + return source.length > 0 ? source : Text$from_str(String(Match(ast, Num)->n)); } + /*inline*/ case Var: + return Text$from_str(Match(ast, Var)->name); /*inline*/ case FunctionCall: { DeclareMatch(call, ast, FunctionCall); return Texts(fmt_inline(call->fn, comments), "(", must(format_inline_args(call->args, comments)), ")"); @@ -566,6 +579,18 @@ Text_t format_code(ast_t *ast, Table_t comments, Text_t indent) { return code; } /*multiline*/ case TextLiteral: { fail("Something went wrong, we shouldn't be formatting text literals directly"); } + /*multiline*/ case Path: { + assert(inlined.length > 0); + return inlined; + } + /*inline*/ case Min: + /*inline*/ case Max: { + Text_t lhs = termify(ast->tag == Min ? Match(ast, Min)->lhs : Match(ast, Max)->lhs, comments, indent); + Text_t rhs = termify(ast->tag == Min ? Match(ast, Min)->rhs : Match(ast, Max)->rhs, comments, indent); + ast_t *key = ast->tag == Min ? Match(ast, Min)->key : Match(ast, Max)->key; + Text_t op = key ? fmt(key, comments, indent) : (ast->tag == Min ? Text("_min_") : Text("_max_")); + return Texts(lhs, " ", op, " ", rhs); + } /*multiline*/ case Stop: /*multiline*/ case Skip: /*multiline*/ case None: diff --git a/src/formatter/utils.h b/src/formatter/utils.h index df9c9b92..9dc230e4 100644 --- a/src/formatter/utils.h +++ b/src/formatter/utils.h @@ -6,6 +6,7 @@ #include "../ast.h" #include "../stdlib/datatypes.h" +#include "../stdlib/optionals.h" #define MAX_WIDTH 100 |
