Functions.java 10 KB

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