Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add support for imports, static calls, and constructors.

  • Loading branch information...
commit b0379404a1948200c8b0783ac91324a6acd99c77 1 parent 9675167
Charles Oliver Nutter authored September 05, 2009
12  lib/surinx/ast.rb
@@ -59,8 +59,13 @@ def build(body)
59 59
 
60 60
   def compile(compiler, expr)
61 61
     compiler.compile(receiver_node)
62  
-    compiler.compile(args_node)
63  
-    compiler.call name, args_node.size + 1, expr
  62
+    
  63
+    if args_node
  64
+      compiler.compile(args_node)
  65
+      compiler.call name, args_node.size + 1, expr
  66
+    else
  67
+      compiler.call name, 1, expr
  68
+    end
64 69
   end
65 70
 end
66 71
 
@@ -93,6 +98,9 @@ def compile(compiler, expr)
93 98
     elsif name == "print"
94 99
       compiler.compile(args_node)
95 100
       compiler.print expr
  101
+    elsif name == "import"
  102
+      compiler.import args_node.get(0).value
  103
+      compiler.this if expr
96 104
     else
97 105
       compiler.this
98 106
       compiler.compile(args_node)
12  lib/surinx/compiler.rb
@@ -131,7 +131,17 @@ def set_constant(name, body)
131 131
   end
132 132
   
133 133
   def get_constant(name)
134  
-    compile(instance_variable_get "@#{name}")
  134
+    value = instance_variable_get "@#{name}"
  135
+    if Module === value
  136
+      @mb.ldc(value)
  137
+    else
  138
+      compile(value)
  139
+    end
  140
+  end
  141
+  
  142
+  def import(name)
  143
+    short_name = name.split(/\./)[-1]
  144
+    instance_variable_set "@#{short_name}", eval(name)
135 145
   end
136 146
 
137 147
   def branch(condition, then_body, else_body, expr)
140  src/com/headius/surinx/SimpleJavaBootstrap.java
@@ -10,6 +10,8 @@
10 10
 import java.dyn.MethodHandle;
11 11
 import java.dyn.MethodHandles;
12 12
 import java.dyn.MethodType;
  13
+import java.lang.reflect.Constructor;
  14
+import java.lang.reflect.InvocationTargetException;
13 15
 import java.lang.reflect.Method;
14 16
 
