From dc6cc2fab6966f21bce65e916936e61a858e0067 Mon Sep 17 00:00:00 2001 From: Max Bernstein Date: Wed, 5 Jun 2024 20:16:00 -0400 Subject: [PATCH] Move print and println to cli platform They don't need to be bundled with everything. --- cli.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ runtime.c | 53 ----------------------------------------------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/cli.c b/cli.c index 1a5cd66d..e0307ddf 100644 --- a/cli.c +++ b/cli.c @@ -1,3 +1,55 @@ +struct object* print(struct object* obj) { + if (is_num(obj)) { + printf("%ld", num_value(obj)); + } else if (is_list(obj)) { + putchar('['); + while (!is_empty_list(obj)) { + print(list_first(obj)); + obj = list_rest(obj); + if (!is_empty_list(obj)) { + putchar(','); + putchar(' '); + } + } + putchar(']'); + } else if (is_record(obj)) { + struct record* record = as_record(obj); + putchar('{'); + for (size_t i = 0; i < record->size; i++) { + printf("%s = ", record_keys[record->fields[i].key]); + print(record->fields[i].value); + if (i + 1 < record->size) { + fputs(", ", stdout); + } + } + putchar('}'); + } else if (is_closure(obj)) { + fputs("", stdout); + } else if (is_string(obj)) { + putchar('"'); + for (uword i = 0; i < string_length(obj); i++) { + putchar(string_at(obj, i)); + } + putchar('"'); + } else if (is_variant(obj)) { + putchar('#'); + printf("%s ", variant_names[variant_tag(obj)]); + print(variant_value(obj)); + } else if (is_hole(obj)) { + fputs("()", stdout); + } else { + assert(is_heap_object(obj)); + fprintf(stderr, "unknown tag: %lu\n", as_heap_object(obj)->tag); + abort(); + } + return obj; +} + +struct object* println(struct object* obj) { + print(obj); + putchar('\n'); + return obj; +} int main() { heap = make_heap(MEMORY_SIZE); HANDLES(); diff --git a/runtime.c b/runtime.c index 27cddb3b..96abfd0c 100644 --- a/runtime.c +++ b/runtime.c @@ -699,56 +699,3 @@ bool string_equal_cstr_len(struct object* string, const char* cstr, uword len) { const char* record_keys[]; const char* variant_names[]; - -struct object* print(struct object* obj) { - if (is_num(obj)) { - printf("%ld", num_value(obj)); - } else if (is_list(obj)) { - putchar('['); - while (!is_empty_list(obj)) { - print(list_first(obj)); - obj = list_rest(obj); - if (!is_empty_list(obj)) { - putchar(','); - putchar(' '); - } - } - putchar(']'); - } else if (is_record(obj)) { - struct record* record = as_record(obj); - putchar('{'); - for (size_t i = 0; i < record->size; i++) { - printf("%s = ", record_keys[record->fields[i].key]); - print(record->fields[i].value); - if (i + 1 < record->size) { - fputs(", ", stdout); - } - } - putchar('}'); - } else if (is_closure(obj)) { - fputs("", stdout); - } else if (is_string(obj)) { - putchar('"'); - for (uword i = 0; i < string_length(obj); i++) { - putchar(string_at(obj, i)); - } - putchar('"'); - } else if (is_variant(obj)) { - putchar('#'); - printf("%s ", variant_names[variant_tag(obj)]); - print(variant_value(obj)); - } else if (is_hole(obj)) { - fputs("()", stdout); - } else { - assert(is_heap_object(obj)); - fprintf(stderr, "unknown tag: %lu\n", as_heap_object(obj)->tag); - abort(); - } - return obj; -} - -struct object* println(struct object* obj) { - print(obj); - putchar('\n'); - return obj; -}