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 44138 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.programming;
2 43512 jjdelcerro
3 47017 jjdelcerro
import java.util.Objects;
4 43512 jjdelcerro
import org.apache.commons.lang3.Range;
5 47115 jjdelcerro
import org.gvsig.expressionevaluator.Code;
6 44138 jjdelcerro
import org.gvsig.expressionevaluator.Codes;
7 47115 jjdelcerro
import org.gvsig.expressionevaluator.Formatter;
8 43939 jjdelcerro
import org.gvsig.expressionevaluator.Function;
9 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
10 47734 jjdelcerro
import org.gvsig.expressionevaluator.PrettyFormatter;
11 47017 jjdelcerro
import org.gvsig.expressionevaluator.impl.function.programming.BreakFunction.BreakException;
12
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction.ReturnException;
13 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
14
15 44138 jjdelcerro
16 43939 jjdelcerro
public class WhileFunction extends AbstractFunction {
17 43512 jjdelcerro
18 44138 jjdelcerro
    public static final String NAME = "WHILE";
19
20 43939 jjdelcerro
    public WhileFunction() {
21 44138 jjdelcerro
        super(Function.GROUP_PROGRAMMING,
22
                NAME,
23 43939 jjdelcerro
                Range.is(2),
24
                "The while() function evaluate expression while the condition is true.",
25 44924 jjdelcerro
                "WHILE {{condition}} BEGIN\n  PASS;\nEND WHILE\n",
26 44098 jjdelcerro
                null,
27
                "Object",
28
                false
29 43939 jjdelcerro
        );
30 43512 jjdelcerro
    }
31
32
    @Override
33 44738 jjdelcerro
    public boolean isHidden() {
34
      return true;
35
    }
36
37
    @Override
38 43939 jjdelcerro
    public boolean useArgumentsInsteadObjects() {
39
        return true;
40
    }
41
42
    @Override
43 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
44 43939 jjdelcerro
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
45
    }
46
47
    @Override
48 44138 jjdelcerro
    public boolean allowConstantFolding() {
49
        return false;
50
    }
51
52
    @Override
53
    public Object call(Interpreter interpreter, Codes args) throws Exception {
54 43939 jjdelcerro
        Object body = null;
55 47017 jjdelcerro
        int argn = 0;
56 43939 jjdelcerro
        boolean condition = getBoolean(interpreter, args, 0);
57
        while( condition ) {
58 47017 jjdelcerro
            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 43939 jjdelcerro
            condition = getBoolean(interpreter, args, 0);
75 47017 jjdelcerro
            argn++;
76 43512 jjdelcerro
        }
77 43939 jjdelcerro
        return body;
78 43512 jjdelcerro
    }
79
80 47115 jjdelcerro
81
    @Override
82
    public String toString(Codes args, Formatter<Code> formatter) {
83 47734 jjdelcerro
        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 47115 jjdelcerro
    }
113 47734 jjdelcerro
114
}