-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed lispy command, added more features
- Loading branch information
1 parent
8f5fd6d
commit 8b94510
Showing
6 changed files
with
249 additions
and
133 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,2 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../lib/lispy').run(); | ||
require('../lib/lispy'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,117 @@ | ||
;; The lispy command script | ||
|
||
(require "./node") | ||
(var fs (require "fs")) | ||
(var path (require "path")) | ||
(var ls (require "../lib/ls")) | ||
(var repl (require "./repl")) | ||
|
||
;; node js error msg for sync io only reports libuv errno, so we make | ||
;; it a little more palatable. | ||
(var fileErrorMsg "File Error - %s | ||
This usually happens when an input file is not found | ||
or when an output file cannot be written (permission denied).") | ||
|
||
(var exit | ||
(function (error) | ||
(if error | ||
(do | ||
(if error.path ;; file err will have error.path | ||
(console.error | ||
fileErrorMsg | ||
error.path) | ||
(console.error error)) | ||
(process.exit 1)) | ||
(process.exit 0)))) | ||
|
||
(var compileFiles | ||
(function (input output) | ||
(compile | ||
(fs.createReadStream input) | ||
(fs.createWriteStream output) | ||
(path.resolve input)))) | ||
|
||
(var compile | ||
(function (input output uri) | ||
(var source "") | ||
;; Accumulate text form input until it ends. | ||
(input.on "data" | ||
(function (chunck) | ||
(set source (+ source (chunck.toString))))) | ||
;; Once input ends try to compile & write to output. | ||
(input.on "end" | ||
(function () | ||
(var isValidFlag /-h\b|-r\b|-v\b/) | ||
|
||
(var error | ||
(function (err) | ||
(console.error err.message) | ||
(process.exit 1))) | ||
|
||
(var help_str " | ||
Usage: lispy [-h] [-r] [<infile>] [<outfile>] | ||
Also compile stdin to stdout | ||
eg. $ echo '(console.log \"hello\")' | lispy | ||
<no arguments> Run REPL | ||
-h Show this help | ||
-r Compile and run | ||
-v Show Version | ||
<infile> Input file to compile | ||
<outfile> Output JS file. If not given | ||
<outfile> will be <infile> with .js extension\n") | ||
|
||
;; We use maybe monad to carry out each step, so that we can | ||
;; halt the operation anytime in between if needed. | ||
|
||
(doMonad maybeMonad | ||
|
||
;; Start maybe Monad bindings | ||
;; First step get args without the first two (node and lispy). | ||
(args (process.argv.slice 2) | ||
|
||
;; get the first arg | ||
arg1 (args.shift) | ||
|
||
;; when no args do stdin -> stdout compile or run repl and return null to | ||
;; halt operations. | ||
noargs | ||
(when (undefined? arg1) | ||
(var input process.stdin) | ||
(var output process.stdout) | ||
(input.resume) | ||
(input.setEncoding "utf8") | ||
(var source "") | ||
;; Accumulate text form input until it ends. | ||
(input.on "data" | ||
(function (chunck) | ||
(set source (+ source (chunck.toString))))) | ||
;; Once input ends try to compile & write to output. | ||
(input.on "end" | ||
(function () | ||
(try | ||
(output.write (ls._compile source uri)) | ||
exit))) | ||
(input.on "error" exit) | ||
(output.on "error" exit))) | ||
|
||
(set exports.run | ||
(function () | ||
(if (= process.argv.length 2) | ||
(do | ||
(process.stdin.resume) | ||
(process.stdin.setEncoding "utf8") | ||
(compile process.stdin process.stdout (process.cwd)) | ||
(setTimeout | ||
(function () | ||
(if (= process.stdin.bytesRead 0) | ||
(do | ||
(process.stdin.removeAllListeners "data") | ||
(repl.runrepl)))) 20)) | ||
|
||
(if (= process.argv.length 3) | ||
(do | ||
(var i (get 2 process.argv)) | ||
(var o (i.replace /\.ls$/ ".js")) | ||
(if (= i o) | ||
(console.log "Input file must have extension '.ls'") | ||
(compileFiles i o))) | ||
(compileFiles (get 2 process.argv) (get 3 process.argv)))))) | ||
(output.write (ls._compile source process.cwd)) | ||
error))) | ||
(input.on "error" error) | ||
(output.on "error" error) | ||
(setTimeout | ||
(function () | ||
(if (= input.bytesRead 0) | ||
(do | ||
(input.removeAllListeners "data") | ||
(repl.runrepl)))) 20) | ||
null) | ||
|
||
;; If arg1 = flag verify valid flag and halt if not otherwise | ||
;; set arg1 to next arg in args | ||
flag | ||
(when (= "-" (get 0 arg1)) | ||
(var flag arg1) | ||
(set arg1 (args.shift)) | ||
(if (isValidFlag.test flag) | ||
flag | ||
(error (new Error (+ "Error: Invalid flag " flag))))) | ||
|
||
run | ||
(cond | ||
(= "-h" flag) (do (console.log help_str) null) | ||
(= "-v" flag) (do (console.log (+ "Version " ls.version)) null) | ||
(= "-r" flag) true) | ||
|
||
|
||
;; if infile undefined | ||
infile | ||
(if arg1 | ||
arg1 | ||
(error (new Error "Error: No Input file given"))) | ||
|
||
;; set outfile args.shift. ! outfile set outfile to infile(.js) | ||
outfile | ||
(do | ||
(var outfile (args.shift)) | ||
(unless outfile | ||
(set outfile (infile.replace /\.ls$/ ".js")) | ||
(if (= outfile infile) | ||
(error (new Error "Error: Input file must have extension '.ls'")))) | ||
outfile) | ||
|
||
;; compile infile to outfile. if not run return null. | ||
js | ||
(try | ||
(fs.writeFileSync outfile | ||
(ls._compile | ||
(fs.readFileSync infile "utf8") | ||
infile) | ||
"utf8") | ||
(if run run null) | ||
(function (err) | ||
(error err) | ||
null))) ;; end of maybe Monad bindings | ||
|
||
;; we are here if -r true, so run it! | ||
(-> | ||
(require "child_process") | ||
(.spawn "node" [outfile] {stdio: "inherit"}))) |
Oops, something went wrong.