Recall the main function in interpreter.js:

1.  function evalExp(exp,envir) {
2.     if (SLang.absyn.isIntExp(exp)) {
3.         return SLang.env.createNum(SLang.absyn.getIntExpValue(exp));
4.     } else if (SLang.absyn.isVarExp(exp)) {
5.         return SLang.env.lookup(envir,SLang.absyn.getVarExpId(exp));
6.     } else if (SLang.absyn.isFnExp(exp)) {
7.         return SLang.env.createClo(
                            SLang.absyn.getFnExpParams(exp),
                            SLang.absyn.getFnExpBody(exp),
                            envir);
8.     } else if (SLang.absyn.isAppExp(exp)) {
9.         var f = evalExp(SLang.absyn.getAppExpFn(exp),envir);
10.        var args = SLang.absyn.getAppExpArgs(exp).map( 
                        function(arg) { return evalExp(arg,envir); } );
11.        if (SLang.env.isClo(f)) {
12.            return evalExp(SLang.env.getCloBody(f),
                           SLang.env.update(
                                 SLang.env.getCloEnv(f),
                                 SLang.env.getCloParams(f),args));
13.        } else {
14.            throw new Error( f +
                    " is not a closure and thus cannot be applied.");
15.        }
16.     } else if (SLang.absyn.isPrimAppExp(exp)) {
17.         return applyPrimitive(SLang.absyn.getPrimAppExpPrim(exp),
                              SLang.absyn.getPrimAppExpArgs(exp)
                                 .map( function(arg) { 
                                       return evalExp(arg,envir); } ));
18.    } else {
19.        throw "Error: Attempting to evaluate an invalid expression";
20.    }
21. }

Note that, when a statement above takes up more than one line, only its first line is numbered. Among the options listed below, which one is the largest set of lines that get executed when the following SLang 1 program is interpreted?

          (fn (u,v) => fn (f) => 3 x +(x,y)) 

Lines 5, 7, 12, 17
  • Line 12
  • Lines 7, 12
  • Lines 3, 7, 12
  • Lines 3, 5, 7, 12
  • Lines 3, 5, 7, 17
  • Lines 3, 5, 7, 12, 17

Note that each one of the lines in the sets above is a return statement for one of the if branches.

It may be helpful to build the AST for this program and then traverse this tree while tracing the interpreter.

How many functions are defined in this program?

How many function applications does this program contain?