small changes

This commit is contained in:
voidNUL 2024-02-25 09:35:06 -06:00
parent dd9a0c6f4e
commit dd0f64c859
3 changed files with 16 additions and 3 deletions

View file

@ -100,7 +100,7 @@ pub fn assemble(input: &str) -> Result<Vec<Word>, TaggedAsmError> {
if let Some(l) = last { if let Some(l) = last {
Err(TaggedAsmError { Err(TaggedAsmError {
kind: AsmError::ExpectedNum, kind: AsmError::ExpectedNum,
word: l.to_string(), word: "[END OF FILE]".to_string(),
}) })
} else { } else {
Ok(output) Ok(output)

View file

@ -18,6 +18,7 @@ pub struct Machine {
ip: Word, ip: Word,
ebp: Word, ebp: Word,
phase: Phase, phase: Phase,
clock: usize,
pub log: String, pub log: String,
pub cstack: Vec<Word>, pub cstack: Vec<Word>,
pub pstack: Vec<Word>, pub pstack: Vec<Word>,
@ -32,6 +33,7 @@ impl Machine {
ip: 0, ip: 0,
ebp: 0, ebp: 0,
phase: Phase::Warmup, phase: Phase::Warmup,
clock: 0,
log: "".to_string(), log: "".to_string(),
cstack: vec![], cstack: vec![],
pstack: vec![], pstack: vec![],
@ -106,6 +108,10 @@ impl Machine {
} }
pub fn step(&mut self) -> CResult { pub fn step(&mut self) -> CResult {
self.clock += 1;
if self.clock > 256 {
return Err(Error::ExecLimit)
}
let word = self let word = self
.instructions .instructions
.get(self.ip as usize) .get(self.ip as usize)
@ -363,7 +369,7 @@ impl Machine {
self.phase = Phase::Recital; self.phase = Phase::Recital;
self.ip = match self.fstack.last() { self.ip = match self.fstack.last() {
Some(i) => *i, Some(i) => *i,
None => return Err(Error::StackUnderflow), None => return Err(Error::NoFunc),
} }
}, },
OpKind::Push => { OpKind::Push => {

View file

@ -18,6 +18,8 @@ pub enum Error {
BadCall, BadCall,
NoExec, NoExec,
IllegalOp, IllegalOp,
NoFunc,
ExecLimit,
EndReached, EndReached,
} }
@ -30,13 +32,18 @@ impl Display for Error {
Self::WriteOob => write!(f, "Memory write out of bounds"), Self::WriteOob => write!(f, "Memory write out of bounds"),
Self::ReadOob => write!(f, "Memory read out of bounds"), Self::ReadOob => write!(f, "Memory read out of bounds"),
Self::IllegalOp => write!(f, "Illegal instruction reached"), Self::IllegalOp => write!(f, "Illegal instruction reached"),
Self::BadCall => { Self::BadCall => {
write!(f, "Attempted to call function which does not exist") write!(f, "Attempted to call function which does not exist")
}, },
Self::NoExec => write!( Self::NoExec => write!(
f, f,
"No exec instruction found. Are you trying to execute a library?" "No exe instruction found. Are you trying to execute a library?"
), ),
Self::NoFunc => {
write!(f, "Cannot begin Recital, no functions have been defined")
},
Self::ExecLimit => write!(f, "Execution limit reached, compile and run natively to do really big stuff!"),
Self::EndReached => write!(f, "Successfully terminated"), Self::EndReached => write!(f, "Successfully terminated"),
} }
} }