Skip to content

Commit 778dd05

Browse files
committed
add assembly dumping
1 parent 10f27be commit 778dd05

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/emitter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub struct Program {
4242

4343
/// Counts for each instruction type that was emitted
4444
instr_type_counts: HashMap<InstrType, u32>,
45+
46+
/// Current line, starting at 0
47+
pub line: u32
4548
}
4649

4750
impl Program {

src/main.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use std::{fs::File, io::Read, path::PathBuf};
88

99
use clap::{Parser, Subcommand};
10+
use color_eyre::{
11+
Section, SectionExt,
12+
owo_colors::{AnsiColors, OwoColorize},
13+
};
1014
use env_logger::{Builder, Env};
1115

1216
use crate::{emitter::Program, parser::document, tokeniser::lex};
@@ -71,9 +75,27 @@ fn main() -> color_eyre::Result<()> {
7175
// add extra newline in case file doesn't have its own
7276
string += "\n";
7377

78+
let lines: Vec<String> = string.lines().map(|x| x.into()).collect();
79+
7480
let mut tokens = lex(string.as_str());
7581
let mut prog = Program::default();
76-
document(&mut tokens, &mut prog, relaxed)?;
82+
let result = document(&mut tokens, &mut prog, relaxed);
83+
84+
match result {
85+
Ok(_) => {}
86+
Err(error) => {
87+
let index = prog.line;
88+
let line = match lines.get::<usize>(index as usize) {
89+
Some(l) => l,
90+
None => "error fetching context",
91+
};
92+
// TODO if we're not in --relaxed mode, suggest running --relaxed
93+
return Err(error.with_section(move || {
94+
format!("{} | {}", index, line)
95+
.header("Assembly context:".color(AnsiColors::Green))
96+
}));
97+
}
98+
}
7799
}
78100
Commands::Version {} => {
79101
println!(

src/parser.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,16 +490,35 @@ pub fn document(
490490
prog.flush()?;
491491
// skip newline
492492
lexer.next();
493+
prog.line += 1;
493494
continue;
494495
}
495496

496497
// first try match a define
497498
// if a line starts with an ident, we assume they're trying to write a define
498499
if tok.is_ident() {
499500
lexer.next();
501+
502+
// in relaxed mode, they might have intended it to be a label
503+
if relaxed && token(lexer)? != T::Equals {
504+
// TODO we should actually check this is valid to do right
505+
debug!("Trying to recover ident -> label in relaxed mode");
506+
match tok {
507+
T::Ident(lab) => {
508+
prog.add_label(lab);
509+
}
510+
_ => {
511+
panic!("Internal error: Should have been an ident!");
512+
}
513+
}
514+
continue;
515+
}
516+
517+
// normal non-relaxed mode
500518
// should be X = Y; check eq
501519
expect(&T::Equals, lexer)?;
502520
let num = num(lexer)?;
521+
// TODO handle this
503522
continue;
504523
}
505524

0 commit comments

Comments
 (0)