1 21 22 package org.armedbear.lisp; 23 24 public final class GenericFunction extends StandardObject 25 { 26 private LispObject discriminatingFunction; 27 28 public GenericFunction(LispClass cls, SimpleVector slots) 29 { 30 super(cls, slots); 31 } 32 33 public LispObject getDiscriminatingFunction() 34 { 35 return discriminatingFunction; 36 } 37 38 public void setDiscriminatingFunction(LispObject function) 39 { 40 discriminatingFunction = function; 41 } 42 43 public LispObject execute() throws ConditionThrowable 44 { 45 LispObject[] args = new LispObject[0]; 46 return execute(args); 47 } 48 49 public LispObject execute(LispObject arg) throws ConditionThrowable 50 { 51 LispObject[] args = new LispObject[1]; 52 args[0] = arg; 53 return execute(args); 54 } 55 56 public LispObject execute(LispObject first, LispObject second) 57 throws ConditionThrowable 58 { 59 LispObject[] args = new LispObject[2]; 60 args[0] = first; 61 args[1] = second; 62 return execute(args); 63 } 64 65 public LispObject execute(LispObject first, LispObject second, 66 LispObject third) 67 throws ConditionThrowable 68 { 69 LispObject[] args = new LispObject[3]; 70 args[0] = first; 71 args[1] = second; 72 args[2] = third; 73 return execute(args); 74 } 75 76 public LispObject execute(LispObject first, LispObject second, 77 LispObject third, LispObject fourth) 78 throws ConditionThrowable 79 { 80 LispObject[] args = new LispObject[4]; 81 args[0] = first; 82 args[1] = second; 83 args[2] = third; 84 args[3] = fourth; 85 return execute(args); 86 } 87 88 public LispObject execute(LispObject[] args) throws ConditionThrowable 89 { 90 return funcall(getDiscriminatingFunction(), args, LispThread.currentThread()); 91 } 92 93 public String writeToString() throws ConditionThrowable 94 { 95 LispObject slots = getSlots(); 96 if (slots instanceof AbstractVector) { 97 AbstractVector v = (AbstractVector) slots; 98 if (v.length() > 0) { 99 LispObject name = v.getRowMajor(0); 100 if (name != null) { 101 StringBuffer sb = new StringBuffer ("#<"); 102 sb.append(getLispClass().getSymbol().writeToString()); 103 sb.append(' '); 104 sb.append(name.writeToString()); 105 sb.append('>'); 106 return sb.toString(); 107 } 108 } 109 } 110 return super.writeToString(); 111 } 112 113 private int callCount; 115 116 public final int getCallCount() 117 { 118 return callCount; 119 } 120 121 public void setCallCount(int n) 122 { 123 callCount = n; 124 } 125 126 public final void incrementCallCount() 127 { 128 ++callCount; 129 } 130 131 private static final Primitive1 GENERIC_FUNCTION_DISCRIMINATING_FUNCTION = 132 new Primitive1("generic-function-discriminating-function", PACKAGE_SYS, false) 133 { 134 public LispObject execute(LispObject arg) throws ConditionThrowable 135 { 136 if (arg instanceof GenericFunction) 137 return ((GenericFunction)arg).getDiscriminatingFunction(); 138 return signal(new TypeError(arg, "generic function")); 139 } 140 }; 141 142 private static final Primitive1 _SET_GENERIC_FUNCTION_DISCRIMINATING_FUNCTION = 143 new Primitive1("%set-generic-function-discriminating-function", PACKAGE_SYS, false) 144 { 145 public LispObject execute(LispObject first, LispObject second) 146 throws ConditionThrowable 147 { 148 if (first instanceof GenericFunction) { 149 ((GenericFunction)first).setDiscriminatingFunction(second); 150 return second; 151 } 152 return signal(new TypeError(first, "generic function")); 153 } 154 }; 155 } 156 | Popular Tags |