Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 153 additions and 41 deletions.
  1. +29 −25 README.md
  2. +34 −0 bin/build.sh
  3. +90 −16 bin/jspp
View
54 README.md
@@ -43,7 +43,7 @@ alert(foo);
### CLI
-1. Install via npm
+Install via npm:
```
sudo npm install javascript-plusplus -g
@@ -55,50 +55,54 @@ or if you're building from sources, go to the main directory and do
sudo npm install -g
```
-2. Example usages:
+Example usages:
```
js++ file1.jspp file2.jspp -o out.js # compile the two files and save to file out.js
cat file1.jspp file2.jspp | js++ - > out.js # same thing, but with stdio pipes
```
-A command-line REPL is not available for the current alpha.
+Once the CLI compiler has been installed, you can use the build script in bin/build.sh to recursively compile all JS++ files in a project directory:
-You can join us on IRC at #javascript++ on freenode and use jsppBot as a sort of REPL.
+1. Create a "src" folder in your project directory. This folder stores all your JavaScript++ source files (in .jspp format)
+2. Create a "build" folder. This folder stores the compiled files
+3. Copy bin/build.sh to your project root directory.
+4. Execute ```sh build.sh``` in the command-line to recursively compile all JS++ source files. The directory structure in the "src" folder will be maintained in your compiled "build" folder.
File Descriptions
-----------------
-bin/ - Executables and Binaries
+bin/ - Executables and Binaries
bin/jspp - CLI compiler
+bin/build.sh - Bash script for recursively compiling all JS++ files
-src/ - Parser and compiler source code
-src/jsdefs.js - Lookup tables
-src/jsparse.js - Lexer/parser
-src/compiler.js - Code generator
+src/ - Parser and compiler source code
+src/jsdefs.js - Lookup tables
+src/jsparse.js - Lexer/parser
+src/compiler.js - Code generator
src/typed-es3.js - Emulation of typed ES3 environment.
-lib/ - Standard Library
-lib/dom - DOM libraries
-lib/jspp - JavaScript++ libraries. Language only; does not manipulate or depend on host objects
-lib/jspp/lang/es5.js - Incorporates ES5 features such as array extras and Function.prototype.bind
-lib/jspp/lang/json2.js - JSON library
+lib/ - Standard Library
+lib/dom - DOM libraries
+lib/jspp - JavaScript++ libraries. Language only; does not manipulate or depend on host objects
+lib/jspp/lang/es5.js - Incorporates ES5 features such as array extras and Function.prototype.bind
+lib/jspp/lang/json2.js - JSON library
lib/server - Server-side JavaScript libraries
-typesys/ - Pluggable type systems
+typesys/ - Pluggable type systems
typesys/strict.js - Strong, static typing for JavaScript++
-test/ - Unit tests
-test/lib - Unit test libraries and modules
-test/src - Source code for unit tests
-test/classes.html - Unit tests for classes
-test/conditionals.html - Unit tests for conditionals
-test/scopes.html - Unit tests for scopes
+test/ - Unit tests
+test/lib - Unit test libraries and modules
+test/src - Source code for unit tests
+test/classes.html - Unit tests for classes
+test/conditionals.html - Unit tests for conditionals
+test/scopes.html - Unit tests for scopes
test/typesys-strict.html - Unit tests for #typesys strict
Credits
-------
-Brendan Eich - JavaScript & Narcissus
-Guillaume Lathoud - v8/JSC port of Narcissus
-Roger Poon - JavaScript++ Language
-Jussi Kalliokoski - CLI Compiler
+Brendan Eich - JavaScript & Narcissus
+Guillaume Lathoud - v8/JSC port of Narcissus
+Roger Poon - JavaScript++ Language
+Jussi Kalliokoski - CLI Compiler
QUnit - QUnit testing framework
View
34 bin/build.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+# Sample bash script to automate the build process for JavaScript++ files
+#
+# Instructions:
+# 1. Create a "src" directory for all your JavaScript++ *.jspp files
+# 2. Create a "build" directory for all your compiled files
+# 3. Drop this script in the root folder where the "src" and "build" folders exist
+# 4. Run the script
+#
+# The "src" directory structure will remain intact within the "build" directory.
+
+BUILD_DIR='build/'
+SOURCE_DIR='src/'
+
+################################################################################
+
+ROOT_DIR="$( cd "$( dirname "$0" )" && pwd )"
+
+cd $ROOT_DIR
+echo 'Cleaning build directory'
+rm -rf $BUILD_DIR
+mkdir $BUILD_DIR
+
+echo 'Compiling JavaScript++ files'
+JSPP_FILES=$(find . -name "*.jspp" -type f)
+for FILE in $JSPP_FILES
+do
+ OUTPUT=$(echo "$FILE" | sed "s@$SOURCE_DIR@$BUILD_DIR@" | sed 's@\.jspp$@.js@')
+ mkdir -p $(echo $OUTPUT | sed 's@/[^/]*\.jsp*$@@')
+ js++ "$FILE" -o $OUTPUT
+done
+
+echo 'Build complete'
View
106 bin/jspp
@@ -9,6 +9,14 @@ function jsparse () {
return jspp.narcissus.jsparse.apply(jspp.narcissus, arguments);
}
+var format = {
+ BOLD : '\u001b[0;1m',
+ GREEN: '\u001b[0;32m',
+ RED : '\u001b[0;31m',
+ REDBG: '\u001b[0;41m',
+ RESET: '\u001b[0m'
+};
+
var args = paramon.readFormat(process.argv, {
name: "js++",
usage: "js++ file(s) [options]",
@@ -49,11 +57,15 @@ var args = paramon.readFormat(process.argv, {
});
args.debug || process.on('uncaughtException', function (e) {
- console.error(e);
+ //Display the error message
+ console.log('[' + format.RED + 'ERROR' + format.RESET + '] ' + (e.message || ""));
+
process.exit(1);
});
-if (!args['$!stray'].length) throw "No files specified.";
+if (!args['$!stray'].length) {
+ throw Error("No files specified.");
+}
/* Heavy async soup */
@@ -74,27 +86,89 @@ function ready () {
var t = Date.now();
- var c = new compiler(jsparse(code), {
- debug: args["debug"],
- nowrap: args["scope"],
- warnings: args["warnings"]
- });
+ var filename = args["output"] || '-';
- c.preprocess();
+ //Wrap the compilation code in a try/catch so we can manually output
+ //compile errors
+ try {
+ var c = new compiler(jsparse(code), {
+ debug: args["debug"],
+ nowrap: args["scope"],
+ warnings: args["warnings"]
+ });
- var output = c.compile();
+ c.preprocess();
- var filename = args["output"] || '-';
+ var output = c.compile();
- console.log('[OK] ' + filename.replace(/\/\//, "/") + ': Compiled in ' + (Date.now() - t) + 'ms.');
+ console.log(
+ '[' + format.GREEN + 'OK' + format.RESET + '] ' +
+ filename.replace(/\/\//, "/") +
+ ': Compiled in ' + (Date.now() - t) + 'ms.'
+ );
- if (filename === '-') {
- console.log(output);
- } else {
- fs.writeFileSync(filename, output, args["encoding"] || 'UTF-8');
+ if (filename === '-') {
+ console.log(output);
+ } else {
+ fs.writeFileSync(filename, output, args["encoding"] || 'UTF-8');
+ }
}
-}
+ //Catch compile errors and manually output, highlight, etc.
+ catch(e){
+ //Get error line number
+ var lineno = /lineno:\s*(\d+)/i.exec(e.message);
+ if (lineno && lineno.length == 2) {
+ lineno = (+lineno[1] - 1) || -1;
+ }
+ else {
+ lineno = -1;
+ }
+
+ //Format the error message
+ e.message = e.message.replace(/\s*filename:\s*,?/i, "");
+ e.message = e.message.replace(/lineno:\s*/i, format.BOLD + "line " + format.RESET);
+
+ //Display the error message
+ console.log(
+ '[' + format.RED + 'ERROR' + format.RESET + '] ' +
+ (e.message || "") +
+ format.BOLD + " char " + format.RESET + e.cursor + "\n"
+ );
+ console.log(" " + format.BOLD + "Filename" + format.RESET + ": " + filename);
+ console.log(" " + format.BOLD + "Source Code" + format.RESET + ": \n");
+
+ //Highlight the specific error character
+ var errorCode = e.source,
+ errorCodeBegin = errorCode.slice(0, e.cursor),
+ errorCodeHighlight = errorCode.slice(e.cursor, e.cursor + 1);
+ errorCodeEnd = errorCode.slice(e.cursor + 1, errorCode.length);
+
+ errorCode = errorCodeBegin +
+ format.RED + errorCodeHighlight + format.RESET +
+ errorCodeEnd;
+
+ //Format error code
+ errorCode = errorCode.split(/[\r\n]/gm);
+ errorCode = errorCode.map(function(lineCode, index) {
+ var indent = " ";
+
+ //Highlight the indent for error lines
+ if (index == lineno) {
+ indent = format.REDBG + " " + format.RESET;
+ }
+
+ return indent + (index + 1) + ". " + lineCode;
+ });
+
+ //Get only the nearest lines for an error rather than outputting entire
+ //source code
+ var beginLine = Math.max(0, lineno - 5),
+ endLine = Math.min(lineno + 5, errorCode.length);
+ errorCode = errorCode.slice(beginLine, endLine);
+ console.log(errorCode.join("\n"));
+ }
+}
var files = args['$!stray'].map(function(f){
var r = new Dummy;

No commit comments for this range

Something went wrong with that request. Please try again.