diff --git a/src/debugger/eval.rs b/src/debugger/eval.rs index aeb0ca5..a3cb3ac 100644 --- a/src/debugger/eval.rs +++ b/src/debugger/eval.rs @@ -1,19 +1,17 @@ use miette::Result; use crate::{ - air::AirStmt, - lexer::{cursor::Cursor, TokenKind}, + air::{AirStmt, AsmLine}, runtime::RunState, - symbol::InstrKind, AsmParser, }; -pub fn run(state: &mut RunState, line: String) { - let stmt = parse(line); - - println!("{:#?}", stmt); - - todo!(); +pub fn run(state: &mut RunState, line: String) -> Result<()> { + let stmt = parse(line)?; + let line = AsmLine::new(0, stmt); + let instr = line.emit()?; + execute(state, instr); + Ok(()) } fn parse(line: String) -> Result { @@ -21,10 +19,12 @@ fn parse(line: String) -> Result { let line = Box::leak(line.into_boxed_str()); let mut parser = AsmParser::new_simple(line)?; - let stmt = parser.parse_simple()?; - println!("{:#?}", stmt); + Ok(stmt) +} - todo!(); +fn execute(state: &mut RunState, instr: u16) { + let opcode = (instr >> 12) as usize; + RunState::OP_TABLE[opcode](state, instr); } diff --git a/src/debugger/mod.rs b/src/debugger/mod.rs index 83269a9..6c26875 100644 --- a/src/debugger/mod.rs +++ b/src/debugger/mod.rs @@ -341,7 +341,9 @@ impl Debugger { Command::Eval { instruction } => { self.was_pc_changed = true; dprintln!(Always, "Eval: <{}>", instruction); - eval::run(state, instruction); + if let Err(err) = eval::run(state, instruction) { + eprintln!("{:?}", err); + } } Command::BreakAdd { location } => { diff --git a/src/parser.rs b/src/parser.rs index 7489377..6e2645d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -88,20 +88,17 @@ fn preprocess_simple(src: &'static str) -> Result> { loop { let token = cur.advance_real()?; match token.kind { - TokenKind::Instr(_) | TokenKind::Trap(_) => res.push(token), + TokenKind::Dir(_) => { + // TODO(feat): Handle error + panic!("unexpected directive"); + } + TokenKind::Byte(_) => unreachable!("Found byte in stream"), + TokenKind::Breakpoint => unreachable!("Found breakpoint in stream"), TokenKind::Comment | TokenKind::Whitespace => continue, TokenKind::Eof => break, - TokenKind::Dir(_) - | TokenKind::Label - | TokenKind::Lit(_) - | TokenKind::Reg(_) - | TokenKind::Byte(_) - | TokenKind::Breakpoint => { - // TODO(feat): Handle error - panic!("unexpected token `{:?}`", token.kind); - } + _ => res.push(token), } } @@ -240,7 +237,7 @@ impl AsmParser { pub fn parse_simple(&mut self) -> Result { let Some(tok) = self.toks.next() else { // TODO(feat): Handle error - panic!("unexpected eof (possibly unreachable)"); + panic!("unexpected eof"); }; match tok.kind { diff --git a/src/runtime.rs b/src/runtime.rs index 28451a1..4a6a554 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -173,7 +173,7 @@ impl RunEnvironment { } impl RunState { - const OP_TABLE: [fn(&mut RunState, u16); 16] = [ + pub const OP_TABLE: [fn(&mut RunState, u16); 16] = [ Self::br, // 0x0 Self::add, // 0x1 Self::ld, // 0x2