-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathruntime.c
67 lines (58 loc) · 1.31 KB
/
runtime.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
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
int64_t *heap;
int64_t *rootstack;
int64_t *free_ptr;
const int64_t TRUE = 0xffffffffffffffff;
const int64_t FALSE = 0x7fffffffffffffff;
void rec_print(int64_t val) {
if(val & 0x00000001 ^ 0x00000001) {
printf("%" PRId64, val >> 1);
}
else if(val == TRUE) {
printf("#t");
}
else if(val == FALSE) {
printf("#f");
}
else if((val & 0x00000003) == 0x00000001) {
int64_t *tup_base = (int64_t*)(val - 1);
int tup_count = *tup_base;
printf("(");
for (int i = 1; i < tup_count + 1; i++) {
int64_t ith_val = *(tup_base + i);
rec_print(ith_val);
if (i != tup_count) {
printf(", ");
}
}
printf(")");
}
else {
printf("Unknown value: %#010x", val);
}
}
int64_t print(int64_t val) {
rec_print(val);
printf("\n");
return val;
}
void initialize() {
heap = malloc(200);
rootstack = malloc(200);
free_ptr = heap;
}
/* FIXME: right now, correct `val` is not passed */
void error_not_number(int64_t val) {
fprintf(stderr, "expected a number: %#010x\n", print(val));
exit(1);
}
void error_not_bool(int64_t val) {
fprintf(stderr, "expected a boolean: %#010x\n", val);
exit(1);
}
void error_not_tuple(int64_t val) {
fprintf(stderr, "expected a tuple: %#010x\n", val);
exit(1);
}