-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.c
105 lines (84 loc) · 2.95 KB
/
lib.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
#include "lib.h"
// clang-format doesn't understand these library functions
// clang-format off
#define triangular \
"("DEF_KEY" recursive_triangular_interpreted \
("LAMBDA_KEY" (n) \
("IF_KEY" (zero? n) \
0 \
("ADD_NAME" n (recursive_triangular_interpreted (sub1 n))))))"
#define tetrahedral \
"("DEF_KEY" recursive_tetrahedral_interpreted \
("LAMBDA_KEY" (n) \
("IF_KEY" (zero? n) \
0 \
("ADD_NAME" (recursive_triangular_interpreted n) \
(recursive_tetrahedral_interpreted (sub1 n))))))"
#define supertetrahedral \
"("DEF_KEY" recursive_supertetrahedral_interpreted \
("LAMBDA_KEY" (n) \
("IF_KEY" (zero? n) \
0 \
("ADD_NAME" (recursive_tetrahedral_interpreted n) \
(recursive_supertetrahedral_interpreted (sub1 n))))))"
#define fact_rec \
"("DEF_KEY" recursive_factorial_interpreted \
("LAMBDA_KEY" (n) \
("IF_KEY" (< n 2) \
1 \
("MUL_NAME" n (recursive_factorial_interpreted (sub1 n))))))"
#define fact_iter \
"("DEF_KEY" iterative_factorial_interpreted \
("LAMBDA_KEY" (n) \
("DEF_KEY" loop \
("LAMBDA_KEY" (count total) \
("IF_KEY" (< count 2) \
total \
(loop (sub1 count) \
("MUL_NAME" total count))))) \
(loop n 1)))"
#define fib_rec \
"("DEF_KEY" recursive_fibonacci_interpreted \
("LAMBDA_KEY" (n) \
("IF_KEY" (< n 2) \
n \
("ADD_NAME" (recursive_fibonacci_interpreted (- n 1)) \
(recursive_fibonacci_interpreted (- n 2))))))"
#define fib_iter \
"("DEF_KEY" iterative_fibonacci_interpreted \
("LAMBDA_KEY" (n) \
("DEF_KEY" loop \
("LAMBDA_KEY" (count a b) \
("IF_KEY" (one? count) \
b \
(loop (sub1 count) \
b \
("ADD_NAME" a b))))) \
(loop n 0 1)))"
/*
newlines are needed because of some quirk in the parsing process
(see read.c and parse.c)
*/
char* library[] = {
fact_rec "\n", fact_iter "\n", fib_rec "\n", fib_iter "\n",
triangular "\n", tetrahedral "\n", supertetrahedral "\n",
};
// clang-format on
/*
lib_len should match the length of library
(this has to be handled manually?)
*/
int lib_len = 7;
/* library loading */
int lib_counter = 0;
char* load_library(void) {
if (DEBUG) print_lib();
char* lib_entry = library[lib_counter];
lib_counter++;
return lib_entry;
}
void print_lib(void) {
char* lib_entry = library[lib_counter];
printf("loading library entry: %s\n", lib_entry);
}
bool lib_loaded(void) { return lib_counter >= lib_len; }