-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
92 lines (82 loc) · 2.02 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include "types.h"
#include "runtime.h"
void print_result(int64_t);
void error_exit() {
printf("err\n");
exit(1);
}
void raise_error() {
return error_handler();
}
int main(int argc, char** argv) {
in = stdin;
out = stdout;
error_handler = &error_exit;
heap = malloc(8 * heap_size);
int64_t result = entry(heap);
print_result(result);
if (result != val_void) printf("\n");
free(heap);
return 0;
}
void print_char(int64_t);
void print_cons(int64_t);
void print_string(int64_t v);
void print_string_char(int64_t v);
void print_result(int64_t result) {
if (cons_type_tag == (ptr_type_mask & result)) {
printf("(");
print_cons(result);
printf(")");
} else if (box_type_tag == (ptr_type_mask & result)) {
printf("#&");
print_result (*((int64_t *)(result ^ box_type_tag)));
// NEW IN HUSTLE+
} else if (string_type_tag == (ptr_type_mask & result)) {
print_string(result);
} else if (int_type_tag == (int_type_mask & result)) {
printf("%" PRId64, result >> int_shift);
} else if (char_type_tag == (char_type_mask & result)) {
print_char(result);
} else {
switch (result) {
case val_true:
printf("#t"); break;
case val_false:
printf("#f"); break;
case val_eof:
printf("#<eof>"); break;
case val_empty:
printf("()"); break;
case val_void:
/* nothing */ break;
}
}
}
void print_cons(int64_t a) {
int64_t car = *((int64_t *)((a + 8) ^ cons_type_tag));
int64_t cdr = *((int64_t *)((a + 0) ^ cons_type_tag));
print_result(car);
if (cdr == val_empty) {
// nothing
} else if (cons_type_tag == (ptr_type_mask & cdr)) {
printf(" ");
print_cons(cdr);
} else {
printf(" . ");
print_result(cdr);
}
}
// Provided for you:
void print_string(int64_t v) {
int64_t* str = (int64_t *)(v ^ string_type_tag);
int64_t len = (str[0] >> int_shift);
int i;
printf("\"");
for (i = 0; i < len; i++)
print_string_char(str[i+1]);
printf("\"");
}