-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext.fxl
109 lines (100 loc) · 1.79 KB
/
context.fxl
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
# Compile an index into C code.
extend
(
load "lib/render/C.fxl";
load "lib/render/base.fxl";
load "lib/index/index.fxl";
std
)
\;
# Process all the rows in an index.
\do_rows=
(@\\do_rows \checked_bound\pos\rows
rows () \row\rows
row \is_leaf\key\val
\\check_ch=
(
\ch=(slice key pos 1)
checked_bound () (say ["if (n <= "(num_str pos)") return 0;"])
say ["if (x["(num_str pos)"] == '"ch"')"]
)
\len=(length key)
is_leaf
(
\\do_strncmp=
(
say ["if (n == "(num_str len)" && strncmp(x,"
(fexl_quote key)",n) == 0)"]
indent (say ["return "(fexl_quote val)";"])
)
lt pos len
(
check_ch
scope
(
do_strncmp
say "return 0;"
)
do_rows T pos rows
);
eq len 0
(
# Optimize null key.
say ["if (n == "(num_str pos)")"]
indent (say ["return "(fexl_quote val)";"])
do_rows T pos rows
)
(
do_strncmp
do_rows checked_bound pos rows
)
)
(
check_ch
scope
(
do_rows F len val
say "return 0;"
)
do_rows T pos rows
)
)
# (compile_index index)
# Return C code which implements the index.
\compile_index=
(\index
say "const char *lookup(const char *x, unsigned long n)"
scope
(
index
(
# Special case for empty index.
say "(void)x;"
say "(void)n;"
)
(\_\_
do_rows F 0 index
)
say "return 0;"
)
)
# (index_put_pairs pairs index)
# Put all the {key val} pairs into the index.
\index_put_pairs=
(@\\loop\pairs\index
pairs index \pair\pairs
pair \key\val
\index=(index_put key val index)
loop pairs index
)
# (compile_pairs pairs)
# Return C code which looks up the key value pairs.
\compile_pairs=
(\pairs
\index=(index_put_pairs pairs [])
compile_index index
)
def "compile_index" compile_index;
def "index_put_pairs" index_put_pairs;
def "compile_pairs" compile_pairs;
std