Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SimpleLanguage.java @ 12752:71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Nov 2013 21:34:44 +0100 |
parents | 7fee8bd5d2bd |
children | 69d2e4baa215 |
comparison
equal
deleted
inserted
replaced
12712:882a0aadfed6 | 12752:71991b7a0f14 |
---|---|
22 */ | 22 */ |
23 package com.oracle.truffle.sl; | 23 package com.oracle.truffle.sl; |
24 | 24 |
25 import java.io.*; | 25 import java.io.*; |
26 | 26 |
27 import javax.script.*; | |
28 | |
27 import com.oracle.truffle.api.*; | 29 import com.oracle.truffle.api.*; |
30 import com.oracle.truffle.api.impl.*; | |
28 import com.oracle.truffle.api.nodes.*; | 31 import com.oracle.truffle.api.nodes.*; |
29 import com.oracle.truffle.sl.nodes.*; | 32 import com.oracle.truffle.sl.runtime.*; |
30 import com.oracle.truffle.sl.parser.*; | |
31 | 33 |
32 public class SimpleLanguage { | 34 public class SimpleLanguage { |
35 | |
36 private static final Object[] NO_ARGUMENTS = new Object[0]; | |
33 | 37 |
34 public static void main(String[] args) throws IOException { | 38 public static void main(String[] args) throws IOException { |
35 run(new FileInputStream(args[0]), System.out, 10, true); | 39 run(new FileInputStream(args[0]), System.out, 10, true); |
36 } | 40 } |
37 | 41 |
40 // CheckStyle: stop system..print check | 44 // CheckStyle: stop system..print check |
41 System.out.printf("== running on %s\n", Truffle.getRuntime().getName()); | 45 System.out.printf("== running on %s\n", Truffle.getRuntime().getName()); |
42 // CheckStyle: resume system..print check | 46 // CheckStyle: resume system..print check |
43 } | 47 } |
44 | 48 |
45 NodeFactory factory = new NodeFactory(printOutput); | 49 SLContext context = new SLContext(printOutput); |
46 | 50 SLScript script; |
47 Parser parser = new Parser(new Scanner(input), factory); | 51 try { |
48 parser.Parse(); | 52 script = SLScript.create(context, input); |
49 | 53 } catch (ScriptException e) { |
50 FunctionDefinitionNode rootNode = factory.findFunction("main"); | 54 // TODO temporary hack |
51 if (log) { | 55 throw new RuntimeException(e); |
52 NodeUtil.printTree(System.out, rootNode); | |
53 } | 56 } |
54 | 57 |
58 if (log) { | |
59 printScript(script); | |
60 } | |
55 try { | 61 try { |
56 CallTarget function = Truffle.getRuntime().createCallTarget(rootNode, rootNode.getFrameDescriptor()); | |
57 for (int i = 0; i < repeats; i++) { | 62 for (int i = 0; i < repeats; i++) { |
58 Arguments arguments = new SLArguments(new Object[0]); | |
59 | |
60 long start = System.nanoTime(); | 63 long start = System.nanoTime(); |
61 Object result = function.call(null, arguments); | 64 Object result = script.run(NO_ARGUMENTS); |
62 long end = System.nanoTime(); | 65 long end = System.nanoTime(); |
63 | 66 |
64 if (result != null) { | 67 if (result != null) { |
65 printOutput.println(result); | 68 printOutput.println(result); |
66 } | 69 } |
71 } | 74 } |
72 } | 75 } |
73 | 76 |
74 } finally { | 77 } finally { |
75 if (log) { | 78 if (log) { |
76 NodeUtil.printTree(System.out, rootNode); | 79 printScript(script); |
77 } | 80 } |
78 } | 81 } |
79 } | 82 } |
83 | |
84 private static void printScript(SLScript script) { | |
85 NodeUtil.printTree(System.out, ((DefaultCallTarget) script.getMain()).getRootNode()); | |
86 } | |
80 } | 87 } |