comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLScript.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
children 69d2e4baa215
comparison
equal deleted inserted replaced
12712:882a0aadfed6 12752:71991b7a0f14
1 /*
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.sl;
24
25 import java.io.*;
26
27 import javax.script.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.sl.parser.*;
31 import com.oracle.truffle.sl.runtime.*;
32
33 public final class SLScript {
34
35 private final SLContext context;
36 private final CallTarget main;
37
38 private SLScript(SLContext context, CallTarget mainFunction) {
39 this.context = context;
40 this.main = mainFunction;
41 }
42
43 public SLContext getContext() {
44 return context;
45 }
46
47 public CallTarget getMain() {
48 return main;
49 }
50
51 public Object run(Object... arguments) {
52 return main.call(null, new SLArguments(arguments));
53 }
54
55 @Override
56 public String toString() {
57 return main.toString();
58 }
59
60 public static SLScript create(SLContext context, String input) throws ScriptException {
61 return create(context, new ByteArrayInputStream(input.getBytes()));
62
63 }
64
65 public static SLScript create(SLContext context, InputStream input) throws ScriptException {
66 SLNodeFactory factory = new SLNodeFactory(context);
67 Parser parser = new Parser(new Scanner(input), factory);
68 factory.setParser(parser);
69 factory.setSource(new Source() {
70 public String getName() {
71 return "Unknown";
72 }
73
74 public String getCode() {
75 return null;
76 }
77 });
78 String error = parser.ParseErrors();
79 if (!error.isEmpty()) {
80 throw new ScriptException(String.format("Error(s) parsing script: %s", error));
81 }
82
83 CallTarget main = context.getFunctionRegistry().lookup("main");
84 if (main == null) {
85 throw new ScriptException("No main function found.");
86 }
87 return new SLScript(context, main);
88 }
89 }