feat(cli): accept source input from standard input
2 files changed, +13 −4
- cli/driver.ML modified +5 −4
- tests/cli.py modified +8 −0
cli/driver.ML modified
| @@ -6,10 +6,11 @@ fun text cs = String.implode (map (Char.chr o IntInf.toInt o Remy.ordinal) cs); | ||
| 6 | 6 | |
| 7 | 7 | fun read path = |
| 8 | 8 | let |
| 9 | - val stream = TextIO.openIn path | |
| 9 | + val stream = if path = "-" then TextIO.stdIn else TextIO.openIn path | |
| 10 | + fun close () = if path = "-" then () else TextIO.closeIn stream | |
| 10 | 11 | val input = TextIO.inputN (stream, 65537) |
| 11 | - handle exn => (TextIO.closeIn stream; raise exn) | |
| 12 | - val _ = TextIO.closeIn stream | |
| 12 | + handle exn => (close (); raise exn) | |
| 13 | + val _ = close () | |
| 13 | 14 | in |
| 14 | 15 | if size input > 65536 then raise Fail "source exceeds 64 KiB" else chars input |
| 15 | 16 | end; |
| @@ -71,7 +72,7 @@ fun main () = | ||
| 71 | 72 | | ["check", binary] => output (Remy.validate (read binary)) |
| 72 | 73 | | ["disassemble", binary] => output (Remy.disassemble (read binary)) |
| 73 | 74 | | [source, destination] => emit destination (Remy.artifact (read source)) |
| 74 | - | _ => raise Fail "usage: remy check binary | remy disassemble binary | remy limits depth nodes stack source binary | remy source binary | remy vectors source data | remy eval source x y t") | |
| 75 | + | _ => raise Fail "usage: remy check binary | remy disassemble binary | remy limits depth nodes stack source binary | remy source binary | remy vectors source data | remy eval source x y t (source - reads standard input)") | |
| 75 | 76 | handle exn => |
| 76 | 77 | (TextIO.output (TextIO.stdErr, "remy: " ^ General.exnMessage exn ^ "\n"); |
| 77 | 78 | OS.Process.exit OS.Process.failure); |
tests/cli.py modified
| @@ -86,6 +86,14 @@ def main(): | ||
| 86 | 86 | source.write_text('(+ x y)') |
| 87 | 87 | invoke(source,source) |
| 88 | 88 | assert source.read_bytes().endswith(bytes([195])) |
| 89 | + invoke('-',binary,data='(+ x y)') | |
| 90 | + assert int(invoke('eval','-',3,4,0,data='(+ x y)')) == 7 | |
| 91 | + invoke('vectors','-',binary,data='(+ x y)') | |
| 92 | + assert binary.stat().st_size == 16384 | |
| 93 | + for data in ['(+ x)', ' ' * 65537]: | |
| 94 | + binary.write_bytes(b'preserve') | |
| 95 | + invoke('-',binary,data=data,ok=False) | |
| 96 | + assert binary.read_bytes() == b'preserve' | |
| 89 | 97 | source.write_text('é') |
| 90 | 98 | assert 'ASCII' in invoke(source,binary,ok=False) |
| 91 | 99 | for text in ['0x','0x10000000000000000','0xfg','0xffextra','-0x1']: |