DataFrame.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 ast.AbstractSyntaxTree;
  8. import ast.Node;
  9. import java.util.HashMap;
  10. import java.util.LinkedHashMap;
  11. import java.util.Map;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. /**
  15. *
  16. * @author Eugenio
  17. */
  18. public class DataFrame {
  19. protected String name;
  20. protected LinkedHashMap<String, Node> values = new LinkedHashMap<>();//Map< String, String> copy = new TreeMap<>();
  21. // protected LinkedList<Node> values = new LinkedList<>();//Map< String, String> copy = new TreeMap<>();
  22. // protected HashMap<String, Integer> size = new LinkedHashMap<>();
  23. // protected HashMap<String, Integer> type = new LinkedHashMap<>();
  24. // protected HashMap<String, Integer> _offset_ = new LinkedHashMap<>();
  25. protected boolean initialized = false;
  26. protected static AbstractSyntaxTree ast;
  27. public static int TYPE_ARGUMENT = 0;
  28. public static int TYPE_VAR = 1;
  29. public static int TYPE_RETURN = 2;
  30. public static int TYPE_STACK = 3;
  31. public int offsetctrl = 0;
  32. protected DataFrame data;
  33. public DataFrame(String name, DataFrame data) {
  34. this.name = name;
  35. this.data = data;
  36. }
  37. public void setInicializar(boolean init) {
  38. initialized = init;
  39. }
  40. public DataFrame Add(String alias, Node var) {
  41. int size = Integer.parseInt(var.G("size"));
  42. for (int i = 0; i < size; i++) {
  43. values.put(alias + "." + i, var);
  44. }
  45. return this;
  46. }
  47. public LinkedHashMap<String, Node> values() {
  48. return values;
  49. }
  50. public boolean has(String id) {
  51. for (Map.Entry<String, Node> x : values.entrySet()) {
  52. if (x.getKey().equals(id)) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public static void setAbstractSyntaxTree(AbstractSyntaxTree ast) {
  59. DataFrame.ast = ast;
  60. }
  61. public int Size() {
  62. int val = 0;
  63. for (Map.Entry<String, Node> v : values.entrySet()) {
  64. try {
  65. val += v.getValue().getInt("size");
  66. } catch (Exception ex) {
  67. Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
  68. }
  69. }
  70. return val;
  71. }
  72. @Override
  73. public String toString() {
  74. StringBuilder s = new StringBuilder("Data Frame <" + name + ">:" + Size() + "\n");
  75. for (Map.Entry<String, Node> x : values.entrySet()) {
  76. s.append(x.getKey()).append(": ").append(x.getValue().G("name")).append("\n");
  77. }
  78. return s.toString();
  79. }
  80. /**
  81. * Formata oS haSh para a impreSSão no metodatao toString
  82. *
  83. * @param hash
  84. * @return
  85. */
  86. protected String hashToString(HashMap<String, Node> hash) {
  87. String r = "";
  88. for (Map.Entry<String, Node> entry : hash.entrySet()) {
  89. String name = entry.getKey();
  90. Node value = entry.getValue();
  91. // int type = this.type.G(name);
  92. r += "{" + name + ":" + value + "},";
  93. }
  94. return r;
  95. }
  96. public int Offset(String id) {
  97. if (id.contains("_G") && data != null) {
  98. return data.Offset(id);
  99. }
  100. int offset = 0;
  101. try {
  102. id = normalize(id);
  103. for (Map.Entry<String, Node> x : values.entrySet()) {
  104. if (!x.getKey().equals(id)) {
  105. offset += x.getValue().getInt("size");
  106. continue;
  107. }
  108. break;
  109. }
  110. } catch (Exception ex) {
  111. Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
  112. }
  113. return offset;
  114. }
  115. protected String normalize(String id) {
  116. String shift = "0", or = id;
  117. if (id.contains("[")) {
  118. String[] parts = id.split("\\[");
  119. id = parts[0];
  120. shift = parts[1].replace("]", "");
  121. }
  122. if (!id.contains(".")) {
  123. id += "." + shift;
  124. }
  125. // System.out.println("Normalize:" + id + "::" + or);
  126. return id;
  127. }
  128. public DataFrame copy(DataFrame data) {
  129. for (Map.Entry<String, Node> x : data.values().entrySet()) {
  130. this.values.put(x.getKey(), x.getValue());
  131. }
  132. return this;
  133. }
  134. }