1 /* -*- mode: java; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil; -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is [Open Source Virtual Machine.].
17 * The Initial Developer of the Original Code is
18 * Adobe System Incorporated.
19 * Portions created by the Initial Developer are Copyright (C) 2004-2006
20 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 use default namespace ESC,
42 internal function compile(consume, produce, context, start_line) {
45 let input = consume();
46 let parser = new Parse::Parser(input, getTopFixtures(), context, start_line);
47 let prog = parser.program();
51 let res = produce( Gen::cg(prog) );
55 return [t2-t1, t3-t2, res];
58 // Public API below. The API is ad hoc on purpose, each function
59 // serves a different use case and is provided for convenience.
60 // Keeping the API small is not (currently) a goal.
64 var es3_keywords = false; // recognize only ES3 keywords (not future reserved)
65 var es4_kwd_debugger = false; // language misfeature
66 var debugging = false; // true to enable debug code
67 var ext_dynamic_override = false; // true to enable 'dynamic override' expression
68 var ext_toplevel_letexpr = false; // true to recognize let expressions at the top level
69 var profile_compiler = false; // true to profile the ESC run (used in esc.es)
72 const version = { major: 0, minor: 1, nick: "That depends on what the meaning of 'is' is" };
73 const flags = new Flags;
81 internal var flagvalues = [ ["-es3", [["es3_keywords",true]]],
82 ["-no-es3", [["es3_keywords",false]]],
83 ["-g", [["debugging",true]]],
84 ["-debug", [["debugging",true]]],
85 ["-no-debug", [["debugging",false]]],
86 ["-extensions", [["ext_dynamic_override",true],
87 ["ext_toplevel_letexpr",true]]],
88 ["-no-extensions", [["ext_dynamic_override",false],
89 ["ext_toplevel_letexpr",false]]],
90 ["-Xprofile", [["profile_compiler",true]]] ];
93 function filterCommandLine(argv) {
95 while (argv.length > 0) {
96 for ( let i=0 ; i < flagvalues.length ; i++ ) {
97 let [name,settings] = flagvalues[i];
98 if (argv[0] == name) {
99 for ( let j=0 ; j < settings.length ; j++ ) {
100 let [prop,val] = settings[j];
112 function getTopFixtures()
113 ESC::bootstrap_namespaces; // in esc-env.es
115 function compileFile(fname, start_line=1)
116 compile( (function () Util::readStringFromFile(fname)),
117 (function (abc) Util::writeBytesToFile(abc.getBytes(), fname + ".abc")),
121 function compileAndLoadFile(fname, start_line=1)
122 compile( (function () Util::readStringFromFile(fname)),
123 (function (abc) Util::loadBytes(abc.getBytes())),
127 function compileAndLoadString(input, context, start_line=1)
128 compile( (function () input),
129 (function (abc) Util::loadBytes(abc.getBytes())),
133 function compileStringToBytes(input, context="(string)", start_line=1) {
134 let [_,_,res] = compile( (function () input),
135 (function (abc) abc.getBytes()),
141 function parseFromStringAndEncodeAst(input, context="string input"): String {
142 let parser = new Parse::Parser(input, getTopFixtures(), context);
143 let program = parser.program();
144 return (new Ast::Serializer()).serialize(program);
147 function parseFromFileAndEncodeAst(fname)
148 parseFromStringAndEncodeAst(Util::readStringFromFile(fname), fname);
150 function decodeAstFromString(input): Ast::Program
151 (new Ast::Unserializer()).unserializeText(input);
153 function decodeAstFromFile(fname)
154 decodeAstFromString(Util::readStringFromFile(fname));