Skip to content

Commit

Permalink
add free variable finder file (fv.rkt)
Browse files Browse the repository at this point in the history
  • Loading branch information
olincb committed May 12, 2022
1 parent 9e6dadf commit 8c5dce1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lang/fv.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#lang racket
(require "ast.rkt")
(provide fv)

;; Expr -> [Listof Id]
;; List all of the free variables in e
(define (fv e)
(remove-duplicates (fv* e)))

(define (fv* e)
(match e
[(Var x) (list x)]
[(Prim1 p e) (fv* e)]
[(Prim2 p e1 e2) (append (fv* e1) (fv* e2))]
[(Prim3 p e1 e2 e3) (append (fv* e1) (fv* e2) (fv* e3))]
[(If e1 e2 e3) (append (fv* e1) (fv* e2) (fv* e3))]
[(Begin e1 e2) (append (fv* e1) (fv* e2))]
[(Let x e1 e2) (append (fv* e1) (remq* (list x) (fv* e2)))]
[(App e1 es) (append (fv* e1) (append-map fv* es))]
[(Lam f xs e) (remq* xs (fv* e))]
[_ '()]))

0 comments on commit 8c5dce1

Please sign in to comment.