about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--modal/.gitignore1
-rw-r--r--modal/logic.modal4
-rw-r--r--modal/modal.c252
3 files changed, 257 insertions, 0 deletions
diff --git a/modal/.gitignore b/modal/.gitignore
new file mode 100644
index 0000000..d08a843
--- /dev/null
+++ b/modal/.gitignore
@@ -0,0 +1 @@
+modal
\ No newline at end of file
diff --git a/modal/logic.modal b/modal/logic.modal
new file mode 100644
index 0000000..7699841
--- /dev/null
+++ b/modal/logic.modal
@@ -0,0 +1,4 @@
+<> (eq ?x ?x) (#t)
+<> (eq ?x ?y) (#f)
+
+.. (eq fox bat)
\ No newline at end of file
diff --git a/modal/modal.c b/modal/modal.c
new file mode 100644
index 0000000..c7bf0f8
--- /dev/null
+++ b/modal/modal.c
@@ -0,0 +1,252 @@
+/* 
+	by https://wryl.tech/
+ 	from https://wiki.xxiivv.com/site/modal
+ 	cc modal.c -o modal
+*/
+
+#include <stdio.h>
+
+typedef struct {
+	int id;
+	char *a, *b;
+} Rule;
+
+static int flip, rmin = 0xff, rmax = 0x00, cycles = 0x10000;
+static Rule rules[0x1000], lambda, *rules_ = rules;
+static char dict[0x8000], *dict_ = dict;
+static char bank_a[0x4000], *src_ = bank_a;
+static char bank_b[0x4000], *dst_ = bank_b;
+static char *regs[0x100];
+
+#define spacer(c) (c <= ' ' || c == '(' || c == ')')
+
+static char *
+walk(char *s)
+{
+	char c;
+	int depth = 0;
+	if(*s == '(') {
+		while((c = *s++)) {
+			if(c == '(') depth++;
+			if(c == ')') --depth;
+			if(!depth) return s;
+		}
+	}
+	while((c = *s) && !spacer(c)) s++;
+	return s;
+}
+
+static int
+set_reg(int r, char *b)
+{
+	if(regs[r]) {
+		char *a = regs[r], *aa = walk(a), *bb = walk(b);
+		while(a < aa && b < bb)
+			if(*a++ != *b++) return 0;
+	} else {
+		regs[r] = b;
+		if(r < rmin) rmin = r;
+		if(r > rmax) rmax = r;
+	}
+	return 1;
+}
+
+static void
+put_reg(char r)
+{
+	char c, *s = regs[(int)r], *ss;
+	if(!s) {
+		*dst_++ = '?', *dst_++ = r;
+		return;
+	}
+	ss = walk(s);
+	if(r == '*') {
+		int i, depth = 0;
+		if(*s == '(') { /* special explode tuple */
+			s++;
+			while(s < ss) {
+				while((c = *s) && !spacer(c))
+					*dst_++ = c, s++;
+				*dst_++ = ' ';
+				*dst_++ = '(', s++, depth++;
+			}
+		} else { /* special explode token */
+			while((c = *s++) && !spacer(c))
+				*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
+		}
+		for(i = 0; i < depth; i++)
+			*dst_++ = ')';
+	} else if(r == '.') { /* special unpack */
+		if(*s == '(') s++, --ss;
+		while(s < ss) *dst_++ = *s++;
+	} else if(r == '^') { /* special join */
+		if(*s == '(') s++, --ss;
+		while(s < ss && (c = *s++))
+			if(!spacer(c)) *dst_++ = c;
+	} else if(r == '~') { /* special stdin */
+		while(fread(&c, 1, 1, stdin) && c >= ' ')
+			*dst_++ = c;
+	} else if(r == ':') { /* special stdout */
+		if(*s == '(') s++, --ss;
+		while(s < ss) {
+			c = *s++;
+			if(c == '\\') {
+				switch(*s++) {
+				case 't': putc(0x09, stdout); break;
+				case 'n': putc(0x0a, stdout); break;
+				case 's': putc(0x20, stdout); break;
+				}
+			} else
+				putc(c, stdout);
+		}
+	} else
+		while(s < ss) *dst_++ = *s++;
+}
+
+static char *
+match_rule(Rule *r, char *p)
+{
+	int i;
+	char c, last = 0, *a = r->a, *b = p;
+	if(rmax) {
+		for(i = rmin; i <= rmax; i++)
+			regs[i] = 0;
+		rmin = 0xff, rmax = 0x00;
+	}
+	while((c = *a)) {
+		if(spacer(last) && c == '?') {
+			if(!set_reg(*(++a), b)) return NULL;
+			a++, b = walk(b);
+			continue;
+		}
+		if(c != *b) return NULL;
+		a++, b++, last = c;
+	}
+	c = *b;
+	return spacer(c) ? b : NULL;
+}
+
+static int
+commit_rule(Rule *r, char *s, int create)
+{
+	while((*dst_++ = *s++))
+		;
+	*dst_++ = 0;
+	if((flip = !flip))
+		src_ = bank_b, dst_ = bank_a;
+	else
+		src_ = bank_a, dst_ = bank_b;
+	if(create)
+		fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
+	else
+		fprintf(stderr, "%02d %s\n", r->id, src_);
+	return 1;
+}
+
+static int
+write_rule(Rule *r, char last, char *res)
+{
+	char c, *b = r->b, *origin = dst_;
+	while((c = *b++))
+		if(spacer(last) && c == '?')
+			put_reg(*b++);
+		else
+			*dst_++ = c, last = c;
+	if(dst_ == origin) {
+		while(*res == ' ') res++;
+		if(*res == ')' && *(dst_ - 1) == ' ') dst_--;
+	}
+	return commit_rule(r, res, 0);
+}
+
+static char *
+parse_frag(char *s)
+{
+	char c, *ss;
+	while((c = *s) && c <= ' ') s++;
+	if(*s != ')' && !(*s == '<' && s[1] == '>')) {
+		ss = walk(s);
+		if(*s == '(') {
+			s++;
+			while(s < ss - 1) *dict_++ = *s++;
+			s++;
+		} else
+			while(s < ss) *dict_++ = *s++;
+	}
+	*dict_++ = 0;
+	return s;
+}
+
+static char *
+create_rule(Rule *r, int id, char *s)
+{
+	r->id = id, s += 2;
+	r->a = dict_, s = parse_frag(s);
+	r->b = dict_, s = parse_frag(s);
+	return s;
+}
+
+static int
+rewrite(void)
+{
+	char c, last = 0, *cap, *s = src_, *res;
+	while((c = *s) && c <= ' ') s++;
+	while((c = *s)) {
+		if(spacer(last)) {
+			Rule *r;
+			if(*s == '<' && s[1] == '>') {
+				r = rules_++;
+				s = create_rule(r, rules_ - rules - 1, s);
+				while((c = *s) && c <= ' ') s++;
+				return commit_rule(r, s, 1);
+			}
+			if(*s == '?' && s[1] == '(') {
+				r = &lambda, cap = walk(s + 1);
+				create_rule(&lambda, -1, s), s = cap;
+				while((c = *s) && c <= ' ') s++;
+				if((res = match_rule(&lambda, s)) != NULL)
+					return write_rule(&lambda, last, res);
+			}
+			for(r = rules; r < rules_; r++)
+				if((res = match_rule(r, s)) != NULL)
+					return write_rule(r, last, res);
+		}
+		*dst_++ = last = c;
+		s++;
+	}
+	*dst_++ = 0;
+	return 0;
+}
+
+int
+main(int argc, char **argv)
+{
+	FILE *f;
+	int i;
+	char c, *w = bank_a;
+	if(argc < 2)
+		return !printf("usage: modal [-vqn] source.modal\n");
+	for(i = 1; i < argc && *argv[i] == '-'; i++) {
+		switch(argv[i][1]) {
+		case 'v': /* version */ return !printf("Modal Interpreter, 18 Apr 2024.\n");
+		case 'q': /* quiet */ fclose(stderr); break;
+		case 'n': /* infinite */ cycles = 0xffffffff; break;
+		}
+	}
+	if(!(f = fopen(argv[i], "r")))
+		return !fprintf(stdout, "Invalid Modal file: %s.\n", argv[i]);
+	while(fread(&c, 1, 1, f)) {
+		c = c <= 0x20 ? 0x20 : c;
+		if(w > bank_a) {
+			if(c == ' ' && *(w - 1) == '(') continue;
+			if(c == ')' && *(w - 1) == ' ') w--;
+			if(c == ' ' && *(w - 1) == ' ') w--;
+		}
+		*w++ = c;
+	}
+	while(*(--w) <= ' ') *w = 0;
+	fclose(f);
+	while(rewrite())
+		if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n");
+	return 0;
+}
\ No newline at end of file
akkartik.com> 2016-12-26 20:58:37 -0800 3713 - cross-link calls with definitions in html' href='/akkartik/mu/commit/html/041jump_target.cc.html?h=main&id=201458e3bd2f1d79a0ea0b853552e9df267e92b1'>201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^






201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^
201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^
201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^
201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^

201458e3 ^

204dae92 ^

201458e3 ^
204dae92 ^

201458e3 ^

204dae92 ^


201458e3 ^

204dae92 ^



201458e3 ^
204dae92 ^
201458e3 ^

204dae92 ^


201458e3 ^
204dae92 ^
201458e3 ^

204dae92 ^


201458e3 ^

204dae92 ^

201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^










201458e3 ^
204dae92 ^










201458e3 ^
204dae92 ^










201458e3 ^
204dae92 ^













201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^

201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^




201458e3 ^
204dae92 ^


201458e3 ^
204dae92 ^








201458e3 ^
65361948 ^


a654e4ec ^
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249