diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 64 |
1 files changed, 28 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs index 82bd92d..c7d3590 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,41 +1,33 @@ -const SOURCE:&'static str = -// " -// %SCREEN-SHUNT 00; -// %SCREEN-DRAW 00; - -// @draw_horizontal_line ( len* clr -- ) -// (1) PSHr ( len* | clr ) -// (4) PSH:41 STD:SCREEN-SHUNT ( len* | clr ) -// &loop -// (2) SKDr:SCREEN-DRAW ( len* | clr ) -// (4) DEC* JKC*:~loop ( len* | clr ) -// (2) STD:SCREEN-SHUNT -// (3) POP POPr JMPr* -// "; - -" -%RED 1234; -%GREEN 5678 @test; -%BLUE 9ABC; - -@start - RED - start - GREEN - BLUE - $4 - @end -"; - +use std::io::{Read, Write}; use bedrock_asm::*; fn main() { - println!("------- PROGRAM START -------"); - for line in SOURCE.lines() { - println!("{line}"); - } - println!("-------- PROGRAM END --------"); - println!(); + // Read source code from standard input + let mut source_code = String::new(); + let mut stdin = std::io::stdin().lock(); + if let Err(err) = stdin.read_to_string(&mut source_code) { + eprintln!("Could not read from standard input, quitting."); + eprintln!("({err:?})"); + std::process::exit(1); + }; + + let (bytecode, tokens) = assemble(&source_code); + for token in &tokens { + token.print_error(&source_code); } + eprintln!("Assembled program in {} bytes.", bytecode.len()); + let bytecode_len = bytecode.len(); - parse(SOURCE); + // Write bytecode to standard output + let mut stdout = std::io::stdout().lock(); + match stdout.write(&bytecode) { + Ok(len) => if len != bytecode_len { + eprintln!("Only wrote {len} of {bytecode_len} bytes") + } + Err(err) => { + eprintln!("Could not write to standard output, quitting."); + eprintln!("({err:?})"); + std::process::exit(1); + } + } } + |