01: package gnu.expr;
02:
03: import gnu.mapping.Procedure;
04:
05: public class InlineCalls extends ExpWalker {
06: public static void inlineCalls(Expression exp) {
07: InlineCalls walker = new InlineCalls();
08: exp.walk(walker);
09: //or: walter.walkExpression(exp);
10: }
11:
12: protected Expression walkApplyExp(ApplyExp exp) {
13: super .walkApplyExp(exp);
14: LambdaExp lambda = null;
15: if (exp.func instanceof LambdaExp)
16: lambda = (LambdaExp) exp.func;
17: if (exp.func instanceof QuoteExp) {
18: Object proc = ((QuoteExp) exp.func).getValue();
19: if (proc instanceof CanInline) {
20: return ((CanInline) proc).inline(exp);
21: }
22: }
23: if (exp.func instanceof ReferenceExp) {
24: Declaration decl = ((ReferenceExp) exp.func).binding;
25: if (decl != null && !decl.getFlag(Declaration.IS_UNKNOWN)) {
26: Object proc = decl.getValue();
27: if (proc instanceof LambdaExp)
28: lambda = (LambdaExp) proc;
29: if (proc instanceof QuoteExp)
30: proc = ((QuoteExp) proc).getValue();
31: if (proc instanceof CanInline)
32: return ((CanInline) proc).inline(exp);
33: // if (proc instanceof Procedure)
34: // {
35: // PrimProcedure mproc
36: // = PrimProcedure.getMethodFor((Procedure) proc, exp.args);
37: // if (mproc != null)
38: // return new QuoteExp(mproc);
39: // }
40: }
41: }
42: if (lambda != null) {
43: int args_length = exp.args.length;
44: String msg = null;
45: if (args_length < lambda.min_args)
46: msg = "too few args for ";
47: else if (lambda.max_args >= 0
48: && args_length > lambda.max_args)
49: msg = "too many args " + args_length + " for ";
50: // FIXME make error message
51: if (msg != null)
52: System.err.println("bad call: " + msg
53: + lambda.getName());
54: }
55: return exp;
56: }
57:
58: protected Expression walkSetExp(SetExp exp) {
59: Declaration decl = exp.binding;
60: boolean updateNeeded = false;
61: if (decl != null) {
62: Expression declValue = decl.getValue();
63: if (declValue == exp.new_value)
64: updateNeeded = true;
65: }
66: exp.walkChildren(this );
67: if (updateNeeded) {
68: decl.value = exp.new_value;
69: if (exp.new_value instanceof LambdaExp)
70: ((LambdaExp) exp.new_value).nameDecl = decl;
71: }
72: return exp;
73: }
74: }
|