Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / esc / src / esc-core.es
blobc53de5eff77e7337147c60d7140c078e8a075416
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is [Open Source Virtual Machine.].
16  *
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.
21  *
22  * Contributor(s):
23  *   Adobe AS3 Team
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
39 use default namespace ESC,
40     namespace ESC;
42 internal function compile(consume, produce, context, start_line) {
43     let t1 = new Date;
45     let input = consume();
46     let parser = new Parse::Parser(input, getTopFixtures(), context, start_line);
47     let prog = parser.program();
49     let t2 = new Date;
51     let res = produce( Gen::cg(prog) );
53     let t3 = new Date;
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.
62 class Flags 
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;
75 // Flags are:
76 //   -[no-]debug
77 //   -g
78 //   -[no-]extensions
79 //   -[no-]es3
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) {
94     outer:
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];
101                     flags[prop] = val;
102                 }
103                 argv.shift();
104                 continue outer;
105             }
106         }
107         break;
108     }
109     return argv;
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")),
118              fname,
119              start_line );
121 function compileAndLoadFile(fname, start_line=1)
122     compile( (function () Util::readStringFromFile(fname)),
123              (function (abc) Util::loadBytes(abc.getBytes())),
124              fname,
125              start_line );
127 function compileAndLoadString(input, context, start_line=1)
128     compile( (function () input),
129              (function (abc) Util::loadBytes(abc.getBytes())),
130              context,
131              start_line );
133 function compileStringToBytes(input, context="(string)", start_line=1) {
134     let [_,_,res] = compile( (function () input),
135                              (function (abc) abc.getBytes()),
136                              context,
137                              start_line );
138     return res;
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));