Question: [Environment] Extend the interpreter to support global constants and understand the idea of initial environment in programming languages. Extend the VarLang interpreter such that: (1)

[Environment] Extend the interpreter to support global constants and understand the idea of initial environment in programming languages. Extend the VarLang interpreter such that:

(1) you can initialize the environment using define VarExp Number, and

(2) any VarLang programs that use such predefined constants can be evaluated accordingly.

$(define month 9)

$(let ((x 1)) (+ x month))10

$(define F 96454.56)(define R 10973731.6)

[Environment] Extend the interpreter to support global constants and understand the ideaof initial environment in programming languages. Extend the VarLang interpreter such that:

A5 A * This main class implements the Read-Eval-Print-Loop of the interpreter with * the help of Reader, Evaluator, and Printer classes. * * @author hridesh public class Interpreter { public static void main(String[] args) { System.out.println("Type a program to evaluate and press the enter key," + e.g. (let ((a 3) (b 100) (C 84) (d 279) (e 277)) (+ (* a b) (I c (- de)))) " + "Press Ctrl + C to exit."); Reader reader = new Reader(); Evaluator eval = new Evaluator(); Printer printer = new Printer(); REPL: while (true) { // Read-Eval-Print-Loop (also known as REPL) Program p = null; try { - p = reader.read(); if(p._e == null) continue REPL; Value val = eval. valueof(p); printer.print(val); } catch (Env.LookupException e) { printer.print(e); } catch (IOException e) { System.out.println("Error reading input:" + e.getMessage()); } catch (NullPointerException e) { System.out.println("Error:" + e.getMessage()); } } * Representation of an environment, which maps variables to values. * * @author hridesh public interface Env { Value get (String search_var); /serial/ static public class LookupException extends RuntimeException { LookupException(String message) { super(message); } } static public class EmptyEny implements Env{ public Value get (String search_var) { throw new LookupException("No binding found for name: " + search_var); } } - static public class ExtendEny implements Env { private Env_saved_env; private String_var; private Value val; public ExtendEnv(Env saved_env, String var, Value val){ _saved_env = saved_env; _var = var; _val = val; } public Value get (String search_var) { if (search_var.equals(_var)) return _val; return _saved_env.get(search_var); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!