From 4a6161c0db78e4739433377861c0fa89b92619be Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Mon, 29 Jul 2019 14:20:47 +0100 Subject: [PATCH] Fix graph not considering original binds when recursing --- tests.scm | 18 ++++++++++++++++++ typecheck.scm | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tests.scm b/tests.scm index a198dd4..67d8b30 100644 --- a/tests.scm +++ b/tests.scm @@ -50,6 +50,24 @@ bar)) '(abs a a)) +(test-types (typecheck '(let ([foo 3] + [bar (+ foo baz)] + [baz (- bar 1)]) + bar)) + 'int) + +(test-types (typecheck '(let ([foo 3] + [bar (baz foo)] + [baz (lambda (x) x)]) + baz)) + '(abs a a)) + +(test-types (typecheck '(let ([foo 3] + [bar (baz foo)] + [baz (lambda (x) x)]) + bar)) + 'int) + (test-prog '(+ 1 2) 3) (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142) diff --git a/typecheck.scm b/typecheck.scm index 219b010..e96f694 100644 --- a/typecheck.scm +++ b/typecheck.scm @@ -339,12 +339,13 @@ ; input: a list of binds ((x . y) (y . 3)) ; returns: pair of verts, edges ((x y) . (x . y)) (define (graph bs) + (define (go bs orig-bs) (define (find-refs prog) (ast-collect (lambda (x) (case (ast-type x) ; only count a reference if its a binding - ['var (if (assoc x bs) (list x) '())] + ['var (if (assoc x orig-bs) (list x) '())] [else '()])) prog)) (if (null? bs) @@ -358,10 +359,11 @@ (rest (if (null? (cdr bs)) (cons '() '()) - (graph (cdr bs)))) + (go (cdr bs) orig-bs))) (total-verts (cons vert (car rest))) (total-edges (append edges (cdr rest)))] (cons total-verts total-edges)))) + (go bs bs)) (define (successors graph v) (define (go v E) -- 2.30.2