Types.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Node;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Set;
  12. /**
  13. *
  14. * @author Eugenio
  15. */
  16. public class Types {
  17. // public static AbstractSyntaxTree ast;
  18. public static HashMap<String, Node> _types = new HashMap<>();
  19. // Tamanho em bytes de cada tipo
  20. public static HashMap<String, Integer> _sizes = new HashMap<>();
  21. public static HashMap<String, Integer> primitiveTypes = new HashMap<>();
  22. public static HashMap<String, HashMap<String, String>> _propList = new HashMap<>();
  23. public static HashMap<String, Integer> _offsetList = new HashMap<>();
  24. public static HashMap<String, Integer> getSizes() {
  25. return _sizes;
  26. }
  27. public static void _init() throws Exception {
  28. HashMap<String, Integer> types = new HashMap<String, Integer>() {
  29. {
  30. //Numeric types
  31. // uint8 the set of all unsigned 8-bit integers (0 to 255)
  32. // uint16 the set of all unsigned 16-bit integers (0 to 65535)
  33. // uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
  34. // uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
  35. // int8 the set of all signed 8-bit integers (-128 to 127)
  36. // int16 the set of all signed 16-bit integers (-32768 to 32767)
  37. // int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
  38. // int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
  39. // float32 the set of all IEEE-754 32-bit floating-point numbers
  40. // float64 the set of all IEEE-754 64-bit floating-point numbers
  41. // complex64 the set of all complex numbers with float32 real and imaginary parts
  42. // complex128 the set of all complex numbers with float64 real and imaginary parts
  43. // byte alias for uint8
  44. // rune alias for int32
  45. put("byte", 1);
  46. put("uint8", 1);
  47. put("uint16", 2);
  48. put("uint32", 4);
  49. put("uint64", 8);
  50. put("int8", 1);
  51. put("int16", 2);
  52. put("int32", 4);
  53. put("int64", 8);
  54. put("float32", 4);
  55. put("float64", 8);
  56. put("complex64", 8);
  57. put("complex128", 16);
  58. put("bool", 1);
  59. put("char", 1);
  60. }
  61. };
  62. String type;
  63. for (Map.Entry<String, Integer> entry : types.entrySet()) {
  64. type = entry.getKey();
  65. Add(type, new Node(type)
  66. .Set("size", entry.getValue())
  67. .Set("file", "std")
  68. .Set("scope", "")
  69. .Set("primitive", "true")
  70. .Set("block", "true") // Diz que não deve editar o tipo
  71. .Set("class", "type")
  72. .Set("public", "true")
  73. );
  74. }
  75. }
  76. // Deve ser chamada no fim da leitura do listner para atuaizar os tamanhos dos tipos
  77. public static void Update() throws Exception {
  78. Node def;
  79. for (Map.Entry<String, Node> entry : _types.entrySet()) {
  80. Node type = entry.getValue();
  81. if (type.eq("block", "true")) {
  82. continue;
  83. }
  84. // Completa a definicao da struct importando os atributos herdados
  85. for (Node t : type.findAll("type", "class", 1)) {
  86. def = Get(t.Get("type"));
  87. for (Node attr : def.findAll("attr", "class")) {
  88. attr.Set("path", def.getText());
  89. type.addFilho(attr);
  90. }
  91. }
  92. UpdateSize(type);
  93. }
  94. }
  95. protected static void UpdateSize(Node type) throws Exception {
  96. // Atualiza o tamanho to tipo
  97. int size = 0;
  98. for (Node attr : type.findAll("attr", "class")) {
  99. size += Size(attr.childrens().get(0).Get("type"));
  100. }
  101. _sizes.put(type.getText(), size);
  102. }
  103. /**
  104. * Adiciona um tipo a linguagem
  105. *
  106. * @param id
  107. * @param type
  108. * @throws java.lang.Exception
  109. */
  110. public static void Add(String id, Node type) throws Exception {
  111. if (_types.containsKey(id)) {
  112. throw new Exception(String.format("Tipo '%s' previamente definido na linha %s", id, _types.get(id).Get("line", "")));
  113. }
  114. if (!type.Has("public")) {
  115. type.Set("public", Base.IsPublic(id) ? "true" : "false");
  116. }
  117. type.Set("type", id)
  118. .Set("value", id);
  119. if (type.Has("size")) {
  120. _sizes.put(id, type.getInt("size"));
  121. }
  122. _types.put(id, type);
  123. // Adiciona ao controle de tipos primitivos
  124. if (type.eq("primitive", "true")) {
  125. getPrimitiveTypes().put(type.Get("value"), 1);
  126. }
  127. }
  128. /**
  129. * Retorna verdadeiro Se o tipo exiStir
  130. *
  131. * @param type
  132. * @return true Se o tipo exiSte
  133. * @throws Exception caSo tipo não exiSta
  134. */
  135. public static Node Get(String id) throws Exception {
  136. if (!_types.containsKey(id)) {
  137. throw new Exception(String.format("Tipo '%s' não definido.", id));
  138. }
  139. return _types.get(id);
  140. }
  141. public static boolean Defined(String id) {
  142. return _types.containsKey(id);
  143. }
  144. public static void List() {
  145. System.out.println("Types:\n\t" + _sizes);
  146. }
  147. public static int Size(String type) throws Exception {
  148. if (!_sizes.containsKey(type)) {
  149. Node t = Get(type);
  150. UpdateSize(t);
  151. }
  152. // System.out.println("Size of <" + type + ">" + _sizes.);
  153. return _sizes.get(type);
  154. }
  155. public static int Shift(String type, ArrayList<String> path) throws Exception {
  156. return Shift(type, path.get(path.size() - 1));
  157. }
  158. public static int Shift(String type, String attrib) throws Exception {
  159. Node T = Get(type);
  160. String attrType;
  161. int shift = 0;
  162. // System.out.println("Shift header:" + type + ":" + attrib + ":" + T);
  163. // System.out.println("Shift:{");
  164. for (Node attr : T.findAll("attr", "class", 1)) {
  165. attrType = attr.childrens().get(0).Get("type");
  166. // System.out.println("Shift:" + attr.getText() + "[" + attrib + "]");
  167. if (!attr.getText().equals(attrib)) {
  168. shift += Size(attrType);
  169. //incrementar e buscar no proximo
  170. continue;
  171. }
  172. break;
  173. }
  174. // System.out.println("}");
  175. return shift;
  176. }
  177. public static HashMap<String, Integer> getPrimitiveTypes() {
  178. return primitiveTypes;
  179. }
  180. public static boolean Primitive(String type) {
  181. return primitiveTypes.containsKey(type);
  182. }
  183. public static String[] getPrimitiveTypesList() {
  184. Set<String> set = primitiveTypes.keySet();
  185. String[] ret = new String[set.size()];
  186. int i = 0;
  187. for (String s : set) {
  188. ret[i++] = s;
  189. }
  190. return ret;
  191. }
  192. }