| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.interpreter.evaluator.values; |
|---|
| 19 | |
|---|
| 20 | import com.sun.fortress.interpreter.evaluator.Environment; |
|---|
| 21 | import com.sun.fortress.interpreter.evaluator.types.FType; |
|---|
| 22 | import com.sun.fortress.nodes_util.NodeFactory; |
|---|
| 23 | import com.sun.fortress.useful.BATreeEC; |
|---|
| 24 | import com.sun.fortress.useful.Useful; |
|---|
| 25 | |
|---|
| 26 | import java.util.ArrayList; |
|---|
| 27 | import java.util.Collections; |
|---|
| 28 | import java.util.List; |
|---|
| 29 | import java.util.Set; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | public class OverloadedMethod extends OverloadedFunction implements Method { |
|---|
| 33 | |
|---|
| 34 | BATreeEC<List<FValue>, List<FType>, MethodClosure> mcache = new BATreeEC<List<FValue>, List<FType>, MethodClosure>( |
|---|
| 35 | FValue.asTypesList); |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | public OverloadedMethod(String fnName, Environment within) { |
|---|
| 39 | super(NodeFactory.makeId(NodeFactory.interpreterSpan, fnName), within); |
|---|
| 40 | |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public OverloadedMethod(String fnName, Set<? extends Simple_fcn> ssf, Environment within) { |
|---|
| 44 | super(NodeFactory.makeId(NodeFactory.interpreterSpan, fnName), ssf, within); |
|---|
| 45 | |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | public MethodClosure getApplicableMethod(List<FValue> args) { |
|---|
| 54 | MethodClosure best_f = mcache.get(args); |
|---|
| 55 | if (best_f == null) { |
|---|
| 56 | best_f = (MethodClosure) bestMatch(args, overloads); |
|---|
| 57 | } |
|---|
| 58 | return best_f; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | public FValue applyMethod(FObject selfValue, List<FValue> args) { |
|---|
| 62 | Method best_f = getApplicableMethod(args); |
|---|
| 63 | return best_f.applyMethod(selfValue, args); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | public FValue applyMethod(FObject self) { |
|---|
| 67 | return applyMethod(self, Collections.<FValue>emptyList()); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | public FValue applyMethod(FObject self, FValue a) { |
|---|
| 71 | return applyMethod(self, Collections.singletonList(a)); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | public FValue applyMethod(FObject self, FValue a, FValue b) { |
|---|
| 75 | return applyMethod(self, Useful.list(a, b)); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public FValue applyMethod(FObject self, FValue a, FValue b, FValue c) { |
|---|
| 79 | return applyMethod(self, Useful.list(a, b, c)); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | public void bless() { |
|---|
| 83 | overloads = pendingOverloads; |
|---|
| 84 | pendingOverloads = new ArrayList<Overload>(); |
|---|
| 85 | super.bless(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | } |
|---|