Commits

Tim Vernum committed 185ea27

Import from private branch (rev:39143fe1ffd9)

  • Participants
  • Parent commits c183f2a

Comments (0)

Files changed (11)

File modules/lpc/source/java/compiler/us/terebi/lang/lpc/compiler/bytecode/ExpressionCompiler.java

     public Object visit(ASTCompoundExpression node, Object data)
     {
         LpcExpression var = null;
+        List<ElementBuilder<? extends Statement>> statements = new ArrayList<ElementBuilder<? extends Statement>>();
         for (TokenNode child : ASTUtil.children(node))
         {
+            if(var != null) {
+                statements.add(VM.Statement.ignore(getValue(var)));
+            }
             var = compile(child);
         }
-        return var;
+        Expression expression = VM.Expression.chain(statements, getValue(var));
+        return expression(expression, var.type);
     }
 
     public CompileContext getContext()

File modules/lpc/source/java/compiler/us/terebi/lang/lpc/compiler/bytecode/FunctionLiteralCompiler.java

 
 
         Expression value = VM.Expression.construct(VM.Method.constructor(FunctionValue.class, Callable.class), callable);
-        return new LpcExpression(function.signature.getReturnType(), value);
+        return new LpcExpression(Types.FUNCTION, value);
     }
 
     private LpcExpression compile(ExpressionNode exprNode, ExpressionCompiler compiler)

File modules/lpc/source/java/compiler/us/terebi/lang/lpc/runtime/jvm/object/CompiledDefinition.java

             LpcMember annotation = method.getAnnotation(LpcMember.class);
             if (annotation != null)
             {
-//                try
-//                {
-//                    method = iface.getMethod(method.getName(), method.getParameterTypes());
-                    _methods.put(annotation.name(), new CompiledMethod(this, method, _lookup));
-//                }
-//                catch (SecurityException e)
-//                {
-//                    throw new InternalError(e);
-//                }
-//                catch (NoSuchMethodException e)
-//                {
-//                    throw new InternalError(e);
-//                }
+                _methods.put(annotation.name(), new CompiledMethod(this, method, _lookup));
             }
         }
 

File modules/lpc/source/java/compiler/us/terebi/lang/lpc/runtime/jvm/object/CompiledMethod.java

 import us.terebi.lang.lpc.runtime.LpcValue;
 import us.terebi.lang.lpc.runtime.MemberDefinition;
 import us.terebi.lang.lpc.runtime.ObjectInstance;
+import us.terebi.lang.lpc.runtime.jvm.LpcConstants;
 import us.terebi.lang.lpc.runtime.jvm.LpcMember;
 import us.terebi.lang.lpc.runtime.jvm.LpcMemberType;
 import us.terebi.lang.lpc.runtime.jvm.LpcParameter;
         Class[] interfaces = method.getDeclaringClass().getInterfaces();
         assert interfaces.length == 1;
         Class iface = interfaces[0];
-        
-      try
-      {
-          // Look for method on interface to support polymorphism
-          method = iface.getMethod(method.getName(), method.getParameterTypes());
-      }
-      catch (SecurityException e)
-      {
-          throw new InternalError(e);
-      }
-      catch (NoSuchMethodException e)
-      {
-          // Ignore
-      }
-      _method = method;
+
+        try
+        {
+            // Look for method on interface to support polymorphism
+            method = iface.getMethod(method.getName(), method.getParameterTypes());
+        }
+        catch (SecurityException e)
+        {
+            throw new InternalError(e);
+        }
+        catch (NoSuchMethodException e)
+        {
+            // Ignore
+        }
+        _method = method;
     }
 
     private FunctionSignature resolveSignature(Method method)
     private LpcValue executeMethod(CompiledObjectInstance instance, List< ? extends LpcValue> arguments)
     {
         Object object = instance.getImplementingObject();
-        if (arguments.size() < _method.getParameterTypes().length)
+        final int requiredArgumentCount = _method.getParameterTypes().length;
+        if (arguments.size() < requiredArgumentCount)
         {
             if (this._signature.acceptsLessArguments())
             {
                 List<LpcValue> args = new ArrayList<LpcValue>(arguments);
-                for (int i = arguments.size(); i < _method.getParameterTypes().length; i++)
+                for (int i = arguments.size(); i < requiredArgumentCount; i++)
                 {
-                    args.add(NilValue.INSTANCE);
+                    if (this._signature.getArguments().get(i).isVarArgs())
+                    {
+                        args.add(LpcConstants.ARRAY.EMPTY);
+                    }
+                    else
+                    {
+                        args.add(NilValue.INSTANCE);
+                    }
                 }
                 arguments = args;
             }
                 throw new IllegalArgumentException("Wrong argument count to "
                         + this
                         + " (expected "
-                        + _method.getParameterTypes().length
+                        + requiredArgumentCount
                         + ", got "
                         + arguments.size()
                         + ")");
             }
         }
