01: package kawa.standard;
02:
03: import kawa.lang.*;
04: import gnu.lists.*;
05: import gnu.mapping.*;
06: import gnu.mapping.Location; // As opposed to gnu.bytecode.Location.
07: import gnu.expr.*;
08: import gnu.bytecode.*;
09: import gnu.kawa.reflect.Invoke;
10:
11: /**
12: * The Syntax transformer that re-writes the Kawa "location" primitive.
13: * @author Per Bothner
14: */
15:
16: public class location extends Syntax {
17: public static final location location = new location();
18: static {
19: location.setName("location");
20: }
21:
22: public Expression rewrite(Object obj, Translator tr) {
23: if (!(obj instanceof Pair))
24: return tr.syntaxError("missing argument to location");
25: Pair pair = (Pair) obj;
26: if (pair.cdr != LList.Empty)
27: return tr.syntaxError("extra arguments to location");
28: // Expression arg = tr.rewrite(pair.car);
29: Expression[] args = { location
30: .rewrite(tr.rewrite(pair.car), tr) };
31: return Invoke.makeInvokeStatic(this Type, "makeLocationProc",
32: args);
33: }
34:
35: private static ClassType this Type = ClassType
36: .make("kawa.standard.location");
37:
38: public static Expression rewrite(Expression arg, Translator tr) {
39: if (arg instanceof ReferenceExp) {
40: ReferenceExp rexp = (ReferenceExp) arg;
41: rexp.setDontDereference(true);
42: Declaration decl = rexp.getBinding();
43: if (decl != null) {
44: if (decl.isLexical())
45: decl.setIndirectBinding(true);
46: decl = Declaration.followAliases(decl);
47: decl.setCanRead(true);
48: decl.setCanWrite(true);
49: }
50: return rexp;
51: }
52: if (arg instanceof ApplyExp) {
53: ApplyExp aexp = (ApplyExp) arg;
54: Expression[] args = new Expression[aexp.getArgs().length + 1];
55: args[0] = aexp.getFunction();
56: System.arraycopy(aexp.getArgs(), 0, args, 1,
57: args.length - 1);
58: return Invoke.makeInvokeStatic(this Type,
59: "makeProcLocation", args);
60: }
61: return tr.syntaxError("invalid argument to location");
62: }
63:
64: public static Location makeProcLocation$V(Procedure proc,
65: Object[] args) {
66: int nargs = args.length;
67: if (proc instanceof gnu.kawa.functions.ApplyToArgs && nargs > 0
68: && args[0] instanceof Procedure) // FIXME
69: {
70: proc = (Procedure) args[0];
71: if (proc instanceof LocationProc && nargs == 1)
72: return ((LocationProc) proc).getLocation();
73: Object[] rargs = new Object[nargs - 1];
74: System.arraycopy(args, 1, rargs, 0, rargs.length);
75: return new ProcLocation(proc, rargs);
76: }
77: if (proc instanceof LocationProc && nargs == 0)
78: return ((LocationProc) proc).getLocation();
79: return new ProcLocation(proc, args);
80: }
81:
82: public static Procedure makeLocationProc(Location loc) {
83: return new LocationProc(loc);
84: }
85: }
|