1 package gnu.expr; 2 import gnu.mapping.*; 3 4 public abstract class ModuleWithContext extends ModuleBody 5 { 6 public int match0 (ModuleMethod proc, CallContext ctx) 7 { 8 int num = proc.numArgs(); 9 int min = num & 0xFFF; 10 if (min > 0) 11 return MethodProc.NO_MATCH_TOO_FEW_ARGS|min; 12 ctx.count = 0; 13 ctx.where = 0; 14 if (num < 0) 15 return matchN(proc, ProcedureN.noArgs, ctx); 16 ctx.next = 0; 17 ctx.proc = this; ctx.pc = proc.selector; 18 return 0; 19 } 20 21 public int match1 (ModuleMethod proc, Object arg1, CallContext ctx) 22 { 23 int num = proc.numArgs(); 24 int min = num & 0xFFF; 25 if (min > 1) 26 return MethodProc.NO_MATCH_TOO_FEW_ARGS|min; 27 if (num >= 0) 28 { 29 int max = num >> 12; 30 if (max < 1) 31 return MethodProc.NO_MATCH_TOO_MANY_ARGS|max; 32 ctx.value1 = arg1; 33 ctx.count = 1; 34 ctx.where = CallContext.ARG_IN_VALUE1; 35 ctx.next = 0; 36 ctx.proc = this; ctx.pc = proc.selector; 37 return 0; 38 } 39 ctx.where = 0; 40 Object [] args = { arg1 }; 41 return matchN(proc, args, ctx); 42 } 43 44 public int match2 (ModuleMethod proc, Object arg1, Object arg2, 45 CallContext ctx) 46 { 47 int num = proc.numArgs(); 48 int min = num & 0xFFF; 49 if (min > 2) 50 return MethodProc.NO_MATCH_TOO_FEW_ARGS|min; 51 if (num >= 0) 52 { 53 int max = num >> 12; 54 if (max < 2) 55 return MethodProc.NO_MATCH_TOO_MANY_ARGS|max; 56 ctx.value1 = arg1; 57 ctx.value2 = arg2; 58 ctx.count = 2; 59 ctx.where = CallContext.ARG_IN_VALUE1 60 |(CallContext.ARG_IN_VALUE2<<4); 61 ctx.next = 0; 62 ctx.proc = this; ctx.pc = proc.selector; 63 return 0; 64 } 65 ctx.where = 0; 66 Object [] args = { arg1, arg2 }; 67 return matchN(proc, args, ctx); 68 } 69 70 public int match3 (ModuleMethod proc, Object arg1, Object arg2, Object arg3, 71 CallContext ctx) 72 { 73 int num = proc.numArgs(); 74 int min = num & 0xFFF; 75 if (min > 3) 76 return MethodProc.NO_MATCH_TOO_FEW_ARGS|min; 77 if (num >= 0) 78 { 79 int max = num >> 12; 80 if (max < 3) 81 return MethodProc.NO_MATCH_TOO_MANY_ARGS|max; 82 ctx.value1 = arg1; 83 ctx.value2 = arg2; 84 ctx.value3 = arg3; 85 ctx.count = 3; 86 ctx.where = CallContext.ARG_IN_VALUE1 87 |(CallContext.ARG_IN_VALUE2<<4) 88 |(CallContext.ARG_IN_VALUE3<<8); 89 ctx.next = 0; 90 ctx.proc = this; ctx.pc = proc.selector; 91 return 0; 92 } 93 ctx.where = 0; 94 Object [] args = { arg1, arg2, arg3 }; 95 return matchN(proc, args, ctx); 96 } 97 98 public int match4 (ModuleMethod proc, Object arg1, Object arg2, 99 Object arg3, Object arg4, CallContext ctx) 100 { 101 int num = proc.numArgs(); 102 int min = num & 0xFFF; 103 if (min > 4) 104 return MethodProc.NO_MATCH_TOO_FEW_ARGS|min; 105 if (num >= 0) 106 { 107 int max = num >> 12; 108 if (max < 4) 109 return MethodProc.NO_MATCH_TOO_MANY_ARGS|max; 110 ctx.value1 = arg1; 111 ctx.value2 = arg2; 112 ctx.value3 = arg3; 113 ctx.value4 = arg4; 114 ctx.count = 4; 115 ctx.where = (CallContext.ARG_IN_VALUE1 116 |(CallContext.ARG_IN_VALUE2<<4) 117 |(CallContext.ARG_IN_VALUE3<<8) 118 |(CallContext.ARG_IN_VALUE4<<12)); 119 ctx.next = 0; 120 ctx.proc = this; ctx.pc = proc.selector; 121 return 0; 122 } 123 ctx.where = 0; 124 Object [] args = { arg1, arg2, arg3, arg4 }; 125 return matchN(proc, args, ctx); 126 } 127 128 public int matchN (ModuleMethod proc, Object [] args, CallContext ctx) 129 { 130 int num = proc.numArgs(); 131 int min = num & 0xFFF; 132 if (args.length < min) 133 return MethodProc.NO_MATCH_TOO_FEW_ARGS|min; 134 if (num >= 0) 135 { 136 switch (args.length) 137 { 138 case 0: 139 return match0(proc, ctx); 140 case 1: 141 return match1(proc, args[0], ctx); 142 case 2: 143 return match2(proc, args[0], args[1], ctx); 144 case 3: 145 return match3(proc, args[0], args[1], args[2], ctx); 146 case 4: 147 return match4(proc, args[0], args[1], args[2], args[3], ctx); 148 default: 149 int max = num >> 12; 150 if (args.length > max) 151 return MethodProc.NO_MATCH_TOO_MANY_ARGS|max; 152 } 153 } 154 ctx.values = args; 155 ctx.count = args.length; 156 ctx.where = 0; 157 ctx.next = 0; 158 ctx.proc = this; ctx.pc = proc.selector; 159 return 0; 160 } 161 162 public Object apply0 (ModuleMethod method) 163 throws Throwable 164 { 165 CallContext ctx = CallContext.getInstance(); 166 method.check0(ctx); 167 return ctx.runUntilValue(); 168 } 169 170 public Object apply1 (ModuleMethod method, Object arg1) 171 throws Throwable 172 { 173 CallContext ctx = CallContext.getInstance(); 174 method.check1(arg1, ctx); 175 return ctx.runUntilValue(); 176 } 177 178 public Object apply2 (ModuleMethod method, Object arg1, Object arg2) 179 throws Throwable 180 { 181 CallContext ctx = CallContext.getInstance(); 182 method.check2(arg1, arg2, ctx); 183 return ctx.runUntilValue(); 184 } 185 186 public Object apply3 (ModuleMethod method, 187 Object arg1, Object arg2, Object arg3) 188 throws Throwable 189 { 190 CallContext ctx = CallContext.getInstance(); 191 method.check3(arg1, arg2, arg3, ctx); 192 return ctx.runUntilValue(); 193 } 194 195 public Object apply4 (ModuleMethod method, 196 Object arg1, Object arg2, Object arg3, Object arg4) 197 throws Throwable 198 { 199 CallContext ctx = CallContext.getInstance(); 200 method.check4(arg1, arg2, arg3, arg4, ctx); 201 return ctx.runUntilValue(); 202 } 203 204 public Object applyN (ModuleMethod method, Object [] args) 205 throws Throwable 206 { 207 CallContext ctx = CallContext.getInstance(); 208 method.checkN(args, ctx); 209 return ctx.runUntilValue(); 210 } 211 } 212 | Popular Tags |