Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
443f5ca
commit 2fe910e
Showing
5 changed files
with
73 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,69 @@ | ||
import JokeEngine from 'joke'; | ||
import fs from 'fs'; | ||
|
||
if (!process.argv[2]) { | ||
console.log(`Usage: node joke.js sourceFile`); | ||
process.exit(0); | ||
} | ||
import readline from 'readline'; | ||
|
||
let debug = false; | ||
let argc = 2; | ||
while (true) { | ||
if (process.argv[argc][0] != '-') { | ||
if (!process.argv[argc] || process.argv[argc][0] != '-') { | ||
break; | ||
} | ||
|
||
switch (process.argv[argc]) { | ||
case '-d': | ||
debug = true; | ||
break; | ||
case '-h': | ||
console.log('Usage: node joke.js [options] [sourceFile]\n'); | ||
console.log('Options:'); | ||
console.group(); | ||
console.log('-d\tEnable debug mode (dump compiler output)') | ||
console.groupEnd(); | ||
process.exit(0); | ||
} | ||
argc++; | ||
} | ||
|
||
const sourceFile = process.argv[argc]; | ||
fs.readFile(sourceFile, 'utf8', (err, data) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
function runSourceFile(sourceFile) { | ||
fs.readFile(sourceFile, 'utf8', (err, data) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
try { | ||
const joke = new JokeEngine(debug); | ||
joke.run(sourceFile, data); | ||
} catch (e) { | ||
console.error(`${e.name}: ${e.message}`); | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
const joke = new JokeEngine(debug); | ||
joke.run(sourceFile, data); | ||
} catch (e) { | ||
console.error(`${e.name}: ${e.message}`); | ||
} | ||
}); | ||
function executeRepl() { | ||
const joke = new JokeEngine(debug); | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
prompt: '> ' | ||
}); | ||
rl.on('line', (line) => { | ||
try { | ||
joke.run('<stdin>', line); | ||
} catch (e) { | ||
console.error(`${e.name}: ${e.message}`); | ||
} | ||
rl.prompt(); | ||
}); | ||
rl.on('close', () => { | ||
process.exit(0); | ||
}) | ||
rl.prompt(); | ||
} | ||
|
||
const sourceFile = process.argv[argc]; | ||
if (sourceFile) { | ||
runSourceFile(sourceFile); | ||
} else { | ||
executeRepl(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters