Functions.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.HashMap;
  10. import java.util.Map;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. /**
  14. *
  15. * @author Eugenio
  16. */
  17. public class Functions {
  18. public static Pattern TYPEANDMETHOD = Pattern.compile("\\((?<type>\\w+(\\.\\w+)+)\\)\\.(?<method>\\w+)");
  19. /*Key: Nome da funcao, Val: Tipo da funcao */
  20. protected static HashMap<String, Integer> funcoes;
  21. /*Key: Nome da funcao, Val: Tipo da funcao */
  22. protected static HashMap<String, HashMap<String, String>> params;
  23. /*Key: Nome da funcao, Val: Numero de parametros */
  24. protected static HashMap<String, Integer> numparms;
  25. /*Key: Nome da funcao, Val: Tipo de retorno */
  26. protected static HashMap<String, HashMap<String, String>> retorno;
  27. /*Key: Nome da funcao, Val: escopo */
  28. protected static HashMap<String, String> escopo;
  29. /*Key: Nome da funcao, Val: Node function */
  30. protected static HashMap<String, Node> funcs;
  31. public static String FREE = "__FREE__";
  32. public static String NEW = "__NEW__";
  33. protected static Node callTree;
  34. protected static Node currentCall;
  35. protected static HashMap<String, Integer> called;
  36. public static void _init() {
  37. System.out.println("Inicializando API.Funcao");
  38. funcoes = new HashMap<>();
  39. params = new HashMap<>();
  40. numparms = new HashMap<>();
  41. retorno = new HashMap<>();
  42. escopo = new HashMap<>();
  43. funcs = new HashMap<>();
  44. called = new HashMap<>();
  45. }
  46. public static void List() {
  47. StringBuilder sb = new StringBuilder();
  48. for (String name : funcs.keySet()) {
  49. sb.append("\t" + name + "\n");
  50. }
  51. System.out.println("Funcoes:\n" + sb.toString());
  52. }
  53. public static void Add(String id, Node func) throws Exception {
  54. if (funcs.containsKey(id)) {
  55. throw new Exception(
  56. String.format("Função ou metodo '%s' definido previamente na linha %s",
  57. id,
  58. funcs.get(id).Get("line", "")
  59. ));
  60. }
  61. func.Set("public", Base.IsPublic(func.Get("name")) ? "true" : "false");
  62. // System.out.println("Add FUNC:" + id);
  63. funcs.put(id, func);
  64. // System.out.println("Funcs:" + funcs.keySet());
  65. }
  66. public static boolean Defined(String id) throws Exception {
  67. if (!funcs.containsKey(id)) {
  68. throw new Exception(String.format("A função '%s' não foi definida. Disponiveis '%s'", id, funcs.keySet()));
  69. }
  70. return funcs.containsKey(id);
  71. }
  72. public static Node Get(String id) throws Exception {
  73. Node n;
  74. if (!Defined(id)) {
  75. // Se for metodo de uma struct busca por tipos
  76. n = DeepGet(id);
  77. } else {
  78. n = funcs.get(id);
  79. }
  80. // if (n == null) {
  81. // throw new Exception(String.format("Função ou Metodo '%s' não definido", id));
  82. // }
  83. return n;
  84. }
  85. public static Node DeepGet(String id) throws Exception {
  86. Matcher matcher = TYPEANDMETHOD.matcher(id);
  87. String type = null, method = null;
  88. while (matcher.find()) {
  89. type = matcher.group("type");
  90. method = matcher.group("method");
  91. break;
  92. }
  93. Node T = Types.Get(type);
  94. // System.out.println("DeepGet:" + type + ":" + method + ":" + T);
  95. if (T != null) {
  96. Node def;
  97. String packageName;
  98. for (Node e : T.findAll("type", "class", 1)) {
  99. type = e.Get("type");
  100. packageName = type.split("\\.")[0];
  101. // id = packageName + ".method.(" + type + ")." + method;
  102. id = "method.(" + type + ")." + method;
  103. // System.out.println("Deep:" + id);
  104. def = Get(id);
  105. if (def != null) {
  106. return def;
  107. }
  108. }
  109. }
  110. return null;
  111. }
  112. // Metodos antigos
  113. public static boolean setCurrentCall(String function) {
  114. Node n = new Node(function);
  115. boolean _continue = true;
  116. if (!called.containsKey(function)) {
  117. called.put(function, 0);
  118. } else {
  119. called.put(function, called.get(function) + 1);
  120. _continue = false;
  121. }
  122. if (currentCall != null) {
  123. currentCall.addFilho(n);
  124. }
  125. currentCall = n;
  126. return _continue;
  127. }
  128. public static void returnCallLevel() {
  129. if (currentCall.parent != null) {
  130. String longid = currentCall.getText();
  131. if (currentCall.closest(longid, "value") != null) {
  132. currentCall.Set("recursive", "t");
  133. }
  134. currentCall.Set("multi_calls", called.get(longid));
  135. currentCall = currentCall.parent;
  136. }
  137. }
  138. public static void printCallTree() {
  139. if (callTree != null) {
  140. System.out.println(callTree.stringfy());
  141. }
  142. }
  143. public static Node getCallTree() {
  144. return callTree;
  145. }
  146. public static void genCallTree(AbstractSyntaxTree ast) throws Exception {
  147. Node main = ast.getMain();
  148. String id = main.getText();
  149. setCurrentCall(id);
  150. Node parent;
  151. callTree = new Node(id);
  152. currentCall = callTree;
  153. for (Node call : main.find("stmts").findAll("call", "class")) {
  154. System.out.println("GenCallTree:" + call.getText());
  155. parent = call.parent;
  156. // genCallTree(findDefinition(parent), parent.Get("longid"));
  157. }
  158. returnCallLevel();
  159. }
  160. //
  161. // protected static void genCallTree(Node n, String longid) throws Exception {
  162. //
  163. // if (n == null) {
  164. // throw new Exception("Not found definition to " + longid + " !");
  165. // }
  166. //
  167. // Node parent;
  168. //
  169. // if (setCurrentCall(longid)) {
  170. // for (Node call : n.find("stmts").findAll(".call", "class")) {
  171. // parent = call.parent;
  172. // genCallTree(findDefinition(parent), parent.Get("longid"));
  173. // }
  174. // }
  175. //
  176. // returnCallLevel();
  177. //
  178. // }
  179. // protected static Node findDefinition(Node call) {
  180. // String longid = call.Get("longid");
  181. //
  182. // switch (call.Get("class")) {
  183. // case "function::call":
  184. // System.out.println("longid:" + longid);
  185. // return Functions.function(longid);
  186. // default:
  187. // return Methods.function(longid);
  188. // }
  189. // }
  190. /**
  191. * Adiciona uma nova função a linguagem
  192. *
  193. * @param nome da funcao
  194. * @param tipoRetorno tipo do retorno da funcao
  195. * @param paramsList liSta de parametroS com tipo
  196. * @param escopo eScopo em que função foi declarada
  197. * @param func
  198. */
  199. public static void add(String nome, HashMap<String, String> tipoRetorno, HashMap<String, String> paramsList, String escopo, Node func) {
  200. funcoes.put(nome, 1);
  201. retorno.put(nome, tipoRetorno);
  202. funcs.put(nome, func);
  203. params(nome, paramsList);
  204. escopo(nome, escopo);
  205. }
  206. /**
  207. * RegiStra uma definicao de uma função O nome deve Ser doS SeguinteS
  208. * formatoS <br>
  209. * -- funcname correSponde ao id da funcao <br>
  210. *
  211. * {funcname}() // CorreSponde a funcoeS definidaS no pacote local<br>
  212. * {package}.{funcname}() // CorreSponde a funcoeS importadaS de outroS
  213. * pacoteS<br>
  214. * {package}.{method}.{type}.{funcname}() //Corresponde a metodos de um tipo
  215. *
  216. * @param nome
  217. * @param func
  218. */
  219. public static void add(String nome, Node func) {
  220. funcs.put(nome, func);
  221. // escopo(nome, escopo);
  222. }
  223. // /**
  224. // * Adiciona uma nova função a linguagem
  225. // *
  226. // * @param nome da funcao
  227. // * @param tipoRetorno tipo do retorno da funcao
  228. // * @param escopo escopo em que função foi declarada
  229. // * @throws Exception
  230. // */
  231. // public static void add(String nome, String tipoRetorno, String escopo) {
  232. // HashMap<String, String> paramsList = new HashMap<>();
  233. // add(nome, tipoRetorno, paramsList, escopo);
  234. // numParams(nome, -1);
  235. // }
  236. public static boolean existe(String nome) {
  237. return funcoes.containsKey(nome);
  238. }
  239. public static HashMap<String, String> retorno(String nome) {
  240. return (existe(nome)) ? retorno.get(nome) : null;
  241. }
  242. protected static int numParams(String nome, int num) {
  243. if (existe(nome)) {
  244. numparms.put(nome, num);
  245. return num;
  246. }
  247. return -1;
  248. }
  249. public static int numParams(String nome) {
  250. return (existe(nome)) ? params.get(nome).size() : -1;
  251. }
  252. public static HashMap<String, String> params(String nome) {
  253. return existe(nome) ? params.get(nome) : null;
  254. }
  255. public static void params(String nome, HashMap<String, String> paramsList) {
  256. if (existe(nome)) {
  257. params.put(nome, paramsList);
  258. numParams(nome, paramsList.size());
  259. }
  260. }
  261. public static void escopo(String nome, String escpo) {
  262. if (existe(nome)) {
  263. escopo.put(nome, escpo);
  264. }
  265. }
  266. public static String escopo(String nome) {
  267. return existe(nome) ? escopo.get(nome) : "";
  268. }
  269. public static String toString(String nome) {
  270. existe(nome);
  271. String params = "";
  272. for (Map.Entry<String, String> entry : Functions.params.get(nome).entrySet()) {
  273. String varNome = entry.getKey();
  274. String tipo = entry.getValue();
  275. params += tipo + "," + varNome + ";";
  276. }
  277. return "Nome[" + funcoes.get(nome)
  278. + "]numParametros[" + numParams(nome)
  279. + "]params[" + params
  280. + "]return[" + retorno(nome) + "]";
  281. }
  282. public static Node function(String nome) {
  283. // System.out.println("Functions:" + funcs.keySet());
  284. return (existe(nome)) ? funcs.get(nome) : null;
  285. }
  286. // public static String retornoPonteiro(String nome) {
  287. // return (existe(nome))
  288. // ? (funcs.Get(nome).eq("ponteiro", "true") ? "true" : "false")
  289. // : "false";
  290. // }
  291. // public static Node GetDef(String text) {
  292. // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  293. // }
  294. }