git @ Cat's Eye Technologies Tamsin / ea4a093
Pattern matching works. For whatever it's worth. I don't know what it's worth. Cat's Eye Technologies 11 years ago
2 changed file(s) with 37 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
371371 | blerf(b) = return zon.
372372 | blerf(c) = return zzt.
373373 = zzt
374
375 Thus, we can write productions that recursively call themselves, and
376 terminate on the base case.
377
378 | main = blerf(tree(tree(tree(a,b),c),d)).
379 | blerf(tree(L,R)) = blerf(L).
380 | blerf(Other) = return Other.
381 = a
382
383 What does this get us? I DON'T KNOW. Oh, heck. Let's parse a tree.
384
385 | main = tree → T & rightmost(T).
386 | tree = "tree" & "(" & tree → L & "," & tree → R & ")" & return tree(L, R)
387 | | "0" | "1" | "2".
388 | rightmost(tree(L,R)) = rightmost(R).
389 | rightmost(X) = return X.
390 + tree(tree(0,1),tree(0,tree(1,2)))
391 = 2
322322 ### term matching
323323
324324 def match_all(self, patterns, values):
325 """Returns a dict of bindings if all values match all patterns,
326 or False if there was a mismatch.
327
328 """
325329 i = 0
326330 bindings = {}
327331 while i < len(patterns):
328 sub_bindings = self.match_terms(patterns[i], values[i])
329 bindings.update(sub_bindings)
332 sub = self.match_terms(patterns[i], values[i])
333 if sub == False:
334 return False
335 bindings.update(sub)
330336 i += 1
331337 return bindings
332338
333339 def match_terms(self, pattern, value):
340 """Returns a dict of bindings if the values matches the pattern,
341 or False if there was a mismatch.
342
343 """
334344 if isinstance(pattern, Variable):
345 # TODO: check existing binding! oh well, assume unique for now.
335346 return {pattern.name: value}
336347 elif isinstance(pattern, Term):
337348 i = 0
349 if pattern.name != value.name:
350 return False
338351 bindings = {}
339352 while i < len(pattern.contents):
340353 b = self.match_terms(pattern.contents[i], value.contents[i])
359372 return x
360373 elif ast[0] == 'CALL':
361374 prods = self.find_productions(ast[1])
375 debug("candidate productions: %r" % prods)
362376 args = [x.expand(self) for x in ast[2]]
363377 for prod in prods:
364378 formals = prod[2]
379 debug("formals: %r, args: %r" % (formals, args))
365380 if not formals:
366381 return self.interpret(prod)
367382 bindings = self.match_all(formals, args)
368 if bindings:
383 debug("bindings: %r" % bindings)
384 if bindings != False:
369385 return self.interpret(prod, bindings=bindings)
370386 elif ast[0] == 'SEND':
371387 result = self.interpret(ast[1])