Api.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. Types._init();
  35. Types.Add(Types.INTEGER, new Node(Types.INTEGER)
  36. .S("type", Types.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. Types.Add(Types.BOOLEAN, new Node(Types.BOOLEAN)
  45. .S("type", Types.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. Types.Add(Types.CHAR, new Node(Types.CHAR)
  54. .S("type", Types.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. // Types.add(Types.UNDEFINED, 0);
  65. // Types.add(Types.VOID, 0);
  66. // Types.add("string", 1);
  67. // Types.add("thread", 1);
  68. // Types.add("reference", 1);
  69. Interfaces._init();
  70. /*Inicializando funcoes*/
  71. Functions._init();
  72. /*Inicializando parametros de compilacao*/
  73. BuildParams._init();
  74. // HashMap<String, String> print = new HashMap<>();
  75. // Functions.add("print", "void", print, "program");
  76. //
  77. // HashMap<String, String> read = new HashMap<>();
  78. // Functions.add("read", "void", print, "program");
  79. //
  80. // HashMap<String, String> sync = new HashMap<>();
  81. // Functions.add("sync", "void", sync, "program");
  82. //
  83. // HashMap<String, String> exec = new HashMap<>();
  84. // Functions.add("exec", "void", exec, "program");
  85. //
  86. // HashMap<String, String> synexec = new HashMap<>();
  87. // Functions.add("synexec", "void", synexec, "program");
  88. /*Inicializando Traps*/
  89. // Traps._init();
  90. /*Inicializando Threads*/
  91. // Threads._init();
  92. /*Inicializando Variaveis*/
  93. Variaveis._init();
  94. /*Variavel global de acesso a structs das threads definidas no programa*/
  95. // int cod = Variaveis.add("__THREADS__", "magica", "program");
  96. // Node var = new Node("__THREADS__");
  97. // var.S("class", "var").S("tipo", "magica*").S("escopo", "program");
  98. // Variaveis.var(cod, var);
  99. inicializada = true;
  100. }
  101. /**
  102. * Retorna o numero_da_linha + ':' + numero_da_coluna + '|' nome_arquivo
  103. *
  104. * @param n
  105. * @return
  106. */
  107. public static String getFormatedPosition(Node n) {
  108. return "[" + n.G("line") + ":" + n.G("col") + "|" + n.G("file") + "\t]";
  109. }
  110. /**
  111. * Retorna numero de ocorrenciaS de um Padrão dentro de uma String
  112. *
  113. * @param pattern
  114. * @param input
  115. * @return
  116. */
  117. public static int countRegexOcorrences(String pattern, String input) {
  118. Matcher matcher = Pattern.compile(pattern).matcher(input);
  119. int count = 0;
  120. while (matcher.find()) {
  121. count++;
  122. }
  123. return count;
  124. }
  125. public static int countChatOcorrece(String input, char c) {
  126. int charCount = 0;
  127. for (int i = 0; i < input.length(); i++) {
  128. if (input.charAt(i) == c) {
  129. charCount++;
  130. }
  131. }
  132. return charCount;
  133. }
  134. /**
  135. * Retorna valor de um indice em um vetor unidirecional. <br>
  136. * var[valor] -> valor;
  137. *
  138. * @param var
  139. * @return
  140. */
  141. public static String getIndice(String var) {
  142. return var.split("\\[")[1].replace("]", "");
  143. }
  144. public static String num2bin(int num, int SizeBits) {
  145. String binario = Integer.toBinaryString(num);
  146. String paded = Utils.pad(SizeBits, binario);
  147. return Utils.cut(SizeBits, paded);
  148. }
  149. public static String num2bin(String num, int SizeBits) {
  150. int val = Integer.parseInt(num);
  151. return num2bin(val, SizeBits);
  152. }
  153. public static String reg2bin5(String reg) throws Exception {
  154. return reg2Bin(reg, 5);
  155. }
  156. public static String reg2Bin(String reg, int SizeBits) throws Exception {
  157. if (reg.equals("")) {
  158. return num2bin(0, SizeBits);
  159. }
  160. // System.out.print("Valor do reg:" + reg);
  161. int val = Allocation.reg2Int(reg);
  162. // System.out.println(":val:" + val);
  163. return num2bin(val, SizeBits);
  164. }
  165. public static String bin2Hex(String bin, int size) {
  166. // System.out.println("bin:[" + bin + "]");
  167. if (bin.equals("")) {
  168. return "falha";
  169. }
  170. return String.format("%" + size + "X", Long.parseLong(bin, 2));
  171. }
  172. public static String implode(char delimiter, ArrayList<String> elements) {
  173. String tmp = elements.toString();
  174. return tmp.substring(1, tmp.length() - 1).replace(", ", "" + delimiter);
  175. }
  176. public static boolean IsValue(String param) {
  177. if (Utils.isNumber(param)) {
  178. return true;
  179. } else if (param.toLowerCase().matches("(true|false)")) {
  180. return true;
  181. }
  182. return false;
  183. }
  184. public static String Value(String v) {
  185. String vt = v.toLowerCase();
  186. if (vt.matches("(true|false)")) {
  187. return vt.equals("true") ? "1" : "0";
  188. } else {
  189. return v;
  190. }
  191. }
  192. public static String clearID(String id) {
  193. return id.replace("*", "").replace("&", "");
  194. }
  195. }