package Compiler; import java.util.HashMap; import java.util.Map; /** * Class to record defined variables during the translation stage */ public class Environment { private final Map variableMap = new HashMap<>(); //Define a variable inside the current environment public void defineVariable(String name,Object value){ variableMap.put(name, value); } //Get a variable if it is defined, or report an error public Object getVariable(Token token){ if(variableMap.containsKey(token.text)){ return variableMap.get(token.text); } Language.displayError(token,"Undefined Variable"); throw new Error(); } //Check if a variable is defined, or report an error public Boolean checkVariable(Token token){ if(variableMap.containsKey(token.text)){ return true; } Language.displayError(token,"Undefined Variable"); throw new Error(); } }