From b2ea5642a283ab8a27cfaca873fd801f3e082709 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sat, 1 Mar 2014 23:54:55 -0500 Subject: [PATCH] closure conversion first commit --- closure_conversion.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 closure_conversion.js diff --git a/closure_conversion.js b/closure_conversion.js new file mode 100644 index 0000000..5dff49c --- /dev/null +++ b/closure_conversion.js @@ -0,0 +1,17 @@ +/* Takes an AST and converts all of the functions into closures. + * A closure is a pair of two things: an environment and a function + * The closure has the property that all of the free variables of the function + * are in the environment, or an exception is raised because the variable is not bound + * in the current environment. + * A free variable is simply those that are not in the list of formal parameters. + * We start with the global environment and traverse the AST. Every time a new function is entered + * the current environment gets extended with the formal parameters of the function. + * When a let is encountered the current environment also gets extended. + * The algorithm continues for any further function definitions in that branch + * otherwise it just stops for that particular branch and continues with the rest of the AST + * + * Therefore in order to call a closure one must first extract the actual function and then + * call the function with the environment associated with it. + * For the purposes of type checking it does not matter how the function gets called, the environment + * is only used for looking up the types of names. Formal parameters are given type variables. + */