summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2023-11-26 10:29:55 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2023-11-26 10:33:01 +1300
commit8dfdd603b022ab9775cda08b9c2350d43667ff0f (patch)
tree016f51ecb50af368b37c4eac3c594d1375b7c8c2
parentd40c1f01ab3d30ee58ce56a907569c18f5441b17 (diff)
downloadbedrock-asm-8dfdd603b022ab9775cda08b9c2350d43667ff0f.zip
Exit with status 1 on assembly error
This will break unix pipelines in order to prevent any emulator down the pipeline from attempting to run a malassembled program.
-rw-r--r--src/main.rs8
-rw-r--r--src/semantic_token.rs11
2 files changed, 15 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index c7d3590..8d6e186 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,11 +12,17 @@ fn main() {
};
let (bytecode, tokens) = assemble(&source_code);
+ let mut is_error = false;
for token in &tokens {
- token.print_error(&source_code); }
+ if token.print_error(&source_code) { is_error = true };
+ }
eprintln!("Assembled program in {} bytes.", bytecode.len());
let bytecode_len = bytecode.len();
+ if is_error {
+ std::process::exit(1)
+ }
+
// Write bytecode to standard output
let mut stdout = std::io::stdout().lock();
match stdout.write(&bytecode) {
diff --git a/src/semantic_token.rs b/src/semantic_token.rs
index 3d08b25..77ba892 100644
--- a/src/semantic_token.rs
+++ b/src/semantic_token.rs
@@ -24,11 +24,15 @@ pub struct SemanticToken {
}
impl SemanticToken {
- pub fn print_error(&self, source_code: &str) {
+ /// Returns true if an error was printed.
+ pub fn print_error(&self, source_code: &str) -> bool {
+ let mut is_error = false;
macro_rules! red {()=>{eprint!("\x1b[31m")};}
macro_rules! normal {()=>{eprint!("\x1b[0m")};}
if let SemanticTokenType::Error(token, error) = &self.r#type {
+ is_error = true;
+
red!(); eprint!("[ERROR] "); normal!();
let source = &self.source_location.source;
match error {
@@ -70,11 +74,12 @@ impl SemanticToken {
}
normal!(); eprintln!();
}
- if let SemanticTokenType::MacroDefinition(definition) = &self.r#type {
+ else if let SemanticTokenType::MacroDefinition(definition) = &self.r#type {
for token in &definition.body_tokens {
- token.print_error(source_code);
+ if token.print_error(source_code) { is_error = true }
}
}
+ is_error
}
}