-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
85 lines (72 loc) · 1.56 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
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 0x000000000000000AL
#define FALSE 0x0000000000000002L
#define MASK 0xFFFFFFFFFFFFFFF8L
#define HEAP_SIZE 100000
#define NUM_MIN (-(1L << 62))
#define NUM_MAX ((1L << 62) - 1)
extern int64_t our_main();
extern int64_t *new (int64_t);
extern void print(int64_t);
extern void error(int64_t);
int64_t *mem;
int64_t alloced;
void error(int64_t val) {
switch (val) {
case 0:
fprintf(stderr, "overflow");
break;
}
exit(1);
}
void pr(int64_t val) {
if ((val & 0x1L) == 1) {
fprintf(stdout, "%" PRId64, val >> 1);
} else {
if (val == TRUE)
fprintf(stdout, "%s", "true");
else if (val == FALSE)
fprintf(stdout, "%s", "false");
else if ((val & 0x7) == 0) {
fprintf(stdout, "( ");
int64_t *ptr = (int64_t *)(val & MASK);
int64_t n = *ptr;
for (int i = 0; i < n; i++) {
pr(ptr[i + 1]);
if (i < n - 1) {
fprintf(stdout, ", ");
} else {
fprintf(stdout, " ");
}
}
fprintf(stdout, ")");
} else {
fprintf(stderr, "UNKNOWN: %" PRId64 "\n", val);
exit(1);
}
}
}
void print(int64_t val) {
pr(val);
fprintf(stdout, "\n");
}
int64_t *new (int64_t size) {
int64_t *res = mem;
alloced += size;
if (alloced > HEAP_SIZE) {
fprintf(stderr, "Heap exceeded\n");
exit(1);
}
mem += size;
return res;
}
int main() {
mem = calloc(HEAP_SIZE, sizeof(int64_t));
int64_t result = our_main();
print(result);
return 0;
}