Skip to content

Commit

Permalink
pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerpoon committed Aug 13, 2012
1 parent f8c514c commit 5be7360
Show file tree
Hide file tree
Showing 6 changed files with 1,366 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -4,6 +4,7 @@ Alpha 0.014.1
* CLI Compiler updates and fixes
* JavaScript++ command-line REPL
* Bug fixes for switch statements
* Pretty printing of compiled code

8/7/12 - Alpha 0.014
-----------
Expand Down
2 changes: 1 addition & 1 deletion Makefile
@@ -1,4 +1,4 @@
IN := lib/jspp/lang/es5.js src/jsdefs.js src/jsparse.js src/typed-es3.js src/compiler.js typesys/strict.js
IN := lib/jspp/lang/es5.js src/jsdefs.js src/jsparse.js src/typed-es3.js src/compiler.js src/beautify.js typesys/strict.js
OUT := jspp.js

all: $(OUT)
Expand Down
42 changes: 26 additions & 16 deletions bin/jspp
Expand Up @@ -28,33 +28,39 @@ var args = paramon.readFormat(process.argv, {
name: 'scope',
args: ['--scope', '-s'],
desc: 'Creates a scope.',
maxParams: 0,
maxParams: 0
},
{
name: 'debug',
args: ['--debug', '-d'],
desc: 'Compile in debug mode.',
maxParams: 0,
maxParams: 0
},
{
name: 'warnings',
args: ['--warnings', '-w'],
desc: 'Display warnings.',
maxParams: 0,
maxParams: 0
},
{
name: 'pretty',
args: ['--pretty', '-p'],
desc: 'Pretty print compiled code.',
maxParams: 0
},
{
name: 'encoding',
args: ['--encoding', '-e'],
desc: 'The encoding to use for reading the files.',
maxParams: 1,
minParams: 1,
minParams: 1
},
{
name: 'output',
args: ['--output', '-o'],
desc: 'The output target. Default=-',
maxParams: 1,
minParams: 1,
minParams: 1
}
],
});
Expand Down Expand Up @@ -92,15 +98,15 @@ function ready () {

var t = Date.now();

var filename = args["output"] || '-';
var filename = args.output || '-';

//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"]
debug: args.debug,
nowrap: args.scope,
warnings: args.warnings
});

c.preprocess();
Expand All @@ -112,12 +118,6 @@ function ready () {
format.BOLD + filename.replace(/\/\//, "/") + format.RESET +
': Compiled in ' + (Date.now() - t) + 'ms.'
);

if (filename === '-') {
console.log(output);
} else {
fs.writeFileSync(filename, output, args["encoding"] || 'UTF-8');
}
}
//Catch compile errors and manually output, highlight, etc.
catch(e){
Expand Down Expand Up @@ -181,6 +181,16 @@ function ready () {

console.log(errorCode.join("\n"));
}

if (filename === '-') {
console.log(output);
} else {
fs.writeFileSync(
filename,
args.pretty ? jspp.js_beautify(output) : output,
args.encoding || 'UTF-8'
);
}
}

var files = inputFiles.map(function(f){
Expand All @@ -192,7 +202,7 @@ var files = inputFiles.map(function(f){
});
process.stdin.on('end', ready);
} else {
fs.readFile(f, args["encoding"] || 'UTF-8', function(e, data) {
fs.readFile(f, args.encoding || 'UTF-8', function(e, data) {
if (e) throw "Reading file '" + f + "' failed.";

r.r = String(data);
Expand Down
18 changes: 15 additions & 3 deletions compiler.html
Expand Up @@ -9,7 +9,12 @@
<p><b>Source Code:</b></p>
<textarea id="code" style="width:100%;" rows="15"></textarea><br>

<input type="checkbox" id="check_debug" checked="checked"> Compile debug data <input type="checkbox" id="check_warnings" checked="checked"> Show Warnings in Console <input type="checkbox" id="check_wrap" checked="checked"> Wrap in Function<br><br>
<input type="checkbox" id="check_debug" checked="checked"> Compile debug data
<input type="checkbox" id="check_warnings" checked="checked"> Show Warnings in Console
<input type="checkbox" id="check_wrap" checked="checked"> Wrap in Function
<input type="checkbox" id="check_prettyprint"> Pretty Print

<br><br>

<button id="compile">Compile</button> <button id="execute">Execute</button> <span id="compile-time"></span>

Expand All @@ -22,11 +27,14 @@
<script type="text/javascript" src="src/typed-es3.js"></script>
<script type="text/javascript" src="src/compiler.js"></script>
<script type="text/javascript" src="typesys/strict.js"></script>
<script type="text/javascript" src="src/beautify.js"></script>
<script type="text/javascript">
function compile() {
var $ = function(id) { return document.getElementById(id) };

var t = +new Date, status = $("compile-time");
var t = +new Date,
status = $("compile-time"),
compiledCode = "";

try {
var c = new compiler(window.narcissus.jsparse($("code").value), {
Expand All @@ -36,11 +44,15 @@
warnings: $("check_warnings").checked
});
c.preprocess();
$("compiled").value = c.compile();
compiledCode = c.compile();
status.innerText = status.textContent = "Compiled in " + (+new Date - t) + "ms";
}catch(e) {
status.innerText = status.textContent = "Error: " + e;
}

$("compiled").value = $("check_prettyprint").checked ?
js_beautify(compiledCode) :
compiledCode;
}
document.getElementById("compile").onclick=compile;
document.getElementById("execute").onclick=function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "javascript-plusplus",
"author": "Brendan Eich, Guillaume Lathoud, Roger Poon",
"version": "0.011.0",
"version": "0.014.1",
"description": "javascript++",
"main": "./jspp.js",
"repository": {
Expand Down

0 comments on commit 5be7360

Please sign in to comment.