Split out compiler to separate syndicatec command

This commit is contained in:
Tony Garnock-Jones 2016-03-19 13:01:14 -04:00
parent adaf9511bf
commit 3b5a07f954
6 changed files with 48 additions and 21 deletions

23
js/bin/syndicatec Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env node
// -*- javascript -*-
var fs = require('fs');
var compiler = require('../compiler/compiler.js');
function compileAndPrint(inputSource) {
var translatedSource = compiler.compileSyndicateSource(inputSource);
if (translatedSource) {
console.log(translatedSource);
}
}
if (process.argv.length < 3 || process.argv[2] === '-') {
var inputSource = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(buf) { inputSource += buf; });
process.stdin.on('end', function() { compileAndPrint(inputSource); });
} else {
var inputSource = fs.readFileSync(process.argv[2]).toString();
compileAndPrint(inputSource);
}

4
js/compiler/README.md Normal file
View File

@ -0,0 +1,4 @@
# Syndicate/js compiler
Translates ES5 + Syndicate extensions to plain ES5 using
[Ohm](https://github.com/cdglabs/ohm#readme).

View File

@ -316,27 +316,22 @@ semantics.addOperation('pushBindings(accumulator)', {
}
})
function compileExtendedSource(inputSource) {
function compileSyndicateSource(inputSource, onError) {
var parseResult = grammar.match(inputSource);
if (parseResult.failed()) console.error(parseResult.message);
return parseResult.succeeded() && semantics(parseResult).asES5;
}
function compileAndPrint(inputSource) {
var translatedSource = compileExtendedSource(inputSource);
if (translatedSource) {
console.log('"use strict";');
console.log(translatedSource);
if (parseResult.failed()) {
if (onError) {
return onError(parseResult.message, parseResult);
} else {
console.error(parseResult.message);
return false;
}
} else {
return '"use strict";\n' + semantics(parseResult).asES5;
}
}
if (process.argv.length < 3 || process.argv[2] === '-') {
var inputSource = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(buf) { inputSource += buf; });
process.stdin.on('end', function() { compileAndPrint(inputSource); });
} else {
var inputSource = fs.readFileSync(process.argv[2]).toString();
compileAndPrint(inputSource);
}
//---------------------------------------------------------------------------
module.exports.grammar = grammar;
module.exports.semantics = semantics;
module.exports.compileSyndicateSource = compileSyndicateSource;

View File

@ -1,3 +1,4 @@
// -*- javascript -*-
// Syntactic extensions to ES5 for Syndicate/js. See compiler.js for
// the rest of the translator.

View File

@ -8,11 +8,15 @@
"type": "git",
"url": "git://github.com/tonyg/syndicate"
},
"directories": {
"bin": "./bin"
},
"scripts": {
"clean": "rm -f dist/*",
"build-debug": "browserify src/main.js -d -s Syndicate -o dist/syndicate.js",
"build-min": "browserify src/main.js -s Syndicate -o dist/_syndicate.js && uglifyjs dist/_syndicate.js -o dist/syndicate.min.js && rm dist/_syndicate.js",
"build": "npm run build-debug && npm run build-min",
"build-compiler": "browserify compiler/compiler.js -s SyndicateCompiler -o dist/_syndicatecompiler.js && uglifyjs dist/_syndicatecompiler.js -o dist/syndicatecompiler.min.js && rm dist/_syndicatecompiler.js",
"build": "npm run build-debug && npm run build-min && npm run build-compiler",
"watch": "watchify src/main.js -d -s Syndicate -o dist/syndicate.js",
"test": "mocha",
"prepublish": "npm run build"