summaryrefslogtreecommitdiffstats
path: root/src/Compiler/Environment.java
blob: 3ccf425450117f7e338bc1c411dfcc3530144a9c (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
package Compiler;
import java.util.HashMap;
import java.util.Map;

public class Environment {
    private final Map<String,Object> variableMap = new HashMap<>();

    //Define a variable inside the current environment
    //Maybe check if variable is already defined?
    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(String name){
        if(variableMap.containsKey(name)){
            return variableMap.get(name);
        }
        Language.displayError("Undefined Variable");
        throw new Error();
    }

    //Assign a value to an existing variable
    public void assignVariable(String name,Object value){
        if(variableMap.containsKey(name)){
            variableMap.put(name, value);
            return;
        }
        Language.displayError("Variable undefined");
        throw new Error();
    }
}