Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(8 pt) Environment] Extend the interpreter to support global constants and understand the idea of initial environment in programming languages. Extend the VarLang interpreter such

image text in transcribedimage text in transcribedimage text in transcribed

(8 pt) 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 VarErp 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) 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); } (8 pt) 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 VarErp 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) 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Evaluate the indefinite integral. SrV?? + I dx -2

Answered: 1 week ago