+        if (arguments.size() > requiredArgumentCount)
+        {
+            arguments = arguments.subList(0, requiredArgumentCount);
+        }
         try
         {
             Object result = _method.invoke(object, arguments.toArray());

File modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/efun/file/GetDirectoryInfoEfun.java

                 array.add(getInfo(resource, longListing));
             }
         }
-        
+
         return array;
     }
 
             return new ArrayValue(Types.MIXED_ARRAY, //
                     new StringValue(resource.getPath()), new IntValue(resource.getSizeInBytes()), new IntValue(resource.lastModified()));
         }
-        return null;
+        else
+        {
+            return new StringValue(resource.getPath());
+        }
     }
 
     private Resource[] resolveWildCard(String path) throws IOException

File modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/support/MathSupport.java

     {
         if (isInteger(left) && isInteger(right))
         {
-            return intValue(left.asLong() / right.asLong());
+            final long divisor = right.asLong();
+            if (divisor == 0)
+            {
+                throw new LpcRuntimeException("Division by zero");
+            }
+            return intValue(left.asLong() / divisor);
         }
         if (isNumber(left) && isNumber(right))
         {

File modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/support/MiscSupport.java

 
     public static boolean isNumber(LpcType type)
     {
-        return Types.INT.equals(type) || Types.FLOAT.equals(type);
+        return Types.INT.equals(type) || Types.FLOAT.equals(type) || Types.NIL.equals(type);
     }
 
     public static boolean isFunction(LpcValue value)

File modules/lpc/source/java/runtime/us/terebi/lang/lpc/runtime/jvm/support/ValueSupport.java

         Set<LpcType> types = new HashSet<LpcType>();
         for (LpcValue lpcValue : elements)
         {
+            if (lpcValue == null)
+            {
+                throw new IllegalArgumentException("Cannot put null values into an array (" + elements + ")");
+            }
             types.add(lpcValue.getActualType());
         }
 

File modules/lpc/source/java/test/us/terebi/lang/lpc/compiler/integration/FunctionalTest.java

         List<Callable<Object>> list = new ArrayList<Callable<Object>>();
         ObjectBuilder builder = createBuilder(new File("source/lpc/test/us/terebi/lang/lpc/compiler/functional/"));
 
+        list.addAll(getTests("expression.c", builder));
         list.addAll(getTests("binary.c", builder));
         list.addAll(getTests("math.c", builder));
         list.addAll(getTests("logic.c", builder));

File modules/lpc/source/lpc/test/us/terebi/lang/lpc/compiler/functional/expression.c

+public int a5()
+{
+    int i,j,k;
+    i=2,j=4,k=-1;
+    return i+j+k;
+}

File modules/lpc/source/lpc/test/us/terebi/lang/lpc/compiler/functional/loop.c

 {
     int a = 0;
     int * arr = ({ 5, 7, 13, 1, 4, 9 });
-    // 5,12,25,26,30,39
+    // 5,12,25,26 1+2+3,30,39
     foreach ( int e in arr )
     {
         a += e;
     }
     return str;
 }
+
+public int a6()
+{
+    int sum, i, max;
+
+    for(sum=0, i=0, max = 3; i<max; i++)
+        sum += (i+1);
+    // 1+2+3
+
+    return sum;
+}
+
+public int b6()
+{
+    int sum, i, max;
+
+    for(max = 3; i<max; i++)
+        sum += (i+1);
+    // 1+2+3
+
+    return sum;
+}
+