// Copyright 2008 Peter William Birch <birchb@genyis.org>
//
// This software may be used and distributed according to the terms
// of the Genyris License, in the file "LICENSE", incorporated herein by reference.
//
package org.genyris.load;

import java.io.Writer;

import org.genyris.core.Constants;
import org.genyris.core.Exp;
import org.genyris.core.StrinG;
import org.genyris.exception.GenyrisException;
import org.genyris.interp.ApplicableFunction;
import org.genyris.interp.Closure;
import org.genyris.interp.Environment;
import org.genyris.interp.Interpreter;
import org.genyris.io.NullWriter;

public class IncludeFunction extends ApplicableFunction {

    public IncludeFunction(Interpreter interp) {
        super(interp, Constants.PREFIX_SYSTEM + "include", true);
    }

    public Exp bindAndExecute(Closure proc, Exp[] arguments, Environment env)
            throws GenyrisException {
        Exp result;
        Writer out = new NullWriter();
        this.checkArguments(arguments, 1, 2);
        if (!(arguments[0] instanceof StrinG)) {
            throw new GenyrisException(
                    "non-string argument passed to include: "
                            + arguments[0].toString());
        }
        if (arguments.length > 1) {
            if (arguments[1] == TRUE) {
                out = _interp.getDefaultOutputWriter();
            }
        }
        String filename = arguments[0].toString();
        result = SourceLoader.loadScriptFromFile(_interp.getGlobalEnv(),
                _interp.getSymbolTable(), filename, out);

        return result;
    }
}

@@ -236,16 +236,44 @@
 .load;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -936,26 +936,92 @@
   Exp result
+ = NIL
 ;%0A
+        try %7B%0A            @SuppressWarnings(%22resource%22)%0A    
         Writ
@@ -1039,32 +1039,36 @@
 w NullWriter();%0A
+    
         this.che
@@ -1101,24 +1101,28 @@
 2);%0A        
+    
 if (!(argume
@@ -1146,24 +1146,57 @@
  StrinG)) %7B%0A
+                out.close();%0A    
             
@@ -1222,37 +1222,16 @@
 ception(
-%0A                    
 %22non-str
@@ -1288,20 +1288,16 @@
         
-    
 + argume
@@ -1324,26 +1324,32 @@
         
-%7D%0A
+    %7D%0A  
         
 if (argu
@@ -1332,32 +1332,34 @@
     %7D%0A          
+  
 if (arguments.le
@@ -1366,24 +1366,28 @@
 ngth %3E 1) %7B%0A
+    
             
@@ -1410,24 +1410,61 @@
  == TRUE) %7B%0A
+                    out.close();%0A    
             
@@ -1511,32 +1511,36 @@
 ();%0A            
+    
 %7D%0A        %7D%0A    
@@ -1525,32 +1525,36 @@
       %7D%0A        
+ 
+   
 %7D%0A        String
@@ -1547,16 +1547,20 @@
         
+ 
+   
 String f
@@ -1590,24 +1590,28 @@
 toString();%0A
+    
         resu
@@ -1671,16 +1671,20 @@
 lEnv(),%0A
+    
         
@@ -1733,16 +1733,152 @@
 , out);%0A
+            out.close();%0A        %7D catch (IOException unknown) %7B%0A            throw new GenyrisException(unknown.getMessage());%0A        %7D
 %0A       

Fix IDE complaints about resource leaks. Take 2.