Api.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 IntermediaryCode.Allocation;
  8. import ast.Node;
  9. import compiler.IvannosysCompiler;
  10. import java.util.ArrayList;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. /**
  14. *
  15. * @author Eugenio
  16. */
  17. public class Api {
  18. protected static boolean inicializada = false;
  19. public static void inicializar(IvannosysCompiler compiler) throws Exception {
  20. if (inicializada) {
  21. return;
  22. }
  23. /*Inicializando importes*/
  24. /*Inicializando constantes*/
  25. // Constantes.add("FALSE", "bool", "0", "global");
  26. // Constantes.add("false", "bool", "0", "global");
  27. // Constantes.add("TRUE", "bool", "1", "global");
  28. // Constantes.add("true", "bool", "1", "global");
  29. // Constantes.add("null", "bool", "0", "global");
  30. // Constantes.add("NULL", "bool", "0", "global");
  31. // Constantes.add("__BASE_HEAP__", "int", "0", "global");
  32. // Constantes.add("__MAX_MEM__", "int", "1024", "global");
  33. /*Inicializando tipos*/
  34. Tipos._init();
  35. Tipos.Add(Tipos.INTEGER, new Node(Tipos.INTEGER)
  36. .S("type", Tipos.INTEGER)
  37. .S("size", 1)
  38. .S("file", "std")
  39. .S("scope", "")
  40. .S("primitive", "true")
  41. .S("class", "type")
  42. .S("public", "true")
  43. );
  44. Tipos.Add(Tipos.BOOLEAN, new Node(Tipos.BOOLEAN)
  45. .S("type", Tipos.BOOLEAN)
  46. .S("size", 1)
  47. .S("file", "std")
  48. .S("scope", "")
  49. .S("primitive", "true")
  50. .S("class", "type")
  51. .S("public", "true")
  52. );
  53. Tipos.Add(Tipos.CHAR, new Node(Tipos.CHAR)
  54. .S("type", Tipos.CHAR)
  55. .S("size", 1)
  56. .S("file", "std")
  57. .S("scope", "")
  58. .S("primitive", "true")
  59. .S("class", "type")
  60. .S("public", "true")
  61. );
  62. Imports._init(compiler);
  63. Constantes._init();
  64. //
  65. // Tipos.add(Tipos.UNDEFINED, 0);
  66. // Tipos.add(Tipos.VOID, 0);
  67. // Tipos.add("string", 1);
  68. // Tipos.add("thread", 1);
  69. // Tipos.add("reference", 1);
  70. Interfaces._init();
  71. /*Inicializando funcoes*/
  72. Functions._init();
  73. // HashMap<String, String> print = new HashMap<>();
  74. // Functions.add("print", "void", print, "program");
  75. //
  76. // HashMap<String, String> read = new HashMap<>();
  77. // Functions.add("read", "void", print, "program");
  78. //
  79. // HashMap<String, String> sync = new HashMap<>();
  80. // Functions.add("sync", "void", sync, "program");
  81. //
  82. // HashMap<String, String> exec = new HashMap<>();
  83. // Functions.add("exec", "void", exec, "program");
  84. //
  85. // HashMap<String, String> synexec = new HashMap<>();
  86. // Functions.add("synexec", "void", synexec, "program");
  87. /*Inicializando Traps*/
  88. // Traps._init();
  89. /*Inicializando Threads*/
  90. // Threads._init();
  91. /*Inicializando Variaveis*/
  92. Variaveis._init();
  93. /*Variavel global de acesso a structs das threads definidas no programa*/
  94. // int cod = Variaveis.add("__THREADS__", "magica", "program");
  95. // Node var = new Node("__THREADS__");
  96. // var.S("class", "var").S("tipo", "magica*").S("escopo", "program");
  97. // Variaveis.var(cod, var);
  98. inicializada = true;
  99. }
  100. /**
  101. * Retorna o numero_da_linha + ':' + numero_da_coluna + '|' nome_arquivo
  102. *
  103. * @param n
  104. * @return
  105. */
  106. public static String getFormatedPosition(Node n) {
  107. return "[" + n.G("line") + ":" + n.G("col") + "|" + n.G("file") + "\t]";
  108. }
  109. /**
  110. * Retorna numero de ocorrenciaS de um Padrão dentro de uma String
  111. *
  112. * @param pattern
  113. * @param input
  114. * @return
  115. */
  116. public static int countRegexOcorrences(String pattern, String input) {
  117. Matcher matcher = Pattern.compile(pattern).matcher(input);
  118. int count = 0;
  119. while (matcher.find()) {
  120. count++;
  121. }
  122. return count;
  123. }
  124. public static int countChatOcorrece(String input, char c) {
  125. int charCount = 0;
  126. for (int i = 0; i < input.length(); i++) {
  127. if (input.charAt(i) == c) {
  128. charCount++;
  129. }
  130. }
  131. return charCount;
  132. }
  133. /**
  134. * Retorna valor de um indice em um vetor unidirecional. <br>
  135. * var[valor] -> valor;
  136. *
  137. * @param var
  138. * @return
  139. */
  140. public static String getIndice(String var) {
  141. return var.split("\\[")[1].replace("]", "");
  142. }
  143. public static String num2bin(int num, int SizeBits) {
  144. String binario = Integer.toBinaryString(num);
  145. String paded = Utils.pad(SizeBits, binario);
  146. return Utils.cut(SizeBits, paded);
  147. }
  148. public static String num2bin(String num, int SizeBits) {
  149. int val = Integer.parseInt(num);
  150. return num2bin(val, SizeBits);
  151. }
  152. public static String reg2bin5(String reg) throws Exception {
  153. return reg2Bin(reg, 5);
  154. }
  155. public static String reg2Bin(String reg, int SizeBits) throws Exception {
  156. if (reg.equals("")) {
  157. return num2bin(0, SizeBits);
  158. }
  159. // System.out.print("Valor do reg:" + reg);
  160. int val = Allocation.reg2Int(reg);
  161. // System.out.println(":val:" + val);
  162. return num2bin(val, SizeBits);
  163. }
  164. public static String bin2Hex(String bin, int size) {
  165. // System.out.println("bin:[" + bin + "]");
  166. if (bin.equals("")) {
  167. return "falha";
  168. }
  169. return String.format("%" + size + "X", Long.parseLong(bin, 2));
  170. }
  171. public static String implode(char delimiter, ArrayList<String> elements) {
  172. String tmp = elements.toString();
  173. return tmp.substring(1, tmp.length() - 1).replace(", ", "" + delimiter);
  174. }
  175. public static boolean IsValue(String param) {
  176. if (Utils.isNumber(param)) {
  177. return true;
  178. } else if (param.toLowerCase().matches("(true|false)")) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. public static String Value(String v) {
  184. String vt = v.toLowerCase();
  185. if (vt.matches("(true|false)")) {
  186. return vt.equals("true") ? "1" : "0";
  187. } else {
  188. return v;
  189. }
  190. }
  191. public static String clearID(String id) {
  192. return id.replace("*", "").replace("&", "");
  193. }
  194. }