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 / DefaultCodeBuilder.java @ 46517
History | View | Annotate | Download (29.7 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import java.util.function.Predicate; |
9 |
import org.gvsig.expressionevaluator.Code; |
10 |
import static org.gvsig.expressionevaluator.Code.CONSTANT; |
11 |
import static org.gvsig.expressionevaluator.Code.IDENTIFIER; |
12 |
import static org.gvsig.expressionevaluator.Code.UNDEFINED; |
13 |
import static org.gvsig.expressionevaluator.Code.CODES; |
14 |
import org.gvsig.expressionevaluator.Code.Constant; |
15 |
import static org.gvsig.expressionevaluator.Code.EMPTY_FORMATTER; |
16 |
import org.gvsig.expressionevaluator.Code.Identifier; |
17 |
import org.gvsig.expressionevaluator.Code.Method; |
18 |
//import org.gvsig.expressionevaluator.Code.Method;
|
19 |
import org.gvsig.expressionevaluator.CodeBuilder; |
20 |
import org.gvsig.expressionevaluator.Codes; |
21 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
22 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
23 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_ADD; |
24 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_AND; |
25 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_CONCAT; |
26 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_DIV; |
27 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_EQ; |
28 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_GE; |
29 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_GT; |
30 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_ILIKE; |
31 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_IS; |
32 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_LE; |
33 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_LIKE; |
34 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_LT; |
35 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_MULT; |
36 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_NE; |
37 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_NEGATE; |
38 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_NOT; |
39 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_OR; |
40 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_REGEXP; |
41 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_SUBST; |
42 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
43 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
44 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
45 |
import org.gvsig.expressionevaluator.Formatter; |
46 |
import org.gvsig.expressionevaluator.Function; |
47 |
import org.gvsig.expressionevaluator.Interpreter; |
48 |
import org.gvsig.expressionevaluator.SymbolTable; |
49 |
import org.gvsig.tools.dynobject.DynObject; |
50 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
51 |
import org.gvsig.tools.exception.BaseException; |
52 |
import org.gvsig.tools.visitor.Visitor; |
53 |
import static org.gvsig.expressionevaluator.Code.CALLABLE; |
54 |
import org.gvsig.expressionevaluator.Code.Callable; |
55 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DICT; |
56 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETITEM; |
57 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_LET; |
58 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_MOD; |
59 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
60 |
import org.gvsig.tools.visitor.FilteredVisitable; |
61 |
|
62 |
@SuppressWarnings("UseSpecificCatch") |
63 |
public class DefaultCodeBuilder implements CodeBuilder { |
64 |
|
65 |
public static abstract class BaseCode implements Code { |
66 |
|
67 |
@Override
|
68 |
public int code() { |
69 |
return UNDEFINED;
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public Code clone() throws CloneNotSupportedException { |
74 |
BaseCode x = (BaseCode) super.clone();
|
75 |
return x;
|
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void accept(Visitor visitor) throws BaseException { |
80 |
this.accept(visitor, null); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public void accept(Visitor visitor, Predicate<FilteredVisitable> exclude) throws BaseException { |
85 |
if( exclude==null || !exclude.test(this) ) { |
86 |
visitor.visit(this);
|
87 |
} |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public Value toValue(ExpressionBuilder builder) {
|
92 |
return null; |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public Value toValue() {
|
97 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
98 |
return this.toValue(builder); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void link(SymbolTable symbolTable) { |
103 |
if( this.code() == Code.CALLABLE ) { |
104 |
Callable caller = (Callable) this; |
105 |
Function fn = symbolTable.function(caller.name()); |
106 |
if( fn != null ) { |
107 |
caller.function(fn); |
108 |
} |
109 |
if( caller.parameters() != null ) { |
110 |
for( Code arg : caller.parameters() ) {
|
111 |
if( arg!=null ) { |
112 |
arg.link(symbolTable); |
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void link() { |
121 |
SymbolTable symbolTable = ExpressionEvaluatorLocator.getExpressionEvaluatorManager().getInmutableSymbolTable(); |
122 |
this.link(symbolTable);
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void replace(Code target, Code replacement) { |
127 |
} |
128 |
|
129 |
|
130 |
} |
131 |
|
132 |
static class BaseConstant extends BaseCode implements Constant { |
133 |
|
134 |
private Object value; |
135 |
private final ExpressionEvaluatorManager manager; |
136 |
|
137 |
public BaseConstant(ExpressionEvaluatorManager manager, Object value) { |
138 |
this.manager = manager;
|
139 |
this.value = value;
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public Code clone() throws CloneNotSupportedException { |
144 |
return (Code) super.clone(); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public int code() { |
149 |
return CONSTANT;
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public Object value() { |
154 |
return this.value; |
155 |
} |
156 |
|
157 |
public void value(Object v) { |
158 |
this.value = v;
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public Value toValue(ExpressionBuilder builder) {
|
163 |
ExpressionBuilder.Constant v = builder.constant(this.value);
|
164 |
v.copyPropertiesFrom(builder); |
165 |
return v;
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public String toString() { |
170 |
return this.toString(EMPTY_FORMATTER); |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public String toString(Formatter<Code> formatter) { |
175 |
if( formatter.canApply(this) ) { |
176 |
return formatter.format(this); |
177 |
} |
178 |
Object v = this.value(); |
179 |
return manager.getReprMethod(v).repr(v);
|
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
public interface RecursionControlSupport { |
185 |
|
186 |
public boolean enterCode(int max); |
187 |
|
188 |
public void exitCode(); |
189 |
|
190 |
public void resetRecursionState(); |
191 |
} |
192 |
|
193 |
private static class RecursionSupport implements RecursionControlSupport, Cloneable { |
194 |
|
195 |
private int counter; |
196 |
|
197 |
public RecursionSupport() {
|
198 |
this.counter = 0; |
199 |
} |
200 |
|
201 |
@Override
|
202 |
public RecursionControlSupport clone() throws CloneNotSupportedException { |
203 |
RecursionControlSupport x = (RecursionControlSupport) super.clone();
|
204 |
return x;
|
205 |
} |
206 |
|
207 |
@Override
|
208 |
public boolean enterCode(int max) { |
209 |
this.counter += 1; |
210 |
return this.counter < max; |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void exitCode() { |
215 |
this.counter -= 1; |
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void resetRecursionState() { |
220 |
this.counter = 0; |
221 |
} |
222 |
|
223 |
} |
224 |
|
225 |
public static class BaseIdentifier extends BaseCode implements Identifier, RecursionControlSupport { |
226 |
|
227 |
private final String name; |
228 |
private final RecursionSupport recursionSupport; |
229 |
|
230 |
public BaseIdentifier(String name) { |
231 |
this.name = name;
|
232 |
this.recursionSupport = new RecursionSupport(); |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public Code clone() throws CloneNotSupportedException { |
237 |
return (Code) super.clone(); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public int code() { |
242 |
return IDENTIFIER;
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public String name() { |
247 |
return this.name; |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public Value toValue(ExpressionBuilder builder) {
|
252 |
ExpressionBuilder.Variable v = builder.variable(this.name);
|
253 |
v.copyPropertiesFrom(builder); |
254 |
return v;
|
255 |
} |
256 |
|
257 |
@Override
|
258 |
public String toString() { |
259 |
return this.toString(EMPTY_FORMATTER); |
260 |
} |
261 |
|
262 |
@Override
|
263 |
public String toString(Formatter<Code> formatter) { |
264 |
if( formatter.canApply(this) ) { |
265 |
return formatter.format(this); |
266 |
} |
267 |
StringBuilder builder = new StringBuilder(); |
268 |
builder.append("\"");
|
269 |
builder.append(this.name());
|
270 |
builder.append("\"");
|
271 |
return builder.toString();
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public boolean enterCode(int max) { |
276 |
return this.recursionSupport.enterCode(max); |
277 |
} |
278 |
|
279 |
@Override
|
280 |
public void exitCode() { |
281 |
this.recursionSupport.exitCode();
|
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void resetRecursionState() { |
286 |
this.recursionSupport.resetRecursionState();
|
287 |
} |
288 |
|
289 |
} |
290 |
|
291 |
public static class BaseCodes implements Codes { |
292 |
|
293 |
private List<Code> codes; |
294 |
|
295 |
public BaseCodes() {
|
296 |
this.codes = new ArrayList<>(); |
297 |
} |
298 |
|
299 |
@Override
|
300 |
public Codes clone() throws CloneNotSupportedException { |
301 |
BaseCodes x = (BaseCodes)super.clone();
|
302 |
x.codes = new ArrayList<>(); |
303 |
for (int i = 0; i < this.codes.size(); i++) { |
304 |
x.add(this.codes.get(i).clone());
|
305 |
} |
306 |
return x;
|
307 |
} |
308 |
|
309 |
@Override
|
310 |
public int code() { |
311 |
return CODES;
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public int size() { |
316 |
if( codes == null ) { |
317 |
return 0; |
318 |
} |
319 |
return this.codes.size(); |
320 |
} |
321 |
|
322 |
public void add(Code arg) { |
323 |
this.codes.add(arg);
|
324 |
} |
325 |
|
326 |
public void set(int pos, Code arg) { |
327 |
this.codes.set(pos, arg);
|
328 |
} |
329 |
|
330 |
public void insert(int pos, Code arg) { |
331 |
this.codes.add(pos, arg);
|
332 |
} |
333 |
|
334 |
@Override
|
335 |
public Iterator<Code> iterator() { |
336 |
final Iterator<Code> it = this.codes.iterator(); |
337 |
return it;
|
338 |
} |
339 |
|
340 |
@Override
|
341 |
public Code get(int n) { |
342 |
return this.codes.get(n); |
343 |
} |
344 |
|
345 |
@Override
|
346 |
public boolean isEmpty() { |
347 |
return this.codes.isEmpty(); |
348 |
} |
349 |
|
350 |
@Override
|
351 |
public List<Code> toList() { |
352 |
return Collections.unmodifiableList(this.codes); |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public void accept(Visitor visitor) throws BaseException { |
357 |
this.accept(visitor, null); |
358 |
} |
359 |
|
360 |
public void accept(Visitor visitor, Predicate<FilteredVisitable> exclude) throws BaseException { |
361 |
if( this.codes != null ) { |
362 |
for( Code arg : this.codes ) { |
363 |
if(arg!=null ) { |
364 |
arg.accept(visitor, exclude); |
365 |
} |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
@Override
|
371 |
public String toString() { |
372 |
return this.toString(EMPTY_FORMATTER); |
373 |
} |
374 |
|
375 |
@Override
|
376 |
public Value toValue(ExpressionBuilder builder) {
|
377 |
throw new UnsupportedOperationException(); |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public Value toValue() {
|
382 |
throw new UnsupportedOperationException(); |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public String toString(Formatter<Code> formatter) { |
387 |
if( formatter.canApply(this) ) { |
388 |
return formatter.format(this); |
389 |
} |
390 |
if( codes != null ) { |
391 |
StringBuilder builder = new StringBuilder(); |
392 |
boolean skipcoma = true; |
393 |
for( Code arg : codes ) {
|
394 |
if( arg == null ) { |
395 |
continue;
|
396 |
} |
397 |
if( skipcoma ) {
|
398 |
skipcoma = false;
|
399 |
} else {
|
400 |
builder.append(", ");
|
401 |
} |
402 |
builder.append(arg.toString(formatter)); |
403 |
} |
404 |
return builder.toString();
|
405 |
} |
406 |
return ""; |
407 |
} |
408 |
|
409 |
@Override
|
410 |
public void link(SymbolTable symbolTable) { |
411 |
for (Code arg : this.codes) { |
412 |
arg.link(symbolTable); |
413 |
} |
414 |
} |
415 |
|
416 |
@Override
|
417 |
public void link() { |
418 |
SymbolTable symbolTable = ExpressionEvaluatorLocator.getExpressionEvaluatorManager().getInmutableSymbolTable(); |
419 |
this.link(symbolTable);
|
420 |
} |
421 |
|
422 |
@Override
|
423 |
public void replace(Code target, Code replacement) { |
424 |
for (int i = 0; i < this.codes.size(); i++) { |
425 |
Code code = this.codes.get(i);
|
426 |
if( code == target ) {
|
427 |
codes.set(i, replacement); |
428 |
} else {
|
429 |
code.replace(target, replacement); |
430 |
} |
431 |
} |
432 |
} |
433 |
|
434 |
} |
435 |
|
436 |
public class BaseCaller extends BaseCode implements Callable, RecursionControlSupport { |
437 |
|
438 |
private final String name; |
439 |
private Codes args;
|
440 |
private Function function;
|
441 |
private final int type; |
442 |
private RecursionSupport recursionSupport;
|
443 |
|
444 |
public BaseCaller(String name, int type, Codes args) { |
445 |
this.name = name;
|
446 |
this.args = args;
|
447 |
this.type = type;
|
448 |
this.function = null; |
449 |
this.recursionSupport = new RecursionSupport(); |
450 |
} |
451 |
|
452 |
@Override
|
453 |
public Code clone() throws CloneNotSupportedException { |
454 |
BaseCaller x = (BaseCaller) super.clone();
|
455 |
x.recursionSupport = (RecursionSupport) this.recursionSupport.clone();
|
456 |
if (this.args!=null){ |
457 |
x.args = this.args.clone();
|
458 |
} |
459 |
return x;
|
460 |
} |
461 |
|
462 |
@Override
|
463 |
public int code() { |
464 |
return CALLABLE;
|
465 |
} |
466 |
|
467 |
@Override
|
468 |
public void replace(Code target, Code replacement) { |
469 |
this.args.replace(target, replacement);
|
470 |
} |
471 |
|
472 |
@Override
|
473 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
474 |
return this.function.call(interpreter, args); |
475 |
} |
476 |
|
477 |
@Override
|
478 |
public String name() { |
479 |
return this.name; |
480 |
} |
481 |
|
482 |
@Override
|
483 |
public Function function() {
|
484 |
return this.function; |
485 |
} |
486 |
|
487 |
@Override
|
488 |
public Function function(Function function) {
|
489 |
this.function = function;
|
490 |
return this.function; |
491 |
} |
492 |
|
493 |
@Override
|
494 |
public Codes parameters() {
|
495 |
return this.args; |
496 |
} |
497 |
|
498 |
@Override
|
499 |
public int type() { |
500 |
return this.type; |
501 |
} |
502 |
|
503 |
@Override
|
504 |
public void accept(Visitor visitor, Predicate<FilteredVisitable> exclude) throws BaseException { |
505 |
if( exclude!=null && exclude.test(this) ) { |
506 |
return;
|
507 |
} |
508 |
super.accept(visitor, exclude);
|
509 |
if(this.args!=null ) { |
510 |
this.args.accept(visitor, exclude);
|
511 |
} |
512 |
} |
513 |
|
514 |
@Override
|
515 |
public Value toValue(ExpressionBuilder builder) {
|
516 |
ExpressionBuilder.Value value; |
517 |
switch(this.type) { |
518 |
case UNARY_OPERATOR:
|
519 |
value = builder.function( |
520 |
OPERATOR_NEGATE.equalsIgnoreCase(this.name())?
|
521 |
"-" :
|
522 |
this.name(),
|
523 |
this.parameters().get(0).toValue(builder) |
524 |
); |
525 |
break;
|
526 |
|
527 |
case BINARY_OPERATOR:
|
528 |
value = builder.binaryOperator( |
529 |
this.name(),
|
530 |
this.parameters().get(0).toValue(builder), |
531 |
this.parameters().get(1).toValue(builder) |
532 |
); |
533 |
break;
|
534 |
case FUNCTION:
|
535 |
default:
|
536 |
if(this.function == null){ |
537 |
ExpressionBuilder.Function f = builder.function(this.name());
|
538 |
if( this.parameters()!=null ) { |
539 |
for (Code parameter : this.parameters()) { |
540 |
if (parameter==null) { |
541 |
f.parameter(null);
|
542 |
} else {
|
543 |
f.parameter(parameter.toValue(builder)); |
544 |
} |
545 |
} |
546 |
} |
547 |
value = f; |
548 |
} else {
|
549 |
value = this.function.toValue(builder, this.parameters()); |
550 |
} |
551 |
break;
|
552 |
|
553 |
} |
554 |
value.copyPropertiesFrom(builder); |
555 |
return value;
|
556 |
} |
557 |
|
558 |
@Override
|
559 |
public String toString() { |
560 |
return this.toString(EMPTY_FORMATTER); |
561 |
} |
562 |
|
563 |
@Override
|
564 |
public String toString(Formatter<Code> formatter) { |
565 |
if( formatter.canApply(this) ) { |
566 |
return formatter.format(this); |
567 |
} |
568 |
Code code; |
569 |
StringBuilder builder = new StringBuilder(); |
570 |
switch(this.type) { |
571 |
case UNARY_OPERATOR:
|
572 |
if( OPERATOR_NEGATE.equalsIgnoreCase(this.name()) ) { |
573 |
builder.append("-");
|
574 |
} else {
|
575 |
builder.append(this.name());
|
576 |
} |
577 |
builder.append("(");
|
578 |
builder.append(this.parameters().get(0).toString(formatter)); |
579 |
builder.append(")");
|
580 |
break;
|
581 |
case BINARY_OPERATOR:
|
582 |
builder.append("(");
|
583 |
code = this.parameters().get(0); |
584 |
if( code == null ) { |
585 |
builder.append("?NULL?");
|
586 |
} else {
|
587 |
builder.append(code.toString(formatter)); |
588 |
} |
589 |
builder.append(" ");
|
590 |
builder.append(this.name());
|
591 |
builder.append(" ");
|
592 |
code = this.parameters().get(1); |
593 |
if( code == null ) { |
594 |
builder.append("?NULL?");
|
595 |
} else {
|
596 |
builder.append(code.toString(formatter)); |
597 |
} |
598 |
builder.append(")");
|
599 |
break;
|
600 |
case FUNCTION:
|
601 |
default:
|
602 |
String s = null; |
603 |
if( this.function()!=null ) { |
604 |
s = this.function().toString(args, formatter);
|
605 |
} |
606 |
if( s == null ) { |
607 |
builder.append(this.name());
|
608 |
builder.append("(");
|
609 |
if( this.parameters()!=null ) { |
610 |
builder.append(this.parameters().toString(formatter));
|
611 |
} |
612 |
builder.append(")");
|
613 |
} else {
|
614 |
builder.append(s); |
615 |
} |
616 |
} |
617 |
return builder.toString();
|
618 |
} |
619 |
|
620 |
@Override
|
621 |
public boolean enterCode(int max) { |
622 |
return this.recursionSupport.enterCode(max); |
623 |
} |
624 |
|
625 |
@Override
|
626 |
public void exitCode() { |
627 |
this.recursionSupport.exitCode();
|
628 |
} |
629 |
|
630 |
@Override
|
631 |
public void resetRecursionState() { |
632 |
this.recursionSupport.resetRecursionState();
|
633 |
} |
634 |
} |
635 |
|
636 |
public static class BaseMethod extends BaseCode implements Method { |
637 |
|
638 |
private Code instance;
|
639 |
private final String methodname; |
640 |
private Codes args;
|
641 |
|
642 |
public BaseMethod(Code instance, String methodname, Codes args) { |
643 |
this.instance = instance;
|
644 |
this.methodname = methodname;
|
645 |
this.args = args;
|
646 |
} |
647 |
|
648 |
@Override
|
649 |
public void accept(Visitor visitor, Predicate<FilteredVisitable> exclude) throws BaseException { |
650 |
if( exclude!=null && exclude.test(this) ) { |
651 |
return;
|
652 |
} |
653 |
super.accept(visitor, exclude);
|
654 |
if(this.instance!=null) { |
655 |
this.instance.accept(visitor, exclude);
|
656 |
} |
657 |
if(this.args!=null) { |
658 |
this.args.accept(visitor, exclude);
|
659 |
} |
660 |
} |
661 |
|
662 |
@Override
|
663 |
public Code clone() throws CloneNotSupportedException { |
664 |
BaseMethod x = (BaseMethod) super.clone();
|
665 |
x.args = this.args.clone();
|
666 |
return x;
|
667 |
} |
668 |
|
669 |
@Override
|
670 |
public int code() { |
671 |
return METHOD;
|
672 |
} |
673 |
|
674 |
@Override
|
675 |
public void replace(Code target, Code replacement) { |
676 |
if( target == this.instance ) { |
677 |
this.instance = replacement;
|
678 |
} |
679 |
this.args.replace(target, replacement);
|
680 |
} |
681 |
|
682 |
@Override
|
683 |
public String methodname() { |
684 |
return this.methodname; |
685 |
} |
686 |
|
687 |
@Override
|
688 |
public Code instance() {
|
689 |
return this.instance; |
690 |
} |
691 |
|
692 |
@Override
|
693 |
public Codes parameters() {
|
694 |
return this.args; |
695 |
} |
696 |
|
697 |
@Override
|
698 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
699 |
Object theInstance = interpreter.run(instance);
|
700 |
if( theInstance instanceof SimpleScript ) { |
701 |
try {
|
702 |
return ((SimpleScript)theInstance).invokeFunction(methodname, args);
|
703 |
} catch(NoSuchMethodException ex) { |
704 |
// Ignore... continue calling instance method
|
705 |
} |
706 |
} else if( theInstance instanceof DynObject ) { |
707 |
DynObject dynobj = (DynObject) theInstance; |
708 |
try {
|
709 |
return dynobj.invokeDynMethod(methodname, args);
|
710 |
} catch(DynMethodNotSupportedException ex) {
|
711 |
// Ignore... continue calling instance method
|
712 |
} |
713 |
} |
714 |
return InstanceUtils.callmethod(theInstance, methodname, args);
|
715 |
} |
716 |
|
717 |
@Override
|
718 |
public String toString() { |
719 |
return this.toString(EMPTY_FORMATTER); |
720 |
} |
721 |
|
722 |
@Override
|
723 |
public Value toValue(ExpressionBuilder builder) {
|
724 |
ExpressionBuilder.Method m = builder.method(this.instance.toValue(builder), this.methodname); |
725 |
if( this.parameters()!=null ) { |
726 |
for (Code parameter : this.parameters()) { |
727 |
m.parameter(parameter.toValue(builder)); |
728 |
} |
729 |
} |
730 |
m.copyPropertiesFrom(builder); |
731 |
return m;
|
732 |
} |
733 |
|
734 |
@Override
|
735 |
public String toString(Formatter<Code> formatter) { |
736 |
if( formatter.canApply(this) ) { |
737 |
return formatter.format(this); |
738 |
} |
739 |
StringBuilder builder = new StringBuilder(); |
740 |
builder.append(this.instance.toString(formatter));
|
741 |
builder.append(".");
|
742 |
builder.append(this.methodname());
|
743 |
builder.append("(");
|
744 |
if( this.parameters()!=null ) { |
745 |
builder.append(this.parameters().toString(formatter));
|
746 |
} |
747 |
builder.append(")");
|
748 |
return builder.toString();
|
749 |
} |
750 |
|
751 |
@Override
|
752 |
public String name() { |
753 |
return this.methodname(); |
754 |
} |
755 |
|
756 |
@Override
|
757 |
public Function function() {
|
758 |
return null; |
759 |
} |
760 |
|
761 |
@Override
|
762 |
public Function function(Function function) {
|
763 |
return null; |
764 |
} |
765 |
|
766 |
@Override
|
767 |
public int type() { |
768 |
return Code.METHOD;
|
769 |
} |
770 |
|
771 |
|
772 |
} |
773 |
|
774 |
protected ExpressionEvaluatorManager manager;
|
775 |
|
776 |
public DefaultCodeBuilder(ExpressionEvaluatorManager manager) {
|
777 |
this.manager = manager;
|
778 |
} |
779 |
|
780 |
@Override
|
781 |
public CodeBuilder clone() throws CloneNotSupportedException { |
782 |
// This implementation of CodeBuilder does not maintain state, so
|
783 |
// we only call the super class.
|
784 |
DefaultCodeBuilder other = (DefaultCodeBuilder) super.clone();
|
785 |
return other;
|
786 |
} |
787 |
|
788 |
@Override
|
789 |
public Constant constant(Object value) { |
790 |
return new BaseConstant(this.manager, value); |
791 |
} |
792 |
|
793 |
@Override
|
794 |
public Identifier identifier(String name) { |
795 |
return new BaseIdentifier(name); |
796 |
} |
797 |
|
798 |
@Override
|
799 |
public BaseCodes args() {
|
800 |
return new BaseCodes(); |
801 |
} |
802 |
|
803 |
@Override
|
804 |
public Callable tuple() { |
805 |
BaseCodes args = this.args();
|
806 |
return function(FUNCTION_TUPLE, args);
|
807 |
} |
808 |
|
809 |
@Override
|
810 |
public Callable tuple(Codes args) { |
811 |
if( args == null ) { |
812 |
args = this.args();
|
813 |
} |
814 |
return function(FUNCTION_TUPLE, args);
|
815 |
} |
816 |
|
817 |
@Override
|
818 |
public Callable function(String name, int type, Codes args) { |
819 |
return new BaseCaller(name, type, args); |
820 |
} |
821 |
|
822 |
@Override
|
823 |
public Callable function(String name, Codes args) { |
824 |
return function(name, Callable.FUNCTION, args); |
825 |
} |
826 |
|
827 |
@Override
|
828 |
public Code method(Code instance, String methodname, Codes methodargs) { |
829 |
Method m = new BaseMethod(instance, methodname, methodargs); |
830 |
return m;
|
831 |
} |
832 |
|
833 |
@Override
|
834 |
public Callable operator(String name, Code arg1) { |
835 |
BaseCodes args = args(); |
836 |
args.add(arg1); |
837 |
return function(name, Callable.UNARY_OPERATOR, args); |
838 |
} |
839 |
|
840 |
@Override
|
841 |
public Callable operator(String name, Code arg1, Code arg2) { |
842 |
BaseCodes args = args(); |
843 |
args.add(arg1); |
844 |
args.add(arg2); |
845 |
return function(name, Callable.BINARY_OPERATOR, args); |
846 |
} |
847 |
|
848 |
@Override
|
849 |
public Code not(Code op1) {
|
850 |
return operator(OPERATOR_NOT, op1);
|
851 |
} |
852 |
|
853 |
@Override
|
854 |
public Code negate(Code op1) {
|
855 |
return operator(OPERATOR_NEGATE, op1);
|
856 |
} |
857 |
|
858 |
@Override
|
859 |
public Code concat(Code op1, Code op2) {
|
860 |
return operator(OPERATOR_CONCAT, op1, op2);
|
861 |
} |
862 |
|
863 |
@Override
|
864 |
public Code let(String identifier, Code value) { |
865 |
BaseCodes args = args(); |
866 |
args.add(this.constant(identifier));
|
867 |
args.add(value); |
868 |
return function(FUNCTION_LET, Callable.FUNCTION, args); |
869 |
} |
870 |
|
871 |
@Override
|
872 |
public Code add(Code op1, Code op2) {
|
873 |
return operator(OPERATOR_ADD, op1, op2);
|
874 |
} |
875 |
|
876 |
@Override
|
877 |
public Code subst(Code op1, Code op2) {
|
878 |
return operator(OPERATOR_SUBST, op1, op2);
|
879 |
} |
880 |
|
881 |
@Override
|
882 |
public Code mult(Code op1, Code op2) {
|
883 |
return operator(OPERATOR_MULT, op1, op2);
|
884 |
} |
885 |
|
886 |
@Override
|
887 |
public Code div(Code op1, Code op2) {
|
888 |
return operator(OPERATOR_DIV, op1, op2);
|
889 |
} |
890 |
|
891 |
@Override
|
892 |
public Code mod(Code op1, Code op2) {
|
893 |
BaseCodes args = args(); |
894 |
args.add(op1); |
895 |
args.add(op2); |
896 |
return function(FUNCTION_MOD, args);
|
897 |
} |
898 |
|
899 |
@Override
|
900 |
public Code or(Code op1, Code op2) {
|
901 |
return operator(OPERATOR_OR, op1, op2);
|
902 |
} |
903 |
|
904 |
@Override
|
905 |
public Code and(Code op1, Code op2) {
|
906 |
return operator(OPERATOR_AND, op1, op2);
|
907 |
} |
908 |
|
909 |
@Override
|
910 |
public Code like(Code op1, Code op2) {
|
911 |
return operator(OPERATOR_LIKE, op1, op2);
|
912 |
} |
913 |
|
914 |
@Override
|
915 |
public Code ilike(Code op1, Code op2) {
|
916 |
return operator(OPERATOR_ILIKE, op1, op2);
|
917 |
} |
918 |
|
919 |
@Override
|
920 |
public Code regexp(Code op1, Code op2) {
|
921 |
return operator(OPERATOR_REGEXP, op1, op2);
|
922 |
} |
923 |
|
924 |
@Override
|
925 |
public Code lt(Code op1, Code op2) {
|
926 |
return operator(OPERATOR_LT, op1, op2);
|
927 |
} |
928 |
|
929 |
@Override
|
930 |
public Code gt(Code op1, Code op2) {
|
931 |
return operator(OPERATOR_GT, op1, op2);
|
932 |
} |
933 |
|
934 |
@Override
|
935 |
public Code le(Code op1, Code op2) {
|
936 |
return operator(OPERATOR_LE, op1, op2);
|
937 |
} |
938 |
|
939 |
@Override
|
940 |
public Code ge(Code op1, Code op2) {
|
941 |
return operator(OPERATOR_GE, op1, op2);
|
942 |
} |
943 |
|
944 |
@Override
|
945 |
public Code eq(Code op1, Code op2) {
|
946 |
return operator(OPERATOR_EQ, op1, op2);
|
947 |
} |
948 |
|
949 |
@Override
|
950 |
public Code ne(Code op1, Code op2) {
|
951 |
return operator(OPERATOR_NE, op1, op2);
|
952 |
} |
953 |
|
954 |
@Override
|
955 |
public Code is(Code op1, Code op2) {
|
956 |
return operator(OPERATOR_IS, op1, op2);
|
957 |
} |
958 |
|
959 |
@Override
|
960 |
public Code getattr(Code obj, String attrname) { |
961 |
BaseCodes args = args(); |
962 |
args.add(obj); |
963 |
args.add(constant(attrname)); |
964 |
return function(ExpressionBuilder.FUNCTION_GETATTR, args);
|
965 |
} |
966 |
|
967 |
@Override
|
968 |
public Code getitem(Code obj, Code index) {
|
969 |
BaseCodes args = args(); |
970 |
args.add(obj); |
971 |
args.add(index); |
972 |
return function(FUNCTION_GETITEM, args);
|
973 |
} |
974 |
|
975 |
@Override
|
976 |
public Code dict(Map<String,Code>map ) { |
977 |
BaseCodes args = args(); |
978 |
for (Map.Entry<String, Code> entry : map.entrySet()) { |
979 |
String key = entry.getKey();
|
980 |
Code value = entry.getValue(); |
981 |
args.add(constant(key)); |
982 |
args.add(value); |
983 |
} |
984 |
return function(FUNCTION_DICT, args);
|
985 |
} |
986 |
|
987 |
@Override
|
988 |
public Code $HostExpression(Code obj, String mode_specifier) { |
989 |
BaseCodes args = args(); |
990 |
args.add(obj); |
991 |
args.add(constant(mode_specifier)); |
992 |
return function(ExpressionBuilder.FUNCTION_$HOSTEXPRESSION, args); |
993 |
} |
994 |
|
995 |
} |