From ab84ad75629b0a4124221023ca91411d2cd62a32 Mon Sep 17 00:00:00 2001
From: Ben Bridle <ben@derelict.engineering>
Date: Tue, 25 Mar 2025 12:46:49 +1300
Subject: Restructure program

This commit also includes changes to devices according to the latest
devices specification, in particular the math and system devices.
---
 src/bin/br/main.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100644 src/bin/br/main.rs

(limited to 'src/bin/br/main.rs')

diff --git a/src/bin/br/main.rs b/src/bin/br/main.rs
new file mode 100644
index 0000000..52c53b0
--- /dev/null
+++ b/src/bin/br/main.rs
@@ -0,0 +1,111 @@
+#![feature(path_add_extension)]
+
+mod asm;
+mod run;
+mod formats;
+
+use switchboard::*;
+
+
+fn main() {
+    let mut args = Switchboard::from_env();
+    args.named("help").short('h');
+    args.named("version");
+    args.named("verbose").short('v');
+
+    if args.get("help").as_bool() {
+        print_help();
+    }
+    if args.get("version").as_bool() {
+        print_version();
+    }
+    if args.get("verbose").as_bool() {
+        log::set_log_level(log::LogLevel::Info);
+    }
+    match args.peek() {
+        Some("run") => { args.pop(); run::main(args) },
+        Some("asm") => { args.pop(); asm::main(args) },
+        _ => run::main(args),
+    }
+}
+
+
+fn print_help() -> ! {
+    eprintln!("\
+Usage: br [source]
+       br asm [source] [destination]
+
+Integrated Bedrock assembler and emulator.
+
+Usage:
+  To load a Bedrock program from a file, run `br <path>`, where
+  <path> is the path to an assembled Bedrock program.
+
+  To load a Bedrock program from piped input, run `<command> | br`,
+  where <command> is a command that generates Bedrock bytecode.
+
+  To assemble a Bedrock program from a source file and write to an
+  output file, run `br asm <source> <output>`, where <source> is the
+  path of the source file and <output> is the path to write to.
+
+  To assemble and run a Bedrock program without saving to a file,
+  run `br asm <source> | br`, where <source> is the path of the
+  source file.
+
+Environment variables:
+  BEDROCK_PATH
+    A list of colon-separated paths which will be searched to find
+    a Bedrock program when the program doesn't exist in the current
+    directory. This allows the user to run frequently-used programs
+    from any directory.
+  BEDROCK_LIBS
+    A list of colon-separated paths which will be searched to find
+    Bedrock source code files to use as libraries when assembling a
+    Bedrock program. If a library file resolves an unresolved symbol
+    in the program being assembled, the library file will be merged
+    into the program.
+
+Arguments:
+  [program]            Path to a Bedrock program to run
+
+Switches:
+  --fullscreen     (-f)  Start the program in fullscreen mode (toggle with F11)
+  --size=<dim>     (-s)  Set the initial window size in the format <width>x<height>
+  --zoom=<scale>   (-z)  Set the pixel size for the screen (change with F5/F6)
+  --show-cursor    (-c)  Show the operating system cursor over the window
+  --palette=<pal>        Set a debug colour palette in the format <rgb>,... (toggle with F2)
+  --debug,         (-d)  Show debug information while the program is running
+  --decode-stdin   (-i)  Decode transmissions on standard input from text lines.
+  --encode-stdout  (-o)  Encode transmissions on standard output as text lines.
+  --help           (-h)  Print this help information
+  --verbose,       (-v)  Print additional information
+  --version              Print the program version and exit
+
+Arguments (asm mode):
+  [source]               Bedrock source code file to assemble.
+  [destination]          Destination path for assembler output.
+
+Switches (asm mode):
+  --dry-run        (-n)  Assemble and show errors only, don't write any output
+  --extension            File extension to identify source files (default is 'brc')
+  --format=<fmt>         Output format to use for assembled program (default is 'binary')
+  --no-project-libs      Don't search for libraries in the source parent folder
+  --no-env-libs          Don't search for libraries in the BEDROCK_LIBS path variable
+  --no-libs              Combination of --no-project-libs and --no-env-libs
+  --tree                 Display a tree visualisation of all included library files
+  --with-symbols         Also generate debug symbols file with extension '.sym'
+  --help           (-h)  Print this help information
+  --verbose,       (-v)  Print additional information
+  --version              Print the program version and exit
+");
+    std::process::exit(0);
+}
+
+
+fn print_version() -> ! {
+    let name = env!("CARGO_PKG_NAME");
+    let version = env!("CARGO_PKG_VERSION");
+    eprintln!("{name} v{version}");
+    eprintln!("Written by Ben Bridle.");
+    std::process::exit(0);
+}
-- 
cgit v1.2.3-70-g09d2