Constantes.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package API;
  7. import ast.AbstractSyntaxTree;
  8. import ast.Node;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. /**
  12. *
  13. * @author Eugenio
  14. */
  15. public class Constantes {
  16. public static AbstractSyntaxTree ast;
  17. public static HashMap<String, Node> consts;
  18. public static HashMap<String, String> types;
  19. public static HashMap<String, ArrayList<String>> scopes;
  20. private static String currentKey;
  21. public static void _init() {
  22. if (consts != null) {
  23. return;
  24. }
  25. types = new HashMap<>();
  26. consts = new HashMap<>();
  27. scopes = new HashMap<>();
  28. }
  29. public static void Add(String id, Node constant) {
  30. constant.Set("public", Base.IsPublic(id) ? "true" : "false");
  31. consts.put(id, constant);
  32. }
  33. public static Node Get(String id) {
  34. return consts.get(id);
  35. }
  36. public static boolean Defined(String id) {
  37. return consts.containsKey(id);
  38. }
  39. public static void List() {
  40. System.out.println("Constants:\n" + consts.keySet());
  41. }
  42. /**
  43. * Metodos velhos
  44. */
  45. // /**
  46. // * Adiciona uma constante
  47. // *
  48. // * @param nome nome da constante
  49. // * @param type
  50. // * @param valor valor da constante
  51. // * @param scope
  52. // */
  53. // public static void add(String nome, String type, String valor, String scope) {
  54. // String key = nome + ":" + scope;
  55. // consts.put(key, valor);
  56. // types.put(key, type);
  57. // }
  58. //
  59. // /**
  60. // * Retorna verdadeiro se o constante existir
  61. // *
  62. // * @param name nome da constante
  63. // * @param scope local da definicao da constante
  64. // * @return true se o tipo existe
  65. // */
  66. // public static boolean exist(String name, String scope) {
  67. // if (name.contains(".")) {
  68. // return false;
  69. // }
  70. //
  71. // boolean end = scope.equals("global");
  72. // currentKey = name + ":" + scope;
  73. //
  74. // if (consts.containsKey(currentKey)) {
  75. // return true;
  76. // }
  77. //
  78. // String[] path = scope.split(",", 2);
  79. //
  80. // return end ? false : exist(name, path[path.length - 1]);
  81. // }
  82. //
  83. // /**
  84. // * Retorna o tipo da constante
  85. // *
  86. // * @param name nome da constante
  87. // * @return tipo da constante
  88. // * @throws Exception caso não encontre a constante
  89. // */
  90. // public static String type(String name, String scope) {
  91. // if (exist(name, scope)) {
  92. // return types.get(currentKey);
  93. // }
  94. // return "undefined";
  95. // }
  96. //
  97. // /**
  98. // * Retorna o valor de uma constante na forma de string
  99. // *
  100. // * @param text
  101. // * @param scope
  102. // * @return
  103. // */
  104. // public static String val(String text, String scope) {
  105. // if (exist(text, scope)) {
  106. // return consts.get(currentKey);
  107. // }
  108. // return "undefined";
  109. // }
  110. //
  111. // public static String val(Node var) {
  112. // return val(var.getText(), var.get("scope"));
  113. // }
  114. //
  115. // public static int intVal(Node var) throws Exception {
  116. // String v = val(var);
  117. // if (UtilsCompiler.isNumber(v)) {
  118. // return UtilsCompiler.parseInt(v);
  119. // }
  120. // throw new Exception("O valor " + v + " não é um numero!");
  121. // }
  122. }