DataFrame.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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) throws Exception {
  44. // System.out.println("Add data to frame:" + var);
  45. int size = var.getInt("size"), vSize = 1;
  46. // boolean isArray = var.eq("array", "true");
  47. // if (isArray) {
  48. // vSize = Tipos.Size(var.G("type"));
  49. // }
  50. for (int i = 0; i < size; i++) {
  51. values.put(
  52. alias + "." + i,
  53. var.copy().S("size", vSize)
  54. );
  55. }
  56. return this;
  57. }
  58. public LinkedHashMap<String, Node> values() {
  59. return values;
  60. }
  61. public boolean has(String id) {
  62. for (Map.Entry<String, Node> x : values.entrySet()) {
  63. if (x.getKey().equals(id)) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. public static void setAbstractSyntaxTree(AbstractSyntaxTree ast) {
  70. DataFrame.ast = ast;
  71. }
  72. public int Size() {
  73. int val = 0;
  74. for (Map.Entry<String, Node> v : values.entrySet()) {
  75. try {
  76. val += v.getValue().getInt("size");
  77. } catch (Exception ex) {
  78. Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
  79. }
  80. }
  81. return val;
  82. }
  83. @Override
  84. public String toString() {
  85. StringBuilder s = new StringBuilder("Data Frame <" + name + ">:" + Size() + "\n");
  86. Node value;
  87. for (Map.Entry<String, Node> x : values.entrySet()) {
  88. value = x.getValue();
  89. s.append(x.getKey())
  90. .append(": ")
  91. .append(value.G("name"))
  92. .append("\tsize:")
  93. .append(value.G("size"))
  94. .append("\n");
  95. }
  96. return s.toString();
  97. }
  98. /**
  99. * Formata oS haSh para a impreSSão no metodatao toString
  100. *
  101. * @param hash
  102. * @return
  103. */
  104. protected String hashToString(HashMap<String, Node> hash) {
  105. String r = "";
  106. for (Map.Entry<String, Node> entry : hash.entrySet()) {
  107. String name = entry.getKey();
  108. Node value = entry.getValue();
  109. // int type = this.type.G(name);
  110. r += "{" + name + ":" + value + "},";
  111. }
  112. return r;
  113. }
  114. public int Offset(String id) {
  115. if (id.contains("_G") && data != null) {
  116. return data.Offset(id);
  117. }
  118. int offset = 0;
  119. try {
  120. // String cp = id;
  121. id = normalize(id);
  122. for (Map.Entry<String, Node> x : values.entrySet()) {
  123. // System.out.println("Offset:(" + id + ")" + x.getKey());
  124. if (!x.getKey().equals(id)) {
  125. offset += x.getValue().getInt("size");
  126. continue;
  127. }
  128. break;
  129. }
  130. } catch (Exception ex) {
  131. Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
  132. }
  133. return offset;
  134. }
  135. protected Pattern normalId = Pattern.compile("(?<id>[\\w_]+)(\\[(?<index>\\w+)\\])?");
  136. protected String normalize(String id) {
  137. String shift = "0", index;
  138. Matcher m = normalId.matcher(id);
  139. if (m.find()) {
  140. id = m.group("id");
  141. index = m.group("index");
  142. if (Utils.isNumber(index)) {
  143. shift = index;
  144. }
  145. }
  146. if (!id.contains(".")) {
  147. id += "." + shift;
  148. }
  149. // System.out.println("Normalize:" + id + "::" + or);
  150. return id;
  151. }
  152. public DataFrame copy(DataFrame data) {
  153. for (Map.Entry<String, Node> x : data.values().entrySet()) {
  154. // System.out.println("Copy:" + x.getKey() + "-" + x.getValue());
  155. this.values.put(x.getKey(), x.getValue());
  156. }
  157. return this;
  158. }
  159. void Replace(String dst, String p1) {
  160. String rest;
  161. // System.out.println("Replace >> " + this.values);
  162. LinkedHashMap<String, Node> add = new LinkedHashMap<>();
  163. for (Map.Entry<String, Node> x : this.values.entrySet()) {
  164. rest = x.getKey().replace(dst, "");
  165. if (rest.equals("") || rest.substring(0, 1).equals(".")) {
  166. add.put(p1 + rest, x.getValue());
  167. } else {
  168. add.put(x.getKey(), x.getValue());
  169. }
  170. }
  171. this.values = add;
  172. // System.out.println("Replace << " + this.values);
  173. }
  174. }