Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / WhileFunction.java @ 47734

History | View | Annotate | Download (3.67 KB)

1
package org.gvsig.expressionevaluator.impl.function.programming;
2

    
3
import java.util.Objects;
4
import org.apache.commons.lang3.Range;
5
import org.gvsig.expressionevaluator.Code;
6
import org.gvsig.expressionevaluator.Codes;
7
import org.gvsig.expressionevaluator.Formatter;
8
import org.gvsig.expressionevaluator.Function;
9
import org.gvsig.expressionevaluator.Interpreter;
10
import org.gvsig.expressionevaluator.PrettyFormatter;
11
import org.gvsig.expressionevaluator.impl.function.programming.BreakFunction.BreakException;
12
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction.ReturnException;
13
import org.gvsig.expressionevaluator.spi.AbstractFunction;
14

    
15

    
16
public class WhileFunction extends AbstractFunction {
17

    
18
    public static final String NAME = "WHILE";
19

    
20
    public WhileFunction() {
21
        super(Function.GROUP_PROGRAMMING, 
22
                NAME, 
23
                Range.is(2),
24
                "The while() function evaluate expression while the condition is true.",
25
                "WHILE {{condition}} BEGIN\n  PASS;\nEND WHILE\n",
26
                null,
27
                "Object",
28
                false
29
        );
30
    }
31

    
32
    @Override
33
    public boolean isHidden() {
34
      return true;
35
    }
36
    
37
    @Override
38
    public boolean useArgumentsInsteadObjects() {
39
        return true;
40
    }
41

    
42
    @Override
43
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
44
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
45
    }
46
    
47
    @Override
48
    public boolean allowConstantFolding() {
49
        return false;
50
    }    
51
    
52
    @Override
53
    public Object call(Interpreter interpreter, Codes args) throws Exception {
54
        Object body = null;
55
        int argn = 0;
56
        boolean condition = getBoolean(interpreter, args, 0);
57
        while( condition ) {
58
            try {
59
                body = getObject(interpreter, args, 1);
60
            } catch(BreakException ex) {
61
                break;
62
            } catch(ReturnException ex) {
63
                throw ex;
64
            } catch(Exception ex) {
65
                String stmt = "unknown";
66
                try {
67
                    stmt = Objects.toString(args);
68
                } catch(Exception ex2) {
69
                    
70
                }
71
                LOGGER.warn("Error in while function calling arg "+ argn + ", "+ stmt, ex);
72
                throw ex;
73
            }
74
            condition = getBoolean(interpreter, args, 0);
75
            argn++;
76
        }
77
        return body;
78
    }
79
    
80

    
81
    @Override
82
    public String toString(Codes args, Formatter<Code> formatter) {
83
        Code codeCondition = args.get(0);
84
        Code codeLoopBody = args.get(1);
85
        PrettyFormatter builder = PrettyFormatter.get(formatter);
86
        try {
87
            builder.push();
88
            builder.append("WHILE ");
89
            builder.append(codeCondition.toString(formatter));
90
            builder.append(" LOOP ");
91
            builder.indent();
92
            builder.nl();
93
            if( Code.isFunction(codeLoopBody,"BLOCK") ) {
94
                Code.Callable block = (Code.Callable)codeLoopBody;
95
                for (Code parameter : block.parameters()) {
96
                    builder.nl();
97
                    builder.append(parameter.toString(formatter));
98
                    builder.append("; ");
99
                }
100
            } else {
101
                builder.nl();
102
                builder.append(codeLoopBody.toString(formatter));
103
                builder.append(" ");
104
            }            
105
            builder.unindent();
106
            builder.nl();
107
            builder.append("END LOOP ");
108
            return builder.build();
109
        } finally {
110
            builder.pop();
111
        }
112
    }
113

    
114
}