comparison truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java @ 22104:cf19259edf87

TruffleVM.eval and Source.withMimeType
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 24 Aug 2015 08:46:21 +0200
parents a7ca9e9a1d51
children 7ee578004be7
comparison
equal deleted inserted replaced
22103:7646278cca8a 22104:cf19259edf87
63 import com.oracle.truffle.sl.nodes.instrument.*; 63 import com.oracle.truffle.sl.nodes.instrument.*;
64 import com.oracle.truffle.sl.nodes.local.*; 64 import com.oracle.truffle.sl.nodes.local.*;
65 import com.oracle.truffle.sl.parser.*; 65 import com.oracle.truffle.sl.parser.*;
66 import com.oracle.truffle.sl.runtime.*; 66 import com.oracle.truffle.sl.runtime.*;
67 import com.oracle.truffle.tools.*; 67 import com.oracle.truffle.tools.*;
68 import java.nio.file.Path;
68 69
69 /** 70 /**
70 * SL is a simple language to demonstrate and showcase features of Truffle. The implementation is as 71 * SL is a simple language to demonstrate and showcase features of Truffle. The implementation is as
71 * simple and clean as possible in order to help understanding the ideas and concepts of Truffle. 72 * simple and clean as possible in order to help understanding the ideas and concepts of Truffle.
72 * The language has first class functions, but no object model. 73 * The language has first class functions, but no object model.
198 int repeats = 1; 199 int repeats = 1;
199 if (args.length >= 2) { 200 if (args.length >= 2) {
200 repeats = Integer.parseInt(args[1]); 201 repeats = Integer.parseInt(args[1]);
201 } 202 }
202 203
204 Source source;
203 if (args.length == 0) { 205 if (args.length == 0) {
204 vm.eval("application/x-sl", new InputStreamReader(System.in)); 206 source = Source.fromReader(new InputStreamReader(System.in), "<stdin>").withMimeType("application/x-sl");
205 } else { 207 } else {
206 vm.eval(new File(args[0]).toURI()); 208 source = Source.fromFileName(args[0]);
207 } 209 }
210 vm.eval(source);
208 Symbol main = vm.findGlobalSymbol("main"); 211 Symbol main = vm.findGlobalSymbol("main");
209 if (main == null) { 212 if (main == null) {
210 throw new SLException("No function main() defined in SL source file."); 213 throw new SLException("No function main() defined in SL source file.");
211 } 214 }
212 while (repeats-- > 0) { 215 while (repeats-- > 0) {
219 * Temporary method during API evolution, supports debugger integration. 222 * Temporary method during API evolution, supports debugger integration.
220 */ 223 */
221 public static void run(Source source) throws IOException { 224 public static void run(Source source) throws IOException {
222 TruffleVM vm = TruffleVM.newVM().build(); 225 TruffleVM vm = TruffleVM.newVM().build();
223 assert vm.getLanguages().containsKey("application/x-sl"); 226 assert vm.getLanguages().containsKey("application/x-sl");
224 vm.eval(new File(source.getPath()).toURI()); 227 vm.eval(source);
225 Symbol main = vm.findGlobalSymbol("main"); 228 Symbol main = vm.findGlobalSymbol("main");
226 if (main == null) { 229 if (main == null) {
227 throw new SLException("No function main() defined in SL source file."); 230 throw new SLException("No function main() defined in SL source file.");
228 } 231 }
229 main.invoke(null); 232 main.invoke(null);
231 234
232 /** 235 /**
233 * Parse and run the specified SL source. Factored out in a separate method so that it can also 236 * Parse and run the specified SL source. Factored out in a separate method so that it can also
234 * be used by the unit test harness. 237 * be used by the unit test harness.
235 */ 238 */
236 public static long run(TruffleVM context, URI source, PrintWriter logOutput, PrintWriter out, int repeats, List<NodeFactory<? extends SLBuiltinNode>> currentBuiltins) throws IOException { 239 public static long run(TruffleVM context, Path path, PrintWriter logOutput, PrintWriter out, int repeats, List<NodeFactory<? extends SLBuiltinNode>> currentBuiltins) throws IOException {
237 builtins = currentBuiltins; 240 builtins = currentBuiltins;
238 241
239 if (logOutput != null) { 242 if (logOutput != null) {
240 logOutput.println("== running on " + Truffle.getRuntime().getName()); 243 logOutput.println("== running on " + Truffle.getRuntime().getName());
241 // logOutput.println("Source = " + source.getCode()); 244 // logOutput.println("Source = " + source.getCode());
242 } 245 }
243 246
247 Source src = Source.fromFileName(path.toString());
244 /* Parse the SL source file. */ 248 /* Parse the SL source file. */
245 Object result = context.eval(source); 249 Object result = context.eval(src.withMimeType("application/x-sl"));
246 if (result != null) { 250 if (result != null) {
247 out.println(result); 251 out.println(result);
248 } 252 }
249 253
250 /* Lookup our main entry point, which is per definition always named "main". */ 254 /* Lookup our main entry point, which is per definition always named "main". */