package jscript;
import java.io.Reader;
import java.io.StringReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.*;
import jscript.parser.*;
import jscript.util.PrimitiveType;
import jscript.exception.*;
import jscript.engine.*;
import jscript.console.*;
public class JScript{
public static final String title = "JScript 1.2 build 1127 by Edison";
protected JScriptParser parser;
protected ParsingContext pCtx;
protected EvaluatingContext ctx;
public JScript(){
parser = new JScriptParser(new StringReader("return;"));
pCtx = new ParsingContext();
ctx = new EvaluatingContext();
Console.init(pCtx, ctx);
}
public void defineVariable(String name, Class type, Object value) {
pCtx.defineVariable(name, type);
ctx.defineVariable(name, type);
if (value != null) setVariable(name, value, type);
}
public void setVariable(String name, Object value) {
setVariable(name, value, value.getClass());
}
public void setVariable(String name, Object value, Class fromClass) {
if (fromClass.isInstance(value) ||
(fromClass.isPrimitive() && value != null && PrimitiveType.wrapClassMap.get(fromClass) == value.getClass())) {
ctx.setVariableValue(name, value, fromClass);
} else {
throw new EvaluatingException(value + " is not instance of " + fromClass);
}
}
public Object getVariable(String name) {
return ctx.getVariableValue(name);
}
public void execute(Reader r) throws Exception {
parser.ReInit(r);
parser.parseRConsole(pCtx).execute(ctx);
}
public Object eval(String expression) throws Exception {
return eval(expression, Collections.EMPTY_LIST);
}
public Object eval(String expression, List importList) throws Exception {
return eval(expression, new ArgList(), importList, false);
}
public Object eval(String expression, Object[] args) throws Exception {
return eval(expression, args, Collections.EMPTY_LIST);
}
public Object eval(String expression, Object[] args, List importList) throws Exception {
return eval(expression, new ArgList(args), importList, false);
}
public Object eval(String expression, boolean fork) throws Exception {
return eval(expression, new ArgList(), Collections.EMPTY_LIST, fork);
}
protected Object eval(String expression, ArgList argList, List importList, boolean fork) throws Exception {
try{
Reader r = new StringReader(expression);
parser.ReInit(r);
ParsingContext pCtx = fork ? new ParsingContext() : this.pCtx;
EvaluatingContext ctx = fork ? new EvaluatingContext(argList) : this.ctx;
if (importList != null) {
for (Iterator it = importList.iterator(); it.hasNext();) {
String pkg = (String)it.next();
pCtx.importPackage(pkg);
ctx.importPackage(pkg);
}
}
return parser.eval(pCtx).evaluate(ctx);
}catch(InvokationException e){
throw (Exception)e.getCause();
}
}
public static void main(String[] args) throws Exception{
if (args.length == 0) console();
else if (args[0].equalsIgnoreCase("-i")){
console(new FileInputStream(args[1]));
} else {
FileReader r = null;
try{
r = new FileReader(args[0]);
String[] subArgs = new String[args.length - 1];
System.arraycopy(args, 1, subArgs, 0, subArgs.length);
new JScriptWrapper(r).run(subArgs);
}finally{
if (r != null) r.close();
}
}
}
private static void console() throws Exception{
console(null);
}
private static void console(InputStream init) throws Exception{
System.out.println(title);
ParsingContext pCtx = new ParsingContext();
EvaluatingContext ctx = new EvaluatingContext();
Console.init(pCtx, ctx);
JScriptParser parser = null;
if (init == null) {
parser = new JScriptParser(System.in);
} else {
parser = new JScriptParser(init);
try{
if (parser.parse(pCtx).execute(ctx) != null) {
System.out.println("System exited.");
if (ctx.isHasReturnValue()){
System.out.println(ctx.getReturnValue());
}
return;
}
}catch(Exception e){
e.printStackTrace();
}
parser.ReInit(System.in);
}
System.out.print(">> ");
while (true) {
try{
if (parser.parseConsole(pCtx).execute(ctx) != null) {
System.out.println("System exited.");
if (ctx.isHasReturnValue()){
System.out.println(ctx.getReturnValue());
}
break;
}
}catch(Exception e){
e.printStackTrace();
parser.ReInit(System.in);
System.out.print(">> ");
continue;
}
}
}
}