-
Notifications
You must be signed in to change notification settings - Fork 6
/
struct.h
129 lines (106 loc) · 2.17 KB
/
struct.h
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
#ifndef __STRUCT_H__
#define __STRUCT_H__
#include <stdint.h>
#define TRUE 1
#define FALSE 0
#define FIXNUM_ENCODING 1
#ifndef USE_JIT
# define USE_JIT 0
#endif
#define SPECIALIZE_AFTER_COUNT 100
enum {
num_type,
func_type,
sym_type,
debruijn_type,
plus_type,
minus_type,
times_type,
app_type,
lambda_type,
if0_type
};
enum {
env_type = 100
};
typedef struct env env;
typedef struct lambda_expr lambda_expr;
typedef struct tagged {
int type;
} tagged;
#if !FIXNUM_ENCODING
# define TAGGED_TYPE(t) ((t)->type)
#else
# define TAGGED_TYPE(t) ((((intptr_t)(t)) & 0x1) ? num_type : ((t)->type))
#endif
#if !FIXNUM_ENCODING
typedef struct num_val {
tagged t;
int n;
} num_val;
# define NUM_VAL(v) (((num_val *)(v))->n)
#else
# define NUM_VAL(v) (((intptr_t)(v)) >> 1)
#endif
#if USE_JIT
typedef tagged* (*jitted_proc)();
#endif
typedef struct func_val {
tagged t;
lambda_expr *lam;
env* e;
# if USE_JIT
int specialize_counter;
# endif
} func_val;
typedef struct symbol {
tagged t;
char *s;
} symbol;
typedef struct debruijn_expr {
tagged t;
int pos;
} debruijn_expr;
typedef struct bin_op_expr {
tagged t;
tagged *left, *right;
} bin_op_expr;
typedef struct lambda_expr {
tagged t;
symbol *arg_name;
tagged *body;
# if USE_JIT
jitted_proc code;
jitted_proc tail_code;
# endif
} lambda_expr;
typedef struct if0_expr {
tagged t;
tagged *tst;
tagged *thn;
tagged *els;
} if0_expr;
struct env {
int type;
symbol *id;
tagged *val;
struct env *rest;
};
#if !FIXNUM_ENCODING
tagged* make_num(int n);
#else
# define make_num(n) ((tagged *)(intptr_t)(((n) << 1) | 0x1))
#endif
tagged* make_func(tagged *lambda, env *e);
symbol* make_symbol(char *s);
int same_symbol(symbol* a, symbol *b);
tagged* make_debruijn(int pos);
tagged* make_bin_op(int type, tagged *left, tagged *right);
tagged* make_plus(tagged *left, tagged *right);
tagged* make_minus(tagged *left, tagged *right);
tagged* make_times(tagged *left, tagged *right);
tagged* make_app(tagged *left, tagged *right);
tagged* make_lambda(symbol *arg_name, tagged *body);
tagged* make_if0(tagged *tst, tagged *thn, tagged *els);
env* make_env(symbol *id, tagged *val, env *rest);
#endif