-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.c
131 lines (112 loc) · 2.25 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <stdint.h>
#define heap_size 32*1024*1024
#define globals_size heap_size
#define word_size 4
#define int int32_t
#define fixnum_mask 3
#define fixnum_tag 0
#define fixnum_shift 2
#define char_mask 255
#define char_tag 15
#define char_shift 8
#define bool_mask 127
#define bool_tag 31
#define bool_shift 7
#define empty_list 47
#define pair_mask 7
#define pair_tag 1
#define pair_shift 3
#define vector_mask 7
#define vector_tag 2
#define vector_shift 3
#define string_mask 7
#define string_tag 3
#define string_shift 3
#define closure_mask 7
#define closure_tag 6
#define closure_shift 3
typedef struct
{
int * heap;
int * globals;
} memlayout;
void print(int val)
{
if((val & fixnum_mask) == fixnum_tag)
{
printf("%d", val >> fixnum_shift);
}
else if((val & char_mask) == char_tag)
{
printf("%c", (char)(val >> char_shift));
}
else if((val & bool_mask) == bool_tag)
{
if(val >> bool_shift)
{
printf("#t");
}
else
{
printf("#f");
}
}
else if(val == empty_list)
{
printf("()");
}
else if((val & pair_mask) == pair_tag)
{
int car = *(int *)(val - pair_tag);
int cdr = *(int *)(val + word_size - pair_tag);
printf("(");
print(car);
printf(" . ");
print(cdr);
printf(")");
}
else if((val & vector_mask) == vector_tag)
{
int length = *(int *)(val - vector_tag);
printf("#(");
int i;
for(i = word_size; i <= length; i = i + word_size)
{
if(i != word_size) printf(" ");
print(*(int *)(val + i - vector_tag));
}
printf(")");
}
else if((val & string_mask) == string_tag)
{
int length = *(int *)(val - string_tag);
printf("\"");
int i;
for(i = word_size; i <= length; i = i + word_size)
{
printf("%c", *(int *)(val + i - string_tag));
}
printf("\"");
}
else if((val & closure_mask) == closure_tag)
{
printf("#<procedure>");
}
else
{
printf("error: unknown type");
}
}
int main(int argc, char** argv)
{
memlayout m;
m.heap = malloc(heap_size);
m.globals = malloc(globals_size);
int val = scheme_entry(&m);
print(val);
printf("\n");
free(m.heap);
free(m.globals);
return 0;
}