-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadverb.h
154 lines (136 loc) · 6.35 KB
/
adverb.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2010, 2011 Andrey Zholos
This file is part of kuc, a vector programming language.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ADVERB_H
#define ADVERB_H
#include "value.h"
enum {
// adverb names
adverb_each,
adverb_fold,
adverb_scan,
adverb_each_right,
adverb_each_left,
adverb_each_pair,
adverb_named_last = adverb_each_pair,
// clause equivalents
adverb_scan_count,
// adverbs by type
adverb_each_call,
adverb_each_select,
adverb_repeat,
adverb_binary_fold,
adverb_variadic_fold,
adverb_trace,
adverb_trace_count,
adverb_binary_scan,
adverb_variadic_scan,
adverb_binary_scan_count,
adverb_variadic_scan_count,
adverb_each_right_call,
adverb_each_right_join,
adverb_each_left_call,
adverb_each_left_split,
adverb_each_pair_call,
adverb_each_pair_concurrent
};
extern const struct adverb {
char name;
bool colon;
index (*valence)(Value verb);
index (*min_valence)(Value verb);
struct value_clause literal;
} adverbs[24];
extern const struct adverb_run {
Value (*unary)(Value, Value);
Value (*binary)(Value, Value, Value);
Value (*variadic)(Value, index n, const Value*); // n > 2
} adverbs_run[24];
Value each(Value, Value);
Value fold(Value, Value);
Value scan(Value, Value);
Value each_right(Value, Value);
Value each_left(Value, Value);
Value each_pair(Value, Value);
Value scan_count(Value, Value);
Value each_call_unary(Value, Value);
Value each_call_binary(Value, Value, Value);
Value each_call_variadic(Value, index, const Value*);
Value each_select_unary(Value, Value);
Value each_select_binary(Value, Value, Value);
Value each_select_variadic(Value, index, const Value*);
Value changing(Value, Value);
Value trace_changing(Value, Value);
Value trace_count_changing(Value, Value);
Value loop(Value, Value, Value);
Value trace_loop(Value, Value, Value);
Value trace_count_loop(Value, Value, Value);
Value binary_fold_unary(Value, Value);
Value binary_fold_binary(Value, Value, Value);
Value variadic_fold(Value, index, const Value*);
Value binary_scan_unary(Value, Value);
Value binary_scan_binary(Value, Value, Value);
Value variadic_scan(Value, index, const Value*);
Value binary_scan_count_unary(Value, Value);
Value binary_scan_count_binary(Value, Value, Value);
Value variadic_scan_count(Value, index, const Value*);
Value each_right_call(Value, Value, Value);
Value each_left_call(Value, Value, Value);
Value each_right_join(Value, Value);
Value each_left_split(Value, Value);
Value each_pair_call_unary(Value, Value);
Value each_pair_call_binary(Value, Value, Value);
Value each_pair_concurrent(Value, Value);
// note: r is accounted by the user
#define AUTO_STRENGTHEN(r, count, call_first, call, inner) { \
index as_n; \
count(as_n) \
if (as_n <= 0) \
r = (MValue)∅ \
else { \
Value as_v; index as_i = 0; \
call_first(as_v, as_i) \
Type as_type = as_v->type; \
if (as_v->vector || !types[as_type].vectorable) { \
r = create_items(type_variant, as_n); \
goto as_mixed; \
} \
size_t as_size = types[as_type].size; \
short as_offset = types[as_type].offset; \
r = create_items(as_type, as_n); \
goto as_vector; \
\
for (; as_i < as_n; as_i++) { \
inner; \
call(as_v, as_i) \
if (as_v->vector || as_v->type != as_type) { \
r = weaken_to(r, as_i); \
goto as_mixed; \
} \
as_vector: \
COPY(r->items + as_size * as_i, as_v->item + as_offset, as_size); \
} \
goto as_done; \
\
for (; as_i < as_n; as_i++) { \
inner; \
call(as_v, as_i); \
as_mixed: \
r->mixed[as_i] = as_v; \
} \
\
as_done:; \
} \
}
#endif /* ADVERB_H */