Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void foreachStat(Foreach foreach, VariableScope scope) throws CompilationException {
- try {
- VariableScope innerScope = new VariableScope(this, scope, VariableScope.ENCLOSURE_LOOP);
- StringTemplate foreachST = template("foreach");
- foreachST.setAttribute("var", foreach.getVar());
- foreachST.setAttribute("line", foreach.getSrc().substring(foreach.getSrc().indexOf(' ') + 1));
- XmlObject in = foreach.getIn().getAbstractExpression();
- StringTemplate inST = expressionToKarajan(in, scope);
- foreachST.setAttribute("in", inST);
- String inType = datatype(inST);
- // TODO Add checks here for inType being on the new array type structure
- //if (inType.length() < 2 || !inType.substring(inType.length() - 2).equals("[]"))
- // throw new CompilationException("You can iterate through an array structure only");
- String temp = scope.getVariableType("array");
- String varType = getParentLevel(inType);
- //String varType = inType.substring(0, inType.length() - 2);
- innerScope.addVariable(foreach.getVar(), varType);
- foreachST.setAttribute("indexVar", foreach.getIndexVar());
- if(foreach.getIndexVar() != null) {
- String predef[] = {"int","string","float"};
- int i;
- for (i=0; i < 2 ; i++){
- String end = "[" + predef[i] + "]" ;
- if (temp.startsWith(end,inType.length())){
- innerScope.addVariable(foreach.getIndexVar(), predef[i]);
- }
- }
- }
- innerScope.bodyTemplate = foreachST;
- statementsForSymbols(foreach.getBody(), innerScope);
- statements(foreach.getBody(), innerScope);
- String inVar = (String) inST.getAttribute("var");
- Object statementID = new Integer(callID++);
- for (String v : innerScope.getVariables()) {
- scope.addWriter(v, statementID, true);
- if (v.equals(inVar) && !innerScope.isVariableLocallyDefined(v))
- foreachST.setAttribute("selfClose", "true");
- }
- scope.appendStatement(foreachST);
- } catch(CompilationException re) {
- throw new CompilationException("Compile error in foreach statement at "+foreach.getSrc()+": "+re.getMessage(),re);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement