/* --------------------------------------------------------- *
* __________ D E L T A S C R I P T *
* (_________() *
* / === / - A fast, dynamic scripting language *
* | == | - Version 4.13.11.0 *
* / === / - Developed by Adam R. Nelson *
* | = = | - 2011-2013 *
* / === / - Distributed under GNU LGPL v3 *
* (________() - http://github.com/ar-nelson/deltascript *
* *
* --------------------------------------------------------- */
package com.sector91.delta.script.profiling;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.JFileChooser;
import com.sector91.delta.script.DScriptErr;
import com.sector91.delta.script.DScriptContext;
import com.sector91.delta.script.DeltaScript;
import com.sector91.delta.script.instrs.DSInstr;
import com.sector91.delta.script.parser.DScriptBinaryReader;
public class DeltaScriptFileTester
{
public static void main(String[] args) throws Exception
{
System.out.println(":: Displaying open dialog...");
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
fc.requestFocus();
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
System.out.println(":: Loading " + file.getCanonicalPath() + "...");
final FileInputStream fileStream = new FileInputStream(file);
boolean closed = false;
final DScriptContext context = new DScriptContext();
try
{
if (file.getName().endsWith(DeltaScript.SRC_EXT))
{
final StringBuffer scriptText = new StringBuffer();
final BufferedReader reader =
new BufferedReader(new InputStreamReader(fileStream,
DeltaScript.FILE_ENCODING));
while (reader.ready())
scriptText.append(reader.readLine()+"\n");
reader.close();
closed = true;
System.out.println(":: Compiling...");
DSInstr script = DeltaScript.compile(
scriptText.toString(), file.getName(), context);
System.out.println(":: Executing...\n");
DeltaScript.exec(script, context);
System.out.println("\n:: Done.");
}
else if (file.getName().endsWith(DeltaScript.BIN_EXT))
{
System.out.println(":: Compiling...");
final DScriptBinaryReader reader = new DScriptBinaryReader(
fileStream);
DSInstr script = reader.read(context, file.getName());
reader.close();
closed = true;
System.out.println(":: Executing...\n");
DeltaScript.exec(script, context);
System.out.println("\n:: Done.");
}
else
System.out.println(":: Not a valid DeltaScript file. Must" +
" end with .ds or .dsc.");
}
catch (DScriptErr ex)
{System.out.println(ex)/*.getConsoleMessage())*/;}
finally
{if (!closed) fileStream.close();}
}
else
{System.out.println(":: Canceled.");}
}
}