use std::fmt::Debug; use crate::asm::error::Error; use crate::asm::parse::arg_parser::ArgParser; use crate::builtin::defs::BuiltinOp; use crate::runtime::fault::Fault; use crate::runtime::run_thread::{state::RunState, ThreadInfo}; /// A higher level simple opration #[derive(Debug)] pub enum Op { BuiltIn(BuiltinOp), /// Instruction added by an extension Ext(Box), } pub trait OpTrait: Debug + Send + Sync + 'static { fn execute(&self, ti : &ThreadInfo, state: &mut RunState) -> Result; } pub enum ParseOpResult { Parsed(Op), Unknown(ArgParser), } pub trait AsmModule: Debug + Send + 'static { /// Get name of the module fn name(&self) -> &'static str; /// Parse an op. /// If the keyword matches and the function decides to parse the instruction, it must consume /// the argument list and either return Ok or Err. /// /// If the instruction keyword is not recognized, return Unknown with the unchanged argument list. fn parse_op(&self, keyword: &str, arg_tokens: ArgParser) -> Result; } pub type CyclesSpent = usize; #[derive(Debug)] pub struct EvalRes { pub cycles: CyclesSpent, pub advance: i64, } impl Default for EvalRes { fn default() -> Self { Self { cycles: 1, advance: 1, } } } impl OpTrait for Op { fn execute(&self, ti : &ThreadInfo, state: &mut RunState) -> Result { match self { Op::BuiltIn(op) => { op.execute(ti, state) } Op::Ext(op) => { op.execute(ti, state) } } } }