Match subterms. Only 2 test failures for the compiler, now.
Cat's Eye Technologies
11 years ago
2 | 2 |
FILES="scanner term tamsin"
|
3 | 3 |
|
4 | 4 |
for FILE in $FILES; do
|
5 | |
gcc -c $FILE.c -o $FILE.o
|
|
5 |
gcc -g -c $FILE.c -o $FILE.o
|
6 | 6 |
done
|
7 | 7 |
|
8 | 8 |
ar -r libtamsin.a scanner.o term.o tamsin.o
|
117 | 117 |
|
118 | 118 |
int term_match(struct term *pattern, struct term *ground)
|
119 | 119 |
{
|
|
120 |
struct term_list *tl1, *tl2;
|
|
121 |
|
120 | 122 |
if (pattern->storing != NULL) {
|
121 | 123 |
pattern->storing = ground;
|
122 | 124 |
return 1;
|
|
127 | 129 |
if (pattern->subterms == NULL && ground->subterms == NULL) {
|
128 | 130 |
return 1;
|
129 | 131 |
}
|
130 | |
/* deal with subterms here */
|
131 | |
return 0;
|
|
132 |
|
|
133 |
tl1 = pattern->subterms;
|
|
134 |
tl2 = ground->subterms;
|
|
135 |
while (tl1 != NULL && tl2 != NULL) {
|
|
136 |
if (!term_match(tl1->term, tl2->term)) {
|
|
137 |
return 0;
|
|
138 |
}
|
|
139 |
tl1 = tl1->next;
|
|
140 |
tl2 = tl2->next;
|
|
141 |
}
|
|
142 |
if (tl1 != NULL || tl2 != NULL) {
|
|
143 |
/* not the same # of subterms */
|
|
144 |
return 0;
|
|
145 |
}
|
|
146 |
return 1;
|
132 | 147 |
}
|
742 | 742 |
| blerf(a) = return zzrk.
|
743 | 743 |
| blerf(b) = return zon.
|
744 | 744 |
| blerf(c) = return zzt.
|
745 | |
? No 'blerf' production matched arguments [d]
|
|
745 |
? No 'blerf' production matched arguments
|
746 | 746 |
|
747 | 747 |
Thus, we can write productions that recursively call themselves, and
|
748 | 748 |
terminate on the base case.
|
0 | 0 |
# This example Tamsin program was written by Chris Pressey, and is
|
1 | 1 |
# hereby placed in the public domain. It comes with NO WARRANTY.
|
2 | 2 |
|
3 | |
# main = blerf(world).
|
4 | |
|
5 | 3 |
main = blerf(tree(a, b)).
|
6 | |
blerf(tree(a, X)) = return hello(X).
|
7 | |
blerf(X) = return X.
|
8 | |
|
9 | |
|
10 | |
|
11 | |
# blerf(tree(X, Y)) = return X.
|
|
4 |
blerf(tree(X, Y)) = return X.
|
0 | 0 |
#!/bin/sh
|
1 | 1 |
|
2 | 2 |
bin/tamsin compile $1 > foo.c && \
|
3 | |
gcc -Ic_src -Lc_src foo.c -o foo -ltamsin && \
|
|
3 |
gcc -g -Ic_src -Lc_src foo.c -o foo -ltamsin && \
|
4 | 4 |
./foo
|
5 | 5 |
R=$?
|
6 | 6 |
#rm -f foo.c foo
|
48 | 48 |
|
49 | 49 |
scanner = scanner_new(bufterm->atom);
|
50 | 50 |
ok = 0;
|
51 | |
result = NULL;
|
|
51 |
result = term_new("nil");
|
52 | 52 |
|
53 | 53 |
program_main0();
|
54 | 54 |
|
|
144 | 144 |
for f in formals:
|
145 | 145 |
self.emit_term(f, "pattern%s" % i, pattern=True)
|
146 | 146 |
self.emit("if (!term_match(pattern%s, i%s)) {" % (i, i))
|
|
147 |
self.indent()
|
147 | 148 |
|
148 | 149 |
# ...
|
149 | 150 |
next = None
|
|
158 | 159 |
|
159 | 160 |
if next:
|
160 | 161 |
args = ', '.join(["i%s" % i for i in xrange(0, len(formals))])
|
161 | |
self.emit(" program_%s(%s);" % (next, args))
|
162 | |
self.emit(" return;")
|
|
162 |
self.emit("program_%s(%s);" % (next, args))
|
|
163 |
else:
|
|
164 |
self.emit('result = term_new'
|
|
165 |
'("No \'%s\' production matched arguments");' %
|
|
166 |
self.current_prod_name)
|
|
167 |
self.emit("ok = 0;")
|
|
168 |
self.emit("return;")
|
|
169 |
self.outdent()
|
163 | 170 |
self.emit("}")
|
164 | 171 |
self.emit("")
|
165 | 172 |
|