AbstractSyntaxTree.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 ast;
  7. import API.Utils;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Stack;
  11. /**
  12. * @author Eugenio
  13. */
  14. public final class AbstractSyntaxTree {
  15. protected int count;
  16. protected Node current;
  17. protected Node root;
  18. protected Node compileParams;
  19. protected HashMap<String, ArrayList<Node>> ganchos;
  20. protected Stack<Node> ctxs = new Stack<>();
  21. protected static AbstractSyntaxTree instance;
  22. public AbstractSyntaxTree() {
  23. this.root = new Node("init");
  24. Current(root);
  25. compileParams = new Node("");
  26. // this.current = this.root;
  27. // ganchos = new HashMap<>();
  28. // ganchos.put("constantes", new ArrayList<>());
  29. // ganchos.put("variaveis", new ArrayList<>());
  30. // ganchos.put("funcoes", new ArrayList<>());
  31. // ganchos.put("threads", new ArrayList<>());
  32. // ganchos.put("traps", new ArrayList<>());
  33. }
  34. public Node getCompileParams() {
  35. return compileParams;
  36. }
  37. public void setCompileParams(Node compileParams) {
  38. this.compileParams = compileParams;
  39. }
  40. public static AbstractSyntaxTree getAbstractSyntaxTree() {
  41. if (instance == null) {
  42. instance = new AbstractSyntaxTree();
  43. }
  44. return instance;
  45. }
  46. public Node setNode(Node n) {
  47. addChildren(n);
  48. this.current = n;
  49. return this.current;
  50. }
  51. public Node Current(Node n) {
  52. ctxs.push(this.current);
  53. this.current = n;
  54. return this.current;
  55. }
  56. public Node Back() {
  57. this.current = ctxs.pop();
  58. return this.current;
  59. }
  60. public Node getTipoDefinicao(String tipo) {
  61. Node types = this.root.find("program").find("types");
  62. Node t = null;
  63. if (types != null) {
  64. t = types.find(tipo);
  65. if (t != null) {
  66. return t;
  67. }
  68. ArrayList<Node> librarys = this.root.encontrePrimeiroGrau("library", "class");
  69. for (Node library : librarys) {
  70. types = library.find("types");
  71. if (types == null) {
  72. continue;
  73. }
  74. t = types.find(tipo);
  75. if (t != null) {
  76. return t;
  77. }
  78. }
  79. }
  80. return t;
  81. }
  82. public Node addChildren(Node n) {
  83. return this.current.addFilho(n);
  84. }
  85. public Node setParentToCurrent() {
  86. if (this.current.parent != null) {
  87. this.current = this.current.parent;
  88. }
  89. return this.current;
  90. }
  91. public String visit() {
  92. return root.visit();
  93. }
  94. public String stringfy() {
  95. return root.stringfy();
  96. }
  97. public void setAtributo(String atrib, String valor) {
  98. this.current.Set(atrib, valor);
  99. }
  100. public String getAtributo(String atrib) {
  101. return this.current.Get(atrib);
  102. }
  103. public Node getCurrent() {
  104. return this.current;
  105. }
  106. protected Node encontreFuncao(String nome) {
  107. // System.out.println("this.root" + this.root);
  108. Node ret = null;
  109. for (Node node : this.root.childrens()) {
  110. Node funcoes = node.find("funcoes");
  111. if (funcoes == null) {
  112. continue;
  113. }
  114. for (Node func : funcoes.childrens()) {
  115. if (!func.getText().equals(nome)) {
  116. continue;
  117. }
  118. ret = func;
  119. }
  120. }
  121. return ret;
  122. }
  123. public Node getMain() {
  124. return this.root.find("main").find("main");
  125. }
  126. protected Node encontrarDeclaracaoNaFuncao(Node var, Node parent) {
  127. Node funcao = parent.closest("dec::function", "class");
  128. Node parametros = funcao.find("parametros");
  129. String nome = Utils.clearName(var.getText());
  130. if (parametros != null) {
  131. for (Node p : parametros.childrens()) {
  132. if (p.getText().equals(nome)) {
  133. return p;
  134. }
  135. }
  136. }
  137. return null;
  138. // return encontreDeclaracao(var, parent.parent);
  139. }
  140. protected Node encontrarDeclaracaoNoFor(Node var, Node parent) {
  141. Node _for = parent.closest("for", "class");
  142. String nome = Utils.clearName(var.getText());
  143. if (_for != null) {
  144. for (Node ini : _for.find("inicializacoes").childrens()) {
  145. // System.out.println("CLASS:" + ini.Class());
  146. if (ini.getText().equals(nome) && ini.eq("class", "dec::var")) {
  147. return ini;
  148. }
  149. }
  150. }
  151. // System.out.println("Procurando no for e nao encontrei agora vou buscar[" + var.getText() + "] em " + parent.parent);
  152. Node val = encontreDeclaracao(var, parent.parent);
  153. // System.out.println("Retorno da busca no parent:" + val);
  154. return val;
  155. }
  156. public Node encontreDeclaracao(Node n, Node parent) {
  157. // System.out.println("Node:" + n + "\n|parent:" + parent);
  158. String nodeClass = n.Class();
  159. /*Se for a declaracao da variavel*/
  160. if (nodeClass.equals("dec::var")) {
  161. return n;
  162. }
  163. /*Se nao for uma variavel, atribuicao ou operacao unaria return null*/
  164. if (!n.in("class", new String[]{"var", "assign::op", "op::unaria", "atributo"})) {
  165. return null;
  166. }
  167. String nome = Utils.clearName(n.getText());
  168. // System.out.println("Nome1:" + nome);
  169. if (nome.contains(".")) {
  170. String[] partes = nome.split("\\.");
  171. nome = partes[0];
  172. }
  173. // System.out.println("Nome2:" + nome);
  174. String local = "declaracao";
  175. if (n.eq("declaracao", "library")) {
  176. local = "escopo";
  177. }
  178. String[] escopo = n.Get(local).split("\\.");
  179. Node decNode = null;
  180. if (parent.eq("class", "dec::function") && escopo[0].equals("function")) {
  181. decNode = encontrarDeclaracaoNaFuncao(n, parent);
  182. } else if (parent.eq("class", "for")) {
  183. decNode = encontrarDeclaracaoNoFor(n, parent);
  184. } else if (parent.eq("class", "bloco")) {
  185. /*procurar no bloco*/
  186. ArrayList<Node> declaracoes = parent.filtrarFilhosPorClass("dec::var");
  187. for (Node node : declaracoes) {
  188. if (Utils.clearName(node.getText()).equals(nome)) {
  189. decNode = node;
  190. break;
  191. }
  192. }
  193. }
  194. if (decNode != null) {
  195. return decNode;
  196. } else if (parent.parent != null
  197. && !parent.parent.eq("value", "init")) {
  198. // System.out.println("Buscando no parent:" + n.getText() + ":" + parent.parent.Get("id"));
  199. return encontreDeclaracao(n, parent.parent);
  200. } else {
  201. // System.out.println("parent.parent:" + parent.parent);
  202. Node programa = parent.parent.find("program");
  203. Node var = _buscar(programa, nome, "variaveis");
  204. if (var == null) {
  205. ArrayList<Node> librarys = parent.parent.filtrarFilhosPorClass("library");
  206. // System.out.println("librarys:" + librarys);
  207. for (Node library : librarys) {
  208. var = _buscar(library, nome, "variaveis");
  209. if (var != null) {
  210. // System.out.println("Encontrei mat");
  211. break;
  212. }
  213. }
  214. }
  215. return var;
  216. }
  217. }
  218. protected Node _buscar(Node root, String nome, String quem) {
  219. Node n = root.find(quem);
  220. // System.out.println("Buscando [" + nome + "]" + n);
  221. if (n != null) {
  222. ArrayList<Node> variaveis = n.childrens();
  223. for (Node node : variaveis) {
  224. if (Utils.clearName(node.getText()).equals(nome)) {
  225. return node;
  226. }
  227. }
  228. }
  229. return null;
  230. }
  231. public Node getVariaveis() {
  232. return root.find("program").find("variaveis");
  233. }
  234. public Node getRoot() {
  235. return root;
  236. }
  237. public ArrayList<Node> getVariaveisGlobais() {
  238. return this.getRoot().findAll("dec::globais", "class");
  239. }
  240. public Node getPackage(String packageName) {
  241. // System.out.println("Getpackage:" + packageName + getRoot());
  242. return getRoot().find(packageName);
  243. }
  244. public Node BuildParams() {
  245. return this.compileParams;
  246. }
  247. }