Repository navigation
Expand file tree
/
Copy pathLoop.java
More file actions
177 lines (149 loc) · 5.1 KB
/
Copy pathLoop.java
File metadata and controls
177 lines (149 loc) · 5.1 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
168
169
170
171
172
173
174
175
176
177
package loop;
import loop.ast.script.Unit;
import loop.runtime.LoopInvocationHandler;
import java.io.*;
import java.lang.reflect.Proxy;
/**
* Converts parsed, type-solved, emitted code to Java classes.
*/
public class Loop {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
LoopShell.shell();
}
try {
if (args.length > 1)
run(args[0], args);
else
run(args[0]);
} catch (RuntimeException e) {
if (e.getCause() instanceof FileNotFoundException) {
System.out.println("No such file: " + e.getCause().getMessage());
System.out.println();
System.exit(1);
} else
throw e;
}
}
/**
* Executes a loop program stored in {@code file} and returns the
* result of evaluating main()--which is optional.
*/
public static Object run(String file) {
Executable unit = loopCompile(file);
unit.runMain(true);
return safeEval(unit, null);
}
/**
* Executes a loop program stored in {@code file} and returns the
* result of evaluating main()--which is optional. Arguments can
* be passed in which are sent to main() representing the command line.
*/
public static Object run(String file, String[] args) {
Executable unit = loopCompile(file);
unit.runMain(true);
return safeEval(unit, args);
}
/**
* Executes a loop program read from {@code reader }and returns the
* result of evaluating main()--which is optional. Arguments can
* be passed in which are sent to main() representing the command line.
*/
public static Object run(String name, Reader reader, String[] args) {
Executable unit = loopCompile(name, reader);
unit.runMain(true);
return safeEval(unit, args);
}
public static Object evalClassOrFunction(String function,
Unit shellScope) {
Executable executable = new Executable(new StringReader(function));
try {
executable.compileClassOrFunction(shellScope);
} catch (Exception e) {
e.printStackTrace();
return new LoopError("malformed function");
}
if (executable.hasErrors()) {
executable.printStaticErrorsIfNecessary();
return "";
}
return "ok";
}
static Object safeEval(Executable executable, String[] args) {
if (executable.runMain()) {
return executable.main(args);
} else {
executable.getCompiled(); // Forces class to be loaded & initialized.
return null;
}
}
/**
* Compiles the specified file into a binary Java executable.
*/
public static Class<?> compile(String file) {
return loopCompile(file).getCompiled();
}
/**
* Compiles the given script (fed by reader) into a binary Java executable.
*/
public static Class<?> compile(String name, Reader reader) {
return loopCompile(name, reader).getCompiled();
}
/**
* Returns an executable that represents the compiled form of the Loop program.
* <p/>
* See {@link Executable} for more details on the compilation process.
*/
private static Executable loopCompile(String file) {
File script = new File(file);
try {
return loopCompile(script.getName(), new FileReader(script));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
private static Executable loopCompile(String name, Reader reader) {
Executable executable = new Executable(reader, name);
executable.compile();
if (executable.hasErrors()) {
String errors = executable.printStaticErrorsIfNecessary();
throw new LoopCompileException("Syntax errors exist:\n" + errors, executable);
}
return executable;
}
public static Object evalLisp(String name, Reader reader) {
Executable executable = lispCompile(name, reader);
executable.runMain(true);
executable.main(null);
return null;
}
private static Executable lispCompile(String name, Reader reader) {
Executable executable = new Executable(reader, name, true);
executable.compile();
if (executable.hasErrors()) {
String errors = executable.printStaticErrorsIfNecessary();
throw new LoopCompileException("Syntax errors exist:\n" + errors, executable);
}
return executable;
}
public static void error(String error) {
throw new LoopExecutionException(error);
}
/**
* Returns an implementation of the given Java interface that
* is backed by the specified Loop module. The generated Java class is loaded into
* the common runtime Loop class loader. See {@link LoopClassLoader} for details.
*
* @param iface A Java interface that you wish to implement using Loop
* @param module The name of a Loop module minus the '.loop' extension. This name may
* contain a path-prefix from the current directory.
*/
@SuppressWarnings("unchecked")
public static <I> I implement(Class<I> iface, String module) {
if (!iface.isInterface()) {
throw new RuntimeException(iface + " is not an interface ");
}
return (I) Proxy.newProxyInstance(LoopClassLoader.CLASS_LOADER, new Class[]{iface},
new LoopInvocationHandler(iface, module));
}
}