Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public void foreachStat(Foreach foreach, VariableScope scope) throws CompilationException {
  2. try {
  3. VariableScope innerScope = new VariableScope(this, scope, VariableScope.ENCLOSURE_LOOP);
  4.  
  5. StringTemplate foreachST = template("foreach");
  6. foreachST.setAttribute("var", foreach.getVar());
  7. foreachST.setAttribute("line", foreach.getSrc().substring(foreach.getSrc().indexOf(' ') + 1));
  8.  
  9. XmlObject in = foreach.getIn().getAbstractExpression();
  10. StringTemplate inST = expressionToKarajan(in, scope);
  11. foreachST.setAttribute("in", inST);
  12.  
  13. String inType = datatype(inST);
  14. // TODO Add checks here for inType being on the new array type structure
  15. //if (inType.length() < 2 || !inType.substring(inType.length() - 2).equals("[]"))
  16. // throw new CompilationException("You can iterate through an array structure only");
  17. String temp = scope.getVariableType("array");
  18. String varType = getParentLevel(inType);
  19. //String varType = inType.substring(0, inType.length() - 2);
  20. innerScope.addVariable(foreach.getVar(), varType);
  21. foreachST.setAttribute("indexVar", foreach.getIndexVar());
  22. if(foreach.getIndexVar() != null) {
  23. String predef[] = {"int","string","float"};
  24. int i;
  25. for (i=0; i < 2 ; i++){
  26. String end = "[" + predef[i] + "]" ;
  27. if (temp.startsWith(end,inType.length())){
  28. innerScope.addVariable(foreach.getIndexVar(), predef[i]);
  29. }
  30. }
  31. }
  32.  
  33. innerScope.bodyTemplate = foreachST;
  34.  
  35. statementsForSymbols(foreach.getBody(), innerScope);
  36. statements(foreach.getBody(), innerScope);
  37.  
  38. String inVar = (String) inST.getAttribute("var");
  39. Object statementID = new Integer(callID++);
  40. for (String v : innerScope.getVariables()) {
  41. scope.addWriter(v, statementID, true);
  42. if (v.equals(inVar) && !innerScope.isVariableLocallyDefined(v))
  43. foreachST.setAttribute("selfClose", "true");
  44. }
  45. scope.appendStatement(foreachST);
  46. } catch(CompilationException re) {
  47. throw new CompilationException("Compile error in foreach statement at "+foreach.getSrc()+": "+re.getMessage(),re);
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement