01: // Copyright (c) 2001, 2002 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.functions;
05:
06: import gnu.lists.*;
07: import gnu.mapping.*;
08: import gnu.bytecode.*;
09: import gnu.expr.*;
10:
11: public class AppendValues extends MethodProc implements CanInline,
12: Inlineable {
13: public static final AppendValues appendValues = new AppendValues();
14:
15: public void apply(CallContext ctx) {
16: Object endMarker = Special.dfault;
17: for (;;) {
18: Object arg = ctx.getNextArg(endMarker);
19: if (arg == endMarker)
20: break;
21: if (arg instanceof Consumable)
22: ((Consumable) arg).consume(ctx.consumer);
23: else
24: ctx.writeValue(arg);
25: }
26: }
27:
28: public Expression inline(ApplyExp exp, ExpWalker walker) {
29: Expression[] args = exp.getArgs();
30: if (args.length == 1)
31: return args[0];
32: if (args.length == 0)
33: return QuoteExp.voidExp;
34: Expression folded = exp.inlineIfConstant(this , walker);
35: if (folded != exp)
36: return folded;
37: return exp;
38: }
39:
40: public void compile(ApplyExp exp, Compilation comp, Target target) {
41: Expression[] args = exp.getArgs();
42: int nargs = args.length;
43: if (target instanceof ConsumerTarget
44: || target instanceof IgnoreTarget) {
45: for (int i = 0; i < nargs; i++)
46: args[i].compileWithPosition(comp, target);
47: } else if (target instanceof SeriesTarget && nargs > 0) {
48: CodeAttr code = comp.getCode();
49: SeriesTarget serTarget = (SeriesTarget) target;
50: Label done = serTarget.done;
51: serTarget.done = null;
52: for (int i = 0; i < nargs; i++) {
53: if (i + 1 == nargs) // Last one
54: serTarget.done = done;
55: args[i].compileWithPosition(comp, serTarget);
56: }
57: } else {
58: ConsumerTarget.compileUsingConsumer(exp, comp, target);
59: /*
60: CodeAttr code = comp.getCode();
61: Scope scope = code.pushScope();
62: Variable values = scope.addVariable(code, comp.typeValues, null);
63: ConsumerTarget ctarget = new ConsumerTarget(values);
64: code.emitInvokeStatic(comp.typeValues.getDeclaredMethod("make", 0));
65: code.emitStore(values);
66: for (int i = 0; i < nargs; i++)
67: args[i].compile(comp, ctarget);
68: code.emitLoad(values);
69: code.popScope();
70: target.compileFromStack(comp, Compilation.typeValues);
71: */
72: }
73: }
74:
75: public Type getReturnType(Expression[] args) {
76: return Compilation.typeObject;
77: }
78: }
|