-
Notifications
You must be signed in to change notification settings - Fork 2
/
runtime.c
68 lines (57 loc) · 1.37 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
68
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
// Constants for representing integers
#define num_shift 2
#define num_mask 0b11
#define num_tag 0b00
// Constants for representing booleans
#define bool_shift 7
#define bool_mask 0b1111111
#define bool_tag 0b0011111
#define heap_mask 0b111
#define pair_tag 0b010
#define fn_tag 0b110
// true: represented as 0b10011111
// false: represented as 0b00011111
extern uint64_t entry(void *heap);
uint64_t read_num() {
int r;
scanf("%d", &r);
return (uint64_t)(r) << num_shift;
}
void print_value(uint64_t value) {
if ((value & num_mask) == num_tag) {
int64_t ivalue = (int64_t)value;
printf("%" PRIi64, ivalue >> num_shift);
} else if ((value & bool_mask) == bool_tag) {
if (value >> bool_shift) {
printf("true");
} else {
printf("false");
}
} else if ((value & heap_mask) == pair_tag) {
uint64_t v1 = *(uint64_t*)(value - pair_tag);
uint64_t v2 = *(uint64_t*)(value - pair_tag + 8);
printf("(pair ");
print_value(v1);
printf(" ");
print_value(v2);
printf(")");
} else if ((value & heap_mask) == fn_tag) {
printf("<function>");
} else {
printf("BAD VALUE %" PRIi64, value);
}
}
void error() {
printf("ERROR");
exit(1);
}
void print_newline() {
printf("\n");
}
int main(int argc, char **argv) {
entry((void*)malloc(4096));
return 0;
}