Skip to content

Commit

Permalink
added experimental Harmony Object literal extensions
Browse files Browse the repository at this point in the history
added new features described in harmony.md file.  Also various bug fixes
and ES5 feature additions including: * ES5 future reserved words are
defined as keywords * various paren-free related bug fixes * as per ES5
reserved keywords can be used as property names * as per ES5 numeric and
string literals may be used property names in access property
definitions within object literals.
  • Loading branch information
allenwb committed Aug 15, 2011
1 parent 1731e16 commit 38f361d
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 117 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

Narcissus is a JavaScript interpreter written in pure JavaScript (i.e., a [meta-circular evaluator](http://en.wikipedia.org/wiki/Meta-circular_evaluator)), using the [SpiderMonkey](http://www.mozilla.org/js/spidermonkey/) engine.

This verson of Narcissus adds various experimental features that have been proposed for ES Harmony. See `harmony.md` for a list of new features.

Originally a proof-of-concept by [Brendan Eich](http://brendaneich.com/), Narcissus is being revived as a test-bed for rapidly prototyping new language features for the JavaScript language (as well as the ECMAScript standard).

# Usage

To run the Narcissus shell, install the [SpiderMonkey shell](https://developer.mozilla.org/en/Introduction_to_the_JavaScript_shell) and either set the `NJS_SHELL` environment variable to the path to the `js` executable, or creating a symbolic link to `js` in the top-level Narcissus directory. Then run Narcissus with the `njs` script.
To run the Narcissus shell, install the [SpiderMonkey shell](https://developer.mozilla.org/en/Introduction_to_the_JavaScript_shell) (note that pre-built versions of the shell are available in the Firefox nightly builds, for example <http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/jsshell-win32.zip> or <http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/jsshell-mac.zip>). After bbuilding or downloading set the `NJS_SHELL` environment variable to the path to the `js` executable, or creating a symbolic link to `js` in the top-level Narcissus directory. Then run Narcissus with the `njs` script.

Usage: njs

Expand Down Expand Up @@ -36,3 +38,4 @@ More to come.
* Dave Herman
* Dimitris Vardoulakis
* Patrick Walton
* Allen Wirfs-Brock
70 changes: 47 additions & 23 deletions lib/jsdecomp.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,11 @@ Narcissus.decompiler = (function() {
p += "(";

switch (n.type) {

case FUNCTION:
case GETTER:
case SETTER:
if (n.type === FUNCTION)
p += "function";
else if (n.type === GETTER)
p += "get";
else
p += "set";

p += (n.name ? " " + n.name : "") + "(";
if (!n.skipKeyword)
p += "function" + (n.name ? " " : "");
p += (n.name ? n.name : "") + "(";
for (var i = 0, j = n.params.length; i < j; i++)
p += (i > 0 ? ", " : "") + pp(n.params[i], d);
p += ") " + pp(n.body, d);
Expand Down Expand Up @@ -388,12 +382,20 @@ Narcissus.decompiler = (function() {
}
break;

case PROTO:
var nc = n.children;
p += pp(nc[0], d) + " <| " + pp(nc[1], d);
break;

case EXTEND:
case SUPER_DOT:
case DOT:
var nc = n.children;
p += pp(nc[0], d) + "." + pp(nc[1], d);
break;

case INDEX:
case SUPER_INDEX:
case INDEX:
var nc = n.children;
p += pp(nc[0], d) + "[" + pp(nc[1], d) + "]";
break;
Expand Down Expand Up @@ -439,29 +441,47 @@ Narcissus.decompiler = (function() {
p += " if (" + pp(n.guard, d) + ")";
break;

case OBJECT_INIT:
case OBJECT_INIT: {
var nc = n.children;
if (nc[0] && nc[0].type === PROPERTY_INIT)
p += "{\n";
else
p += "{";
var pname = nc.propertyName;
// if (nc[0] && nc[0].type !== PROPERTY_INIT)
p += "{\n";
// else
// p += "{";
for (var i = 0, j = nc.length; i < j; i++) {
if (i > 0) {
p += ",\n";
}

var t = nc[i];
if (t.type === PROPERTY_INIT) {
var tc = t.children;
p += indent(4, pp(tc[0], d)) + ": " +
indent(4, pp(tc[1], d)).substring(4);
} else {
p += indent(4, pp(t, d));
pname = t.propertyName;
if (pname.type === IDENTIFIER)
pname = '"' + pname.value + '"';
else
pname = '[' + pp(pname, d) + ']';
switch (t.type) {
case PROPERTY_INIT:
p += indent(4, pname + ": " +
pp(t.initializer, d));
break;
case GETTER:
case SETTER:
t.functionDef.skipKeyword = true;
p += indent(4, (t.type===GETTER ? "get " : "set ") +
(!t.functionDef.name ? pname + " " : "" ) +
pp(t.functionDef, d));
break;
case METHOD_INIT:
t.functionDef.skipKeyword = true;
p += indent(4, (!t.functionDef.name ? pname + " " : "") +
pp(t.functionDef, d));
break;
}
}
p += "\n}";
break;

}

case NULL:
p += "null";
break;
Expand All @@ -470,6 +490,10 @@ Narcissus.decompiler = (function() {
p += "this";
break;

case SUPER:
p += "super";
break;

case TRUE:
p += "true";
break;
Expand Down
15 changes: 9 additions & 6 deletions lib/jsdefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,31 @@ Narcissus.definitions = (function() {
"!", "~", "UNARY_PLUS", "UNARY_MINUS",
"++", "--",
".",
"<|",
"[", "]",
"{", "}",
"(", ")",

// Nonterminal tree node type codes.
"SCRIPT", "BLOCK", "LABEL", "FOR_IN", "CALL", "NEW_WITH_ARGS", "INDEX",
"ARRAY_INIT", "OBJECT_INIT", "PROPERTY_INIT", "GETTER", "SETTER",
"ARRAY_INIT", "OBJECT_INIT", "PROPERTY_INIT", "METHOD_INIT", "GETTER", "SETTER",
"GROUP", "LIST", "LET_BLOCK", "ARRAY_COMP", "GENERATOR", "COMP_TAIL",

"EXTEND", "SUPER_DOT", "SUPER_INDEX",

// Terminals.
"IDENTIFIER", "NUMBER", "STRING", "REGEXP",

// Keywords.
"break",
"case", "catch", "const", "continue",
"case", "catch", "class", "const", "continue",
"debugger", "default", "delete", "do",
"else",
"else", "enum", "export", "extends",
"false", "finally", "for", "function",
"if", "in", "instanceof",
"if", "import", "in", "instanceof",
"let",
"new", "null",
"return",
"switch",
"super","switch",
"this", "throw", "true", "try", "typeof",
"var", "void",
"yield",
Expand Down Expand Up @@ -137,6 +139,7 @@ Narcissus.definitions = (function() {
'!=': "NE",
'<<': "LSH",
'<=': "LE",
'<|': "PROTO",
'<': "LT",
'>>>': "URSH",
'>>': "RSH",
Expand Down
Loading

0 comments on commit 38f361d

Please sign in to comment.