git @ Cat's Eye Technologies Tamsin / e425185
Match subterms. Only 2 test failures for the compiler, now. Cat's Eye Technologies 11 years ago
6 changed file(s) with 31 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
22 FILES="scanner term tamsin"
33
44 for FILE in $FILES; do
5 gcc -c $FILE.c -o $FILE.o
5 gcc -g -c $FILE.c -o $FILE.o
66 done
77
88 ar -r libtamsin.a scanner.o term.o tamsin.o
117117
118118 int term_match(struct term *pattern, struct term *ground)
119119 {
120 struct term_list *tl1, *tl2;
121
120122 if (pattern->storing != NULL) {
121123 pattern->storing = ground;
122124 return 1;
127129 if (pattern->subterms == NULL && ground->subterms == NULL) {
128130 return 1;
129131 }
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;
132147 }
742742 | blerf(a) = return zzrk.
743743 | blerf(b) = return zon.
744744 | blerf(c) = return zzt.
745 ? No 'blerf' production matched arguments [d]
745 ? No 'blerf' production matched arguments
746746
747747 Thus, we can write productions that recursively call themselves, and
748748 terminate on the base case.
00 # This example Tamsin program was written by Chris Pressey, and is
11 # hereby placed in the public domain. It comes with NO WARRANTY.
22
3 # main = blerf(world).
4
53 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.
00 #!/bin/sh
11
22 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 && \
44 ./foo
55 R=$?
66 #rm -f foo.c foo
4848
4949 scanner = scanner_new(bufterm->atom);
5050 ok = 0;
51 result = NULL;
51 result = term_new("nil");
5252
5353 program_main0();
5454
144144 for f in formals:
145145 self.emit_term(f, "pattern%s" % i, pattern=True)
146146 self.emit("if (!term_match(pattern%s, i%s)) {" % (i, i))
147 self.indent()
147148
148149 # ...
149150 next = None
158159
159160 if next:
160161 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()
163170 self.emit("}")
164171 self.emit("")
165172