DataFrame.java 6.8 KB

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