Commit 035df101 authored by Javier Costa's avatar Javier Costa
Browse files

Scope

parent fb44257f
Loading
Loading
Loading
Loading
+42 −19
Original line number Diff line number Diff line
package tfm.utils;

import tfm.nodes.Node;
import tfm.variables.actions.VariableDeclaration;
import tfm.variables.actions.VariableDefinition;
import tfm.variables.actions.VariableUse;

import java.util.*;

public class Scope {

    private Node parent;
    private Map<String, List<VariableDeclaration>> variableDeclarations;
    private Map<String, List<VariableDefinition>> variableDefinitions;
    private Map<String, List<VariableUse>> variableUses;

    private Node node;

    public Scope(Node node) {
        this.node = node;

    public Scope(Node parent) {
        this.parent = parent;
        variableDeclarations = new HashMap<>();
        variableDefinitions = new HashMap<>();
        variableUses = new HashMap<>();
    }

    public Node getParent() {
        return parent;
    public Scope(Node node, Scope scope) {
        this.node = node;

        variableDeclarations = new HashMap<>(scope.variableDeclarations);
        variableDefinitions = new HashMap<>(scope.variableDefinitions);
        variableUses = new HashMap<>(scope.variableUses);
    }

    @Override
    public int hashCode() {
        return parent.hashCode();
    public Node getNode() {
        return node;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
    public List<VariableDeclaration> getDeclarationsOf(String variable) {
        return variableDeclarations.getOrDefault(variable, new ArrayList<>());
    }

        if (!(o instanceof Scope))
            return false;
    public List<VariableDefinition> getDefinitionsOf(String variable) {
        return variableDefinitions.getOrDefault(variable, new ArrayList<>());
    }

        Scope other = (Scope) o;
    public List<VariableUse> getUsesOf(String variable) {
        return variableUses.getOrDefault(variable, new ArrayList<>());
    }

        return this.parent.equals(other.parent);
    public Set<String> getDeclaredVariables() {
        return variableDeclarations.keySet();
    }

    @Override
    public String toString() {
        return parent.getName();
    public Optional<VariableDefinition> getLastDefinitionOf(String variable) {
        List<VariableDefinition> definitions = getDefinitionsOf(variable);

        if (definitions.isEmpty())
            return Optional.empty();

        return Optional.of(definitions.get(definitions.size() - 1));
    }
}