Advertisement
claukiller

Untitled

Mar 23rd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. package semantic.symboltable;
  2.  
  3. import java.util.*;
  4. import ast.definition.Definition;
  5.  
  6. public class SymbolTable {
  7.  
  8. private int scope=0;
  9. private List<Map<String,Definition>> table;
  10. public SymbolTable() {
  11. // * Creates the table of contexts
  12. table=new ArrayList<Map<String,Definition>>();
  13. // * Adds the first context
  14. table.add(new HashMap<String,Definition>());
  15. }
  16.  
  17. public void set() {
  18. scope++;
  19. table.add(new HashMap<String,Definition>());
  20. }
  21.  
  22. public void reset() {
  23. table.remove(scope--);
  24. }
  25.  
  26. public boolean insert(Definition definition) {
  27. if (table.get(scope).get(definition.getName())!=null)
  28. return false;
  29. table.get(scope).put(definition.getName(), definition);
  30. definition.setScope(this.scope);
  31. return true;
  32. }
  33.  
  34. public Definition find(String id) {
  35. for (int i=scope;i>=0;i--) {
  36. Definition definition=table.get(i).get(id);
  37. if (definition!=null)
  38. return definition;
  39. }
  40. return null;
  41. }
  42.  
  43. public Definition findInCurrentScope(String id) {
  44. return table.get(scope).get(id);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement