X-Git-Url: https://git.lukelau.me/?p=scheme.git;a=blobdiff_plain;f=ast.scm;h=2beb94507572eafd20b586d2ce9ef3867ec63d3e;hp=a38a01257aea179e4638d376400fcc77e1c9318e;hb=8b8c603a6106151188bdeab95501cacaf72912d4;hpb=d486b87b4fb6311cd627887fe3da67a8f8d4cbb4 diff --git a/ast.scm b/ast.scm index a38a012..2beb945 100644 --- a/ast.scm +++ b/ast.scm @@ -48,6 +48,28 @@ (fold-map inner (cdr x)))] [else (f x)])) +(define (ast-find p x) + (define (inner y) (ast-find p y)) + (define (any p x) (fold-left + (lambda (acc y) (if acc #t (p y))) + #f + x)) + (define (either . fs) + (if (null? fs) #f + (if (car fs) (car fs) + (apply either (cdr fs))))) + + (case (ast-type x) + ['let (either (p x) + (any inner (let-bindings x)) + (any inner (let-body x)))] + ['app (either (p x) + (any inner x))] + ['lambda (either (p x) + (inner (lambda-body x)))] + ['if (either (p x) (any inner (cdr x)))] + [else (p x)])) + (define let-bindings cadr) (define let-body cddr)