15 17
 /**
@@ -135,23 +137,53 @@ public static Object fallback(CallSite site, Object receiver, Object[] args) thr
135 137
             }
136 138
             
137 139
             Class rClass = null;
138  
-            if (receiver == null) {
139  
-                rClass = site.callerClass();
140  
-                try {
141  
-                    rMethod = rClass.getMethod(site.name(), argTypes);
142  
-                    target = MethodHandles.lookup().unreflect(rMethod);
143  
-                    target = MethodHandles.dropArguments(target, 0, Object.class);
144  
-                    target = MethodHandles.convertArguments(target, site.type());
145  
-                } catch (NoSuchMethodException nsme) {
146  
-                    // hacky...try with all Object
147  
-                    for (int i = 0; i < argTypes.length; i++) {
148  
-                        argTypes[i] = Object.class;
149  
-                    }
150  
-                    rMethod = rClass.getMethod(site.name(), argTypes);
151  
-                    target = MethodHandles.lookup().unreflect(rMethod);
152  
-                    target = MethodHandles.dropArguments(target, 0, Object.class);
153  
-                    target = MethodHandles.convertArguments(target, site.type());
  140
+            if (receiver == null || receiver.getClass() == Class.class) {
  141
+				if (receiver == null) {
  142
+                	rClass = site.callerClass();
  143
+                } else {
  144
+	      			rClass = (Class)receiver;
154 145
                 }
  146
+
  147
+				if (site.name().equals("new")) {
  148
+					// constructor calls not working in MLVM right now
  149
+	                // try {
  150
+	                //     Constructor rConstructor = rClass.getConstructor(argTypes);
  151
+	                //     target = MethodHandles.lookup().unreflectConstructor(rConstructor);
  152
+	                // } catch (NoSuchMethodException nsme) {
  153
+	                //     // hacky...try with all Object
  154
+	                //     for (int i = 0; i < argTypes.length; i++) {
  155
+	                //         argTypes[i] = Object.class;
  156
+	                //     }
  157
+	                //     Constructor rConstructor = rClass.getConstructor(argTypes);
  158
+	                //     target = MethodHandles.lookup().unreflectConstructor(rConstructor);
  159
+	                // }
  160
+					
  161
+		            argTypes = new Class[args.length + 1];
  162
+		            argTypes[0] = Class.class;
  163
+		            for (int i = 0; i < args.length; i++) {
  164
+		                argTypes[i + 1] = Object.class;
  165
+		            }
  166
+		            rMethod = SimpleJavaBootstrap.class.getMethod("__new__", argTypes);
  167
+		            target = MethodHandles.lookup().unreflect(rMethod);
  168
+		            target = MethodHandles.convertArguments(target, site.type());
  169
+		
  170
+					site.setTarget(target);
  171
+					return __new__(rClass, args);
  172
+				} else {
  173
+	                try {
  174
+	                    rMethod = rClass.getMethod(site.name(), argTypes);
  175
+	                    target = MethodHandles.lookup().unreflect(rMethod);
  176
+	                } catch (NoSuchMethodException nsme) {
  177
+	                    // hacky...try with all Object
  178
+	                    for (int i = 0; i < argTypes.length; i++) {
  179
+	                        argTypes[i] = Object.class;
  180
+	                    }
  181
+	                    rMethod = rClass.getMethod(site.name(), argTypes);
  182
+	                    target = MethodHandles.lookup().unreflect(rMethod);
  183
+	                }	
  184
+	                target = MethodHandles.dropArguments(target, 0, Object.class);
  185
+                    target = MethodHandles.convertArguments(target, site.type());
  186
+				}
155 187
             } else {
156 188
                 rClass = receiver.getClass();
157 189
                 rMethod = rClass.getMethod(site.name(), argTypes);
@@ -305,4 +337,80 @@ public static final Boolean equals(Double a, Integer b) {
305 337
     public static final Boolean equals(Boolean a, Boolean b) {
306 338
         return a == b;
307 339
     }
  340
+
  341
+	public static final Object __new__(Class target) {
  342
+		try {
  343
+			return target.newInstance();
  344
+		} catch (InstantiationException ie) {
  345
+			throw new RuntimeException(ie);
  346
+		} catch (IllegalAccessException iae) {
  347
+			throw new RuntimeException(iae);
  348
+		}
  349
+	}
  350
+
  351
+	public static final Object __new__(Class target, Object arg0) {
  352
+		Constructor c;
  353
+		try {
  354
+			c = target.getConstructor(arg0.getClass());
  355
+			return c.newInstance(arg0);
  356
+		} catch (NoSuchMethodException nsme) {
  357
+			throw new RuntimeException(nsme);
  358
+		} catch (InstantiationException ie) {
  359
+			throw new RuntimeException(ie);
  360
+		} catch (IllegalAccessException iae) {
  361
+			throw new RuntimeException(iae);
  362
+		} catch (InvocationTargetException ite) {
  363
+			throw new RuntimeException(ite);
  364
+		}
  365
+	}
  366
+
  367
+	public static final Object __new__(Class target, Object arg0, Object arg1) {
  368
+		Constructor c;
  369
+		try {
  370
+			c = target.getConstructor(arg0.getClass(), arg1.getClass());
  371
+			return c.newInstance(arg0, arg1);
  372
+		} catch (NoSuchMethodException nsme) {
  373
+			throw new RuntimeException(nsme);
  374
+		} catch (InstantiationException ie) {
  375
+			throw new RuntimeException(ie);
  376
+		} catch (IllegalAccessException iae) {
  377
+			throw new RuntimeException(iae);
  378
+		} catch (InvocationTargetException ite) {
  379
+			throw new RuntimeException(ite);
  380
+		}
  381
+	}
  382
+
  383
+	public static final Object __new__(Class target, Object arg0, Object arg1, Object arg2) {
  384
+		Constructor c;
  385
+		try {
  386
+			c = target.getConstructor(arg0.getClass(), arg1.getClass(), arg2.getClass());
  387
+			return c.newInstance(arg0, arg1, arg2);
  388
+		} catch (NoSuchMethodException nsme) {
  389
+			throw new RuntimeException(nsme);
  390
+		} catch (InstantiationException ie) {
  391
+			throw new RuntimeException(ie);
  392
+		} catch (IllegalAccessException iae) {
  393
+			throw new RuntimeException(iae);
  394
+		} catch (InvocationTargetException ite) {
  395
+			throw new RuntimeException(ite);
  396
+		}
  397
+	}
  398
+
  399
+	public static final Object __new__(Class target, Object[] args) {
  400
+		Class[] argTypes = new Class[args.length];
  401
+		for (int i = 0; i < args.length; i++) argTypes[i] = args[i].getClass();
  402
+		Constructor c;
  403
+		try {
  404
+			c = target.getConstructor(argTypes);
  405
+			return c.newInstance(args);
  406
+		} catch (NoSuchMethodException nsme) {
  407
+			throw new RuntimeException(nsme);
  408
+		} catch (InstantiationException ie) {
  409
+			throw new RuntimeException(ie);
  410
+		} catch (IllegalAccessException iae) {
  411
+			throw new RuntimeException(iae);
  412
+		} catch (InvocationTargetException ite) {
  413
+			throw new RuntimeException(ite);
  414
+		}
  415
+	}
308 416
 }
3  test/test_constructor.sx
... ...
@@ -0,0 +1,3 @@
  1
+import 'java.lang.String'
  2
+
  3
+puts String.new('foo') == 'foo'
2  test/test_import.sx
... ...
@@ -0,0 +1,2 @@
  1
+import 'java.lang.String'
  2
+puts String == 'foo'.getClass
4  test/test_static.sx
... ...
@@ -0,0 +1,4 @@
  1
+import 'java.lang.System'
  2
+
  3
+System.setProperty 'foo', 'bar'
  4
+puts System.getProperty('foo') == 'bar'

0 notes on commit b037940

Please sign in to comment.
Something went wrong with that request. Please try again.