Revision 43521 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/DefaultInterpreter.java
DefaultInterpreter.java | ||
---|---|---|
13 | 13 |
|
14 | 14 |
public class DefaultInterpreter implements Interpreter { |
15 | 15 |
|
16 |
SymbolTable symbolTable = null; |
|
16 |
private SymbolTable symbolTable = null; |
|
17 |
private Double accuracy; |
|
18 |
private Code currentCode; |
|
17 | 19 |
|
18 | 20 |
public DefaultInterpreter() { |
19 | 21 |
this.symbolTable = SQLSymbolTable.getInstance(); |
... | ... | |
28 | 30 |
public SymbolTable getSymbolTable() { |
29 | 31 |
return this.symbolTable; |
30 | 32 |
} |
31 |
|
|
32 | 33 |
@Override |
34 |
public Double getAccuracy() { |
|
35 |
return this.accuracy; |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public void setAccuracy(Double accuracy) { |
|
40 |
this.accuracy = accuracy; |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
33 | 44 |
public Object run(Code code) { |
34 | 45 |
try { |
35 | 46 |
return this.runCode(code); |
... | ... | |
49 | 60 |
if( caller.function() == null ) { |
50 | 61 |
caller.function(this.symbolTable.function(caller.name())); |
51 | 62 |
} |
52 |
if( caller.args()!=null ) {
|
|
63 |
if( caller.args() != null ) {
|
|
53 | 64 |
for( Code arg : caller.args() ) { |
54 | 65 |
linkCode(arg); |
55 | 66 |
} |
... | ... | |
58 | 69 |
} |
59 | 70 |
|
60 | 71 |
private Object runCode(Code code) throws Exception { |
72 |
this.currentCode = code; |
|
61 | 73 |
Object value = null; |
62 | 74 |
switch( code.code() ) { |
63 | 75 |
case Code.CONSTANT: |
... | ... | |
83 | 95 |
caller.function(function); |
84 | 96 |
} |
85 | 97 |
Arguments args = caller.args(); |
86 |
switch(caller.type()) { |
|
87 |
case Caller.UNARY_OPERATOR: |
|
88 |
if( args==null || args.count()!=1 ) { |
|
89 |
throw new RuntimeException("Number of argument mistmatch, expected 1 got "+args.count()+"."); |
|
98 |
try { |
|
99 |
switch( caller.type() ) { |
|
100 |
case Caller.UNARY_OPERATOR: |
|
101 |
if( args == null || args.count() != 1 ) { |
|
102 |
throw new RuntimeException("Number of argument mistmatch, expected 1 got " + args.count() + "."); |
|
103 |
} |
|
104 |
value = ((UnaryOperator) function).call(this, runCode(args.get(0))); |
|
105 |
break; |
|
106 |
|
|
107 |
case Caller.BINARY_OPERATOR: |
|
108 |
if( args == null || args.count() != 2 ) { |
|
109 |
throw new RuntimeException("Number of argument mistmatch, expected 2 got " + args.count() + "."); |
|
110 |
} |
|
111 |
value = ((BinaryOperator) function).call(this, runCode(args.get(0)), runCode(args.get(1))); |
|
112 |
break; |
|
113 |
|
|
114 |
case Caller.FUNCTION: |
|
115 |
int argc = (args == null) ? 0 : args.count(); |
|
116 |
if( !function.argc().contains(argc) ) { |
|
117 |
throw new RuntimeException("Number of argument mistmatch, expected " + function.argc() + " got " + argc + "."); |
|
118 |
} |
|
119 |
Object[] argvalues = new Object[argc]; |
|
120 |
if( args != null ) { |
|
121 |
int i = 0; |
|
122 |
for( Code arg : args ) { |
|
123 |
argvalues[i++] = runCode(arg); |
|
124 |
} |
|
125 |
} |
|
126 |
value = function.call(this, argvalues); |
|
90 | 127 |
} |
91 |
value = ((UnaryOperator)function).call(runCode(args.get(0)));
|
|
92 |
break;
|
|
93 |
|
|
94 |
case Caller.BINARY_OPERATOR:
|
|
95 |
if( args==null || args.count()!=2 ) {
|
|
96 |
throw new RuntimeException("Number of argument mistmatch, expected 2 got "+args.count()+".");
|
|
128 |
} catch (Exception ex) {
|
|
129 |
String argsstr = "???";
|
|
130 |
try { |
|
131 |
argsstr = args.toString();
|
|
132 |
} catch(Exception ex2) {
|
|
133 |
// Ignore.
|
|
97 | 134 |
} |
98 |
value = ((BinaryOperator)function).call(runCode(args.get(0)), runCode(args.get(1))); |
|
99 |
break; |
|
100 |
|
|
101 |
case Caller.FUNCTION: |
|
102 |
int argc = (args==null)? 0:args.count(); |
|
103 |
if( !function.argc().contains(argc) ) { |
|
104 |
throw new RuntimeException("Number of argument mistmatch, expected "+function.argc()+" got "+argc+"."); |
|
105 |
} |
|
106 |
Object[] argvalues = new Object[argc]; |
|
107 |
if( args!=null ) { |
|
108 |
int i=0; |
|
109 |
for( Code arg : args) { |
|
110 |
argvalues[i++]=runCode(arg); |
|
111 |
} |
|
112 |
} |
|
113 |
value = function.call(argvalues); |
|
135 |
throw new RuntimeException("Problems calling function '"+function.name()+"' with args ("+argsstr+").", ex); |
|
114 | 136 |
} |
115 | 137 |
} |
138 |
this.currentCode = null; |
|
116 | 139 |
return value; |
117 | 140 |
} |
141 |
|
|
142 |
@Override |
|
143 |
public Code getCurrentCode() { |
|
144 |
return this.currentCode; |
|
145 |
} |
|
118 | 146 |
} |
Also available in: Unified diff