Mercurial > hg > graal-compiler
diff graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java @ 21557:31fc2fce38f3
Merge.
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 27 May 2015 13:32:18 +0200 |
parents | b1530a6cce8c 99e3f4c5c853 |
children | a880844225e4 |
line wrap: on
line diff
--- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java Wed May 27 00:36:16 2015 +0200 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java Wed May 27 13:32:18 2015 +0200 @@ -24,6 +24,8 @@ import java.io.*; import java.math.*; +import java.net.*; +import java.util.*; import java.util.Scanner; import com.oracle.truffle.api.*; @@ -33,6 +35,7 @@ import com.oracle.truffle.api.source.*; import com.oracle.truffle.api.tools.*; import com.oracle.truffle.api.vm.*; +import com.oracle.truffle.api.vm.TruffleVM.Symbol; import com.oracle.truffle.sl.builtins.*; import com.oracle.truffle.sl.factory.*; import com.oracle.truffle.sl.nodes.*; @@ -132,11 +135,17 @@ */ @TruffleLanguage.Registration(name = "sl", mimeType = "application/x-sl") public class SLMain extends TruffleLanguage { + private static SLMain LAST; + private static List<NodeFactory<? extends SLBuiltinNode>> builtins = Collections.emptyList(); private final SLContext context; public SLMain(Env env) { super(env); - this.context = SLContextFactory.create(new BufferedReader(new InputStreamReader(System.in)), new PrintWriter(System.out)); + context = SLContextFactory.create(new BufferedReader(env().stdIn()), new PrintWriter(env().stdOut(), true)); + LAST = this; + for (NodeFactory<? extends SLBuiltinNode> builtin : builtins) { + context.installBuiltin(builtin); + } } /* Demonstrate per-type tabulation of node execution counts */ @@ -150,7 +159,7 @@ * The main entry point. Use the mx command "mx sl" to run it with the correct class path setup. */ public static void main(String[] args) throws IOException { - TruffleVM vm = TruffleVM.create(); + TruffleVM vm = TruffleVM.newVM().build(); assert vm.getLanguages().containsKey("application/x-sl"); int repeats = 1; @@ -158,12 +167,17 @@ repeats = Integer.parseInt(args[1]); } + if (args.length == 0) { + vm.eval("application/x-sl", new InputStreamReader(System.in)); + } else { + vm.eval(new File(args[0]).toURI()); + } + Symbol main = vm.findGlobalSymbol("main"); + if (main == null) { + throw new SLException("No function main() defined in SL source file."); + } while (repeats-- > 0) { - if (args.length == 0) { - vm.eval("application/x-sl", new InputStreamReader(System.in)); - } else { - vm.eval(new File(args[0]).toURI()); - } + main.invoke(null); } } @@ -171,7 +185,9 @@ * Parse and run the specified SL source. Factored out in a separate method so that it can also * be used by the unit test harness. */ - public static long run(SLContext context, Source source, PrintWriter logOutput, int repeats) { + public static long run(TruffleVM context, URI source, PrintWriter logOutput, PrintWriter out, int repeats, List<NodeFactory<? extends SLBuiltinNode>> currentBuiltins) throws IOException { + builtins = currentBuiltins; + if (logOutput != null) { logOutput.println("== running on " + Truffle.getRuntime().getName()); // logOutput.println("Source = " + source.getCode()); @@ -200,11 +216,14 @@ } /* Parse the SL source file. */ - Parser.parseSL(context, source); + Object result = context.eval(source); + if (result != null) { + out.println(result); + } /* Lookup our main entry point, which is per definition always named "main". */ - SLFunction main = context.getFunctionRegistry().lookup("main"); - if (main.getCallTarget() == null) { + Symbol main = context.findGlobalSymbol("main"); + if (main == null) { throw new SLException("No function main() defined in SL source file."); } @@ -215,19 +234,21 @@ /* Change to dump the AST to IGV over the network. */ boolean dumpASTToIGV = false; - printScript("before execution", context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); + printScript("before execution", LAST.context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); long totalRuntime = 0; try { for (int i = 0; i < repeats; i++) { long start = System.nanoTime(); /* Call the main entry point, without any arguments. */ try { - Object result = main.getCallTarget().call(); + result = main.invoke(null); if (result != SLNull.SINGLETON) { - context.getOutput().println(result); + out.println(result); } } catch (UnsupportedSpecializationException ex) { - context.getOutput().println(formatTypeError(ex)); + out.println(formatTypeError(ex)); + } catch (SLUndefinedFunctionException ex) { + out.println(String.format("Undefined function: %s", ex.getFunctionName())); } long end = System.nanoTime(); totalRuntime += end - start; @@ -238,7 +259,7 @@ } } finally { - printScript("after execution", context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); + printScript("after execution", LAST.context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); } if (nodeExecCounter != null) { nodeExecCounter.print(System.out); @@ -307,7 +328,7 @@ if (ex.getNode() != null && ex.getNode().getSourceSection() != null) { SourceSection ss = ex.getNode().getSourceSection(); if (ss != null && !(ss instanceof NullSourceSection)) { - result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn()); + result.append(" at ").append(ss.getSource().getShortName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn()); } } result.append(": operation"); @@ -356,17 +377,22 @@ @Override protected Object findExportedSymbol(String globalName) { + for (SLFunction f : context.getFunctionRegistry().getFunctions()) { + if (globalName.equals(f.getName())) { + return f; + } + } return null; } @Override protected Object getLanguageGlobal() { - return null; + return context; } @Override protected boolean isObjectOfLanguage(Object object) { - return false; + return object instanceof SLFunction; } }