diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2023-05-08 12:05:57 +1200 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2023-05-08 12:05:57 +1200 |
commit | afa81e9ae6a56efe2eae2990e09c672b74328715 (patch) | |
tree | e13ceca3104a8f4bded3668f8efd743bcfbe4e35 /src/main.rs | |
parent | e38f108921c61e1e66d65a368f2a67a763d61e69 (diff) | |
download | bedrock-asm-afa81e9ae6a56efe2eae2990e09c672b74328715.zip |
Added detection of cyclic macro references, and made assembler binary usable
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); + } + } } + |