summaryrefslogtreecommitdiffstats
path: root/src/Compiler/Environment.java
blob: 8ae8724148bf95248f6eeaa399db1f2deb20e6d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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<String,Object> 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();
    }
}