DataFrame.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 IntermediaryCode;
  7. import API.Utils;
  8. import ast.AbstractSyntaxTree;
  9. import ast.Node;
  10. import java.util.HashMap;
  11. import java.util.LinkedHashMap;
  12. import java.util.Map;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. /**
  18. *
  19. * @author Eugenio
  20. */
  21. public class DataFrame {
  22. protected String name;
  23. protected LinkedHashMap<String, Node> values = new LinkedHashMap<>();//Map< String, String> copy = new TreeMap<>();
  24. // protected LinkedList<Node> values = new LinkedList<>();//Map< String, String> copy = new TreeMap<>();
  25. // protected HashMap<String, Integer> size = new LinkedHashMap<>();
  26. // protected HashMap<String, Integer> type = new LinkedHashMap<>();
  27. // protected HashMap<String, Integer> _offset_ = new LinkedHashMap<>();
  28. protected boolean initialized = false;
  29. protected static AbstractSyntaxTree ast;
  30. public static int TYPE_ARGUMENT = 0;
  31. public static int TYPE_VAR = 1;
  32. public static int TYPE_RETURN = 2;
  33. public static int TYPE_STACK = 3;
  34. public int offsetctrl = 0;
  35. protected DataFrame data;
  36. public DataFrame(String name, DataFrame data) {
  37. this.name = name;
  38. this.data = data;
  39. }
  40. public void setInicializar(boolean init) {
  41. initialized = init;
  42. }
  43. public DataFrame Add(String alias, Node var, int elements) {
  44. // System.out.println("Add data to frame:" + var);
  45. // int vSize = 1;
  46. // int size = var.getInt("size"), vSize = 1;
  47. // boolean isArray = var.eq("array", "true");
  48. // if (isArray) {
  49. // vSize = Tipos.Size(var.G("type"));
  50. // }
  51. for (int i = 0; i < elements; i++) {
  52. values.put(
  53. alias + "." + i,
  54. var.copy()
  55. );
  56. // var.copy().S("size", vSize)
  57. }
  58. return this;
  59. }
  60. // public DataFrame Add(String alias, Node var) throws Exception {
  61. //// System.out.println("Add data to frame:" + var);
  62. // int size = var.getInt("size"), vSize = 1;
  63. //// boolean isArray = var.eq("array", "true");
  64. //
  65. //// if (isArray) {
  66. //// vSize = Tipos.Size(var.G("type"));
  67. //// }
  68. // for (int i = 0; i < size; i++) {
  69. // values.put(
  70. // alias + "." + i,
  71. // var.copy().S("size", vSize)
  72. // );
  73. // }
  74. //
  75. // return this;
  76. // }
  77. public LinkedHashMap<String, Node> values() {
  78. return values;
  79. }
  80. public boolean has(String id) {
  81. for (Map.Entry<String, Node> x : values.entrySet()) {
  82. if (x.getKey().equals(id)) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. public static void setAbstractSyntaxTree(AbstractSyntaxTree ast) {
  89. DataFrame.ast = ast;
  90. }
  91. public int Size() {
  92. int val = 0;
  93. for (Map.Entry<String, Node> v : values.entrySet()) {
  94. try {
  95. val += v.getValue().getInt("size");
  96. } catch (Exception ex) {
  97. Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99. }
  100. return val;
  101. }
  102. @Override
  103. public String toString() {
  104. StringBuilder s = new StringBuilder("Data Frame <" + name + ">:" + Size() + "\n");
  105. Node value;
  106. for (Map.Entry<String, Node> x : values.entrySet()) {
  107. value = x.getValue();
  108. // System.out.println("Value:" + value);
  109. s.append(x.getKey())
  110. .append(": ")
  111. .append(value.G("name"))
  112. .append("\tsize:")
  113. .append(value.G("size"))
  114. .append("\tvalue:")
  115. .append(value.G("var.value"))
  116. .append("\n");
  117. // break;
  118. }
  119. return s.toString();
  120. }
  121. /**
  122. * Formata oS haSh para a impreSSão no metodatao toString
  123. *
  124. * @param hash
  125. * @return
  126. */
  127. protected String hashToString(HashMap<String, Node> hash) {
  128. String r = "";
  129. for (Map.Entry<String, Node> entry : hash.entrySet()) {
  130. String name = entry.getKey();
  131. Node value = entry.getValue();
  132. // int type = this.type.G(name);
  133. r += "{" + name + ":" + value + "},";
  134. }
  135. return r;
  136. }
  137. public int Offset(String id) {
  138. if (id.contains("_G") && data != null) {
  139. return data.Offset(id);
  140. }
  141. int offset = 0;
  142. try {
  143. // String cp = id;
  144. id = normalize(id);
  145. for (Map.Entry<String, Node> x : values.entrySet()) {
  146. // System.out.println("Offset:(" + id + ")" + x.getKey());
  147. if (!x.getKey().equals(id)) {
  148. offset += x.getValue().getInt("size");
  149. continue;
  150. }
  151. break;
  152. }
  153. } catch (Exception ex) {
  154. Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
  155. }
  156. return offset;
  157. }
  158. protected Pattern normalId = Pattern.compile("(?<id>[\\w_]+)(\\[(?<index>\\w+)\\])?");
  159. protected String normalize(String id) {
  160. String shift = "0", index;
  161. Matcher m = normalId.matcher(id);
  162. if (m.find()) {
  163. id = m.group("id");
  164. index = m.group("index");
  165. if (Utils.isNumber(index)) {
  166. shift = index;
  167. }
  168. }
  169. if (!id.contains(".")) {
  170. id += "." + shift;
  171. }
  172. // System.out.println("Normalize:" + id + "::" + or);
  173. return id;
  174. }
  175. public DataFrame copy(DataFrame data) {
  176. for (Map.Entry<String, Node> x : data.values().entrySet()) {
  177. // System.out.println("Copy:" + x.getKey() + "-" + x.getValue());
  178. this.values.put(x.getKey(), x.getValue());
  179. }
  180. return this;
  181. }
  182. void Replace(String dst, String p1) {
  183. String rest;
  184. // System.out.println("Replace >> " + this.values);
  185. LinkedHashMap<String, Node> add = new LinkedHashMap<>();
  186. for (Map.Entry<String, Node> x : this.values.entrySet()) {
  187. rest = x.getKey().replace(dst, "");
  188. if (rest.equals("") || rest.substring(0, 1).equals(".")) {
  189. add.put(p1 + rest, x.getValue());
  190. } else {
  191. add.put(x.getKey(), x.getValue());
  192. }
  193. }
  194. this.values = add;
  195. // System.out.println("Replace << " + this.values);
  196. }
  197. }