Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package semantic.symboltable;
- import java.util.*;
- import ast.definition.Definition;
- public class SymbolTable {
- private int scope=0;
- private List<Map<String,Definition>> table;
- public SymbolTable() {
- // * Creates the table of contexts
- table=new ArrayList<Map<String,Definition>>();
- // * Adds the first context
- table.add(new HashMap<String,Definition>());
- }
- public void set() {
- scope++;
- table.add(new HashMap<String,Definition>());
- }
- public void reset() {
- table.remove(scope--);
- }
- public boolean insert(Definition definition) {
- if (table.get(scope).get(definition.getName())!=null)
- return false;
- table.get(scope).put(definition.getName(), definition);
- definition.setScope(this.scope);
- return true;
- }
- public Definition find(String id) {
- for (int i=scope;i>=0;i--) {
- Definition definition=table.get(i).get(id);
- if (definition!=null)
- return definition;
- }
- return null;
- }
- public Definition findInCurrentScope(String id) {
- return table.get(scope).get(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement