~james/span

1
a68ad347f48eb214044370b3ca67d33d9b3c9a6d
main
span/cli/driver.ML -rw-r--r--2.2 KiB ViewLogBlameView rawPermalink a68ad347f4 initial commit 13 days ago
1val root = valOf (OS.Process.getEnv "SPAN_ROOT");
2val _ = use (root ^ "/.build/Span.Export/code/span.ML");
3
4fun chars s = map (Span.ascii o IntInf.fromInt o Char.ord) (String.explode s);
5fun text cs = String.implode (map (Char.chr o IntInf.toInt o Span.ordinal) cs);
6
7fun read path =
8 let
9 val stream = if path = "-" then TextIO.stdIn else TextIO.openIn path
10 val text = TextIO.inputN (stream, 1048577)
11 val _ = if path = "-" then () else TextIO.closeIn stream
12 in
13 if size text > 1048576 then raise Fail "input exceeds 1 MiB" else text
14 end;
15
16fun write path text =
17 let
18 val stream = BinIO.openOut path
19 val _ = BinIO.output (stream, Byte.stringToBytes text)
20 in BinIO.closeOut stream end;
21
22fun output (ok, message) =
23 (TextIO.output (if ok then TextIO.stdOut else TextIO.stdErr, text message);
24 OS.Process.exit (if ok then OS.Process.success else OS.Process.failure));
25
26fun usage () =
27 (TextIO.output (TextIO.stdOut,
28 "usage: span assemble file [-o binary]\n span trace file\n span list file\n span run file [fuel]\n span disassemble binary\n\nuse - to read standard input\n");
29 OS.Process.exit OS.Process.success);
30
31fun run args =
32 case args of
33 [] => usage ()
34 | ["--help"] => usage ()
35 | ["disassemble", file] => output (Span.disassemble (chars (read file)))
36 | ["assemble", file, "-o", destination] =>
37 let
38 val input = chars (read file)
39 val (ok, bytes) = Span.binary input
40 in
41 if ok then
42 (write destination (text bytes);
43 output (Span.process [chars "assemble"] input))
44 else output (false, bytes)
45 end
46 | ["run", file] => output (Span.process (map chars ["run", "4096"]) (chars (read file)))
47 | ["run", file, fuel] => output (Span.process (map chars ["run", fuel]) (chars (read file)))
48 | [mode, file] =>
49 if List.exists (fn x => x = mode) ["assemble", "trace", "list"] then
50 output (Span.process [chars mode] (chars (read file)))
51 else raise Fail "unknown command, use span --help"
52 | _ => raise Fail "invalid arguments, use span --help";
53
54fun main () = run (CommandLine.arguments ())
55 handle exn =>
56 (TextIO.output (TextIO.stdErr, "span: " ^ General.exnMessage exn ^ "\n");
57 OS.Process.exit OS.Process.failure);