package x2x.translator.xcodeml.xelement;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Hashtable;

/**
 * The XargumentsTable represents the arguments (7.5.3) element in XcodeML
 * intermediate representation.
 *
 * Elements:
 * - Optional:
 *   - exprModel
 */

public class XargumentsTable {
  // TODO exprModel. For the moment only read var
  private Hashtable<String, Xvar> _table;
  private Element _baseElement = null;

  public XargumentsTable(Element arguments){
    _baseElement = arguments;
    _table = new Hashtable<String, Xvar>();
    readTable();
  }

  private void readTable(){
    // Read Var element
    NodeList elements = _baseElement.getElementsByTagName(XelementName.VAR);
    for (int i = 0; i < elements.getLength(); i++) {
      Node n = elements.item(i);
      if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) n;
        Xvar var = new Xvar(el);
        _table.put(var.getType(), var);
      }
    }
  }

  public void add(Xvar var){
    _baseElement.appendChild(var.clone());
    _table.put(var.getType(), var);
  }

  public Xvar get(String key){
    return _table.get(key);
  }
}

@@ -144,16 +144,38 @@
 shtable;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1023,24 +1023,223 @@
 %0A    %7D%0A  %7D%0A%0A
+  public Xvar findArgument(String name)%7B%0A    for(String key : _table.keySet())%7B%0A      if(_table.get(key).getValue().equals(name))%7B%0A        return _table.get(key);%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A
   public voi

Find Var by its name