| 1 | val root = valOf (OS.Process.getEnv "REMY_ROOT"); |
| 2 | val _ = use (root ^ "/.build/Remy.Export/code/remy.ML"); |
| 3 | |
| 4 | fun chars s = map (Remy.ascii o IntInf.fromInt o Char.ord) (String.explode s); |
| 5 | fun text cs = String.implode (map (Char.chr o IntInf.toInt o Remy.ordinal) cs); |
| 6 | |
| 7 | fun read path = |
| 8 | let |
| 9 | val stream = TextIO.openIn path |
| 10 | val input = TextIO.inputN (stream, 65537) |
| 11 | handle exn => (TextIO.closeIn stream; raise exn) |
| 12 | val _ = TextIO.closeIn stream |
| 13 | in |
| 14 | if size input > 65536 then raise Fail "source exceeds 64 KiB" else chars input |
| 15 | end; |
| 16 | |
| 17 | fun write path bytes = |
| 18 | let |
| 19 | val stream = BinIO.openOut path |
| 20 | in |
| 21 | (BinIO.output (stream, Byte.stringToBytes (text bytes)); BinIO.closeOut stream) |
| 22 | handle exn => ((BinIO.closeOut stream handle _ => ()); raise exn) |
| 23 | end; |
| 24 | |
| 25 | fun output (ok, message) = |
| 26 | (TextIO.output (if ok then TextIO.stdOut else TextIO.stdErr, text message); |
| 27 | if ok then () else OS.Process.exit OS.Process.failure); |
| 28 | |
| 29 | fun emit destination ((ok, bytes), report) = |
| 30 | if ok then (write destination bytes; output (true,report)) else output (false,report); |
| 31 | |
| 32 | fun main () = |
| 33 | (case CommandLine.arguments () of |
| 34 | ["vectors", source, destination] => |
| 35 | (case Remy.vectors (read source) of (true, bytes) => write destination bytes | result => output result) |
| 36 | | ["eval", source, x, y, t] => output (Remy.reference (read source) (chars x) (chars y) (chars t)) |
| 37 | | ["limits", depth, nodes, stack, source, destination] => |
| 38 | emit destination (Remy.configured (chars depth) (chars nodes) (chars stack) (read source)) |
| 39 | | [source, destination] => emit destination (Remy.artifact (read source)) |
| 40 | | _ => raise Fail "usage: remy limits depth nodes stack source binary | remy source binary | remy vectors source data | remy eval source x y t") |
| 41 | handle exn => |
| 42 | (TextIO.output (TextIO.stdErr, "remy: " ^ General.exnMessage exn ^ "\n"); |
| 43 | OS.Process.exit OS.Process.failure); |