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