Skip to content

Commit

Permalink
Move print and println to cli platform
Browse files Browse the repository at this point in the history
They don't need to be bundled with everything.
  • Loading branch information
tekknolagi committed Jun 6, 2024
1 parent 782c93c commit dc6cc2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 53 deletions.
52 changes: 52 additions & 0 deletions cli.c
Original file line number Diff line number Diff line change
@@ -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("<closure>", 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();
Expand Down
53 changes: 0 additions & 53 deletions runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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("<closure>", 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;
}

0 comments on commit dc6cc2f

Please sign in to comment.