Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}