001: package gnu.expr;
002:
003: import gnu.mapping.*;
004:
005: public abstract class ModuleWithContext extends ModuleBody {
006: public int match0(ModuleMethod proc, CallContext ctx) {
007: int num = proc.numArgs();
008: int min = num & 0xFFF;
009: if (min > 0)
010: return MethodProc.NO_MATCH_TOO_FEW_ARGS | min;
011: ctx.count = 0;
012: ctx.where = 0;
013: if (num < 0)
014: return matchN(proc, ProcedureN.noArgs, ctx);
015: ctx.next = 0;
016: ctx.proc = this ;
017: ctx.pc = proc.selector;
018: return 0;
019: }
020:
021: public int match1(ModuleMethod proc, Object arg1, CallContext ctx) {
022: int num = proc.numArgs();
023: int min = num & 0xFFF;
024: if (min > 1)
025: return MethodProc.NO_MATCH_TOO_FEW_ARGS | min;
026: if (num >= 0) {
027: int max = num >> 12;
028: if (max < 1)
029: return MethodProc.NO_MATCH_TOO_MANY_ARGS | max;
030: ctx.value1 = arg1;
031: ctx.count = 1;
032: ctx.where = CallContext.ARG_IN_VALUE1;
033: ctx.next = 0;
034: ctx.proc = this ;
035: ctx.pc = proc.selector;
036: return 0;
037: }
038: ctx.where = 0;
039: Object[] args = { arg1 };
040: return matchN(proc, args, ctx);
041: }
042:
043: public int match2(ModuleMethod proc, Object arg1, Object arg2,
044: CallContext ctx) {
045: int num = proc.numArgs();
046: int min = num & 0xFFF;
047: if (min > 2)
048: return MethodProc.NO_MATCH_TOO_FEW_ARGS | min;
049: if (num >= 0) {
050: int max = num >> 12;
051: if (max < 2)
052: return MethodProc.NO_MATCH_TOO_MANY_ARGS | max;
053: ctx.value1 = arg1;
054: ctx.value2 = arg2;
055: ctx.count = 2;
056: ctx.where = CallContext.ARG_IN_VALUE1
057: | (CallContext.ARG_IN_VALUE2 << 4);
058: ctx.next = 0;
059: ctx.proc = this ;
060: ctx.pc = proc.selector;
061: return 0;
062: }
063: ctx.where = 0;
064: Object[] args = { arg1, arg2 };
065: return matchN(proc, args, ctx);
066: }
067:
068: public int match3(ModuleMethod proc, Object arg1, Object arg2,
069: Object arg3, CallContext ctx) {
070: int num = proc.numArgs();
071: int min = num & 0xFFF;
072: if (min > 3)
073: return MethodProc.NO_MATCH_TOO_FEW_ARGS | min;
074: if (num >= 0) {
075: int max = num >> 12;
076: if (max < 3)
077: return MethodProc.NO_MATCH_TOO_MANY_ARGS | max;
078: ctx.value1 = arg1;
079: ctx.value2 = arg2;
080: ctx.value3 = arg3;
081: ctx.count = 3;
082: ctx.where = CallContext.ARG_IN_VALUE1
083: | (CallContext.ARG_IN_VALUE2 << 4)
084: | (CallContext.ARG_IN_VALUE3 << 8);
085: ctx.next = 0;
086: ctx.proc = this ;
087: ctx.pc = proc.selector;
088: return 0;
089: }
090: ctx.where = 0;
091: Object[] args = { arg1, arg2, arg3 };
092: return matchN(proc, args, ctx);
093: }
094:
095: public int match4(ModuleMethod proc, Object arg1, Object arg2,
096: Object arg3, Object arg4, CallContext ctx) {
097: int num = proc.numArgs();
098: int min = num & 0xFFF;
099: if (min > 4)
100: return MethodProc.NO_MATCH_TOO_FEW_ARGS | min;
101: if (num >= 0) {
102: int max = num >> 12;
103: if (max < 4)
104: return MethodProc.NO_MATCH_TOO_MANY_ARGS | max;
105: ctx.value1 = arg1;
106: ctx.value2 = arg2;
107: ctx.value3 = arg3;
108: ctx.value4 = arg4;
109: ctx.count = 4;
110: ctx.where = (CallContext.ARG_IN_VALUE1
111: | (CallContext.ARG_IN_VALUE2 << 4)
112: | (CallContext.ARG_IN_VALUE3 << 8) | (CallContext.ARG_IN_VALUE4 << 12));
113: ctx.next = 0;
114: ctx.proc = this ;
115: ctx.pc = proc.selector;
116: return 0;
117: }
118: ctx.where = 0;
119: Object[] args = { arg1, arg2, arg3, arg4 };
120: return matchN(proc, args, ctx);
121: }
122:
123: public int matchN(ModuleMethod proc, Object[] args, CallContext ctx) {
124: int num = proc.numArgs();
125: int min = num & 0xFFF;
126: if (args.length < min)
127: return MethodProc.NO_MATCH_TOO_FEW_ARGS | min;
128: if (num >= 0) {
129: switch (args.length) {
130: case 0:
131: return match0(proc, ctx);
132: case 1:
133: return match1(proc, args[0], ctx);
134: case 2:
135: return match2(proc, args[0], args[1], ctx);
136: case 3:
137: return match3(proc, args[0], args[1], args[2], ctx);
138: case 4:
139: return match4(proc, args[0], args[1], args[2], args[3],
140: ctx);
141: default:
142: int max = num >> 12;
143: if (args.length > max)
144: return MethodProc.NO_MATCH_TOO_MANY_ARGS | max;
145: }
146: }
147: ctx.values = args;
148: ctx.count = args.length;
149: ctx.where = 0;
150: ctx.next = 0;
151: ctx.proc = this ;
152: ctx.pc = proc.selector;
153: return 0;
154: }
155:
156: public Object apply0(ModuleMethod method) throws Throwable {
157: CallContext ctx = CallContext.getInstance();
158: method.check0(ctx);
159: return ctx.runUntilValue();
160: }
161:
162: public Object apply1(ModuleMethod method, Object arg1)
163: throws Throwable {
164: CallContext ctx = CallContext.getInstance();
165: method.check1(arg1, ctx);
166: return ctx.runUntilValue();
167: }
168:
169: public Object apply2(ModuleMethod method, Object arg1, Object arg2)
170: throws Throwable {
171: CallContext ctx = CallContext.getInstance();
172: method.check2(arg1, arg2, ctx);
173: return ctx.runUntilValue();
174: }
175:
176: public Object apply3(ModuleMethod method, Object arg1, Object arg2,
177: Object arg3) throws Throwable {
178: CallContext ctx = CallContext.getInstance();
179: method.check3(arg1, arg2, arg3, ctx);
180: return ctx.runUntilValue();
181: }
182:
183: public Object apply4(ModuleMethod method, Object arg1, Object arg2,
184: Object arg3, Object arg4) throws Throwable {
185: CallContext ctx = CallContext.getInstance();
186: method.check4(arg1, arg2, arg3, arg4, ctx);
187: return ctx.runUntilValue();
188: }
189:
190: public Object applyN(ModuleMethod method, Object[] args)
191: throws Throwable {
192: CallContext ctx = CallContext.getInstance();
193: method.checkN(args, ctx);
194: return ctx.runUntilValue();
195: }
196: }
|