Menu

[r2]: / trunk / src / jscript / JScript.java  Maximize  Restore  History

Download this file

168 lines (140 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;
}
}
}
}
MongoDB Logo MongoDB