Skip to content

Commit

Permalink
feat: execute eval instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Dec 4, 2024
1 parent 7b89f4c commit cb393d0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
24 changes: 12 additions & 12 deletions src/debugger/eval.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
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<AirStmt> {
// TODO(fix): This is a TERRIBLE solution. Ideally cursor doesn't take &'static str ??
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);
}
4 changes: 3 additions & 1 deletion src/debugger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand Down
19 changes: 8 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,17 @@ fn preprocess_simple(src: &'static str) -> Result<Vec<Token>> {
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),
}
}

Expand Down Expand Up @@ -240,7 +237,7 @@ impl AsmParser {
pub fn parse_simple(&mut self) -> Result<AirStmt> {
let Some(tok) = self.toks.next() else {
// TODO(feat): Handle error
panic!("unexpected eof (possibly unreachable)");
panic!("unexpected eof");
};

match tok.kind {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb393d0

Please sign in to comment.