|
|
@ -3,11 +3,11 @@ use crate::registers::{
|
|
|
|
Rcb, Rcg, Rcr, Rcs, Register, Rgd, Rgi, Rgl, Rgo, Rgp, RCB, RCG, RCR, RGD, RGI, RGL, RGO, RGP,
|
|
|
|
Rcb, Rcg, Rcr, Rcs, Register, Rgd, Rgi, Rgl, Rgo, Rgp, RCB, RCG, RCR, RGD, RGI, RGL, RGO, RGP,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use crate::tokens::{
|
|
|
|
use crate::tokens::{
|
|
|
|
AddToken, ClearToken, CmdToken, CopyToken, DivToken, ExitToken, FromBytecode, GotoToken,
|
|
|
|
AddToken, ClearToken, CmdToken, CopyToken, DebugToken, DivToken, ExitToken, FromBytecode,
|
|
|
|
JeToken, JgToken, JlToken, LabelToken, LoadToken, LshToken, ModToken, MulToken, PauseToken,
|
|
|
|
GotoToken, JeToken, JgToken, JlToken, LabelToken, LoadToken, LshToken, ModToken, MulToken,
|
|
|
|
RshToken, SendToken, SetToken, SubToken, Token, WriteToken, T_ADD, T_CLEAR, T_CMD, T_COPY,
|
|
|
|
PauseToken, RshToken, SendToken, SetToken, SubToken, Token, WriteToken, T_ADD, T_CLEAR, T_CMD,
|
|
|
|
T_DIV, T_EXIT, T_GOTO, T_JE, T_JG, T_JL, T_LABEL, T_LOAD, T_LSH, T_MOD, T_MUL, T_PAUSE, T_RSH,
|
|
|
|
T_COPY, T_DEBUG, T_DIV, T_EXIT, T_GOTO, T_JE, T_JG, T_JL, T_LABEL, T_LOAD, T_LSH, T_MOD, T_MUL,
|
|
|
|
T_SEND, T_SET, T_SUB, T_WRITE,
|
|
|
|
T_PAUSE, T_RSH, T_SEND, T_SET, T_SUB, T_WRITE,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
@ -62,56 +62,46 @@ impl Runtime {
|
|
|
|
/// that can be executed
|
|
|
|
/// that can be executed
|
|
|
|
pub fn parse_bytecode(&mut self, bytecode: Vec<u8>) {
|
|
|
|
pub fn parse_bytecode(&mut self, bytecode: Vec<u8>) {
|
|
|
|
let mut code_iter = bytecode.iter();
|
|
|
|
let mut code_iter = bytecode.iter();
|
|
|
|
|
|
|
|
let mut text = self.text.borrow_mut();
|
|
|
|
|
|
|
|
|
|
|
|
while let Some(instruction) = code_iter.next() {
|
|
|
|
while let Some(instruction) = code_iter.next() {
|
|
|
|
match *instruction {
|
|
|
|
match *instruction {
|
|
|
|
T_EXIT => self
|
|
|
|
T_EXIT => text.push(Box::new(ExitToken::from_bytecode(&[
|
|
|
|
.text
|
|
|
|
instruction,
|
|
|
|
.borrow_mut()
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
.push(Box::new(ExitToken::from_bytecode(&[
|
|
|
|
]))),
|
|
|
|
instruction,
|
|
|
|
T_SET => text.push(Box::new(SetToken::from_bytecode(&[
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
instruction,
|
|
|
|
]))),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
T_SET => self
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
.text
|
|
|
|
]))),
|
|
|
|
.borrow_mut()
|
|
|
|
T_COPY => text.push(Box::new(CopyToken::from_bytecode(&[
|
|
|
|
.push(Box::new(SetToken::from_bytecode(&[
|
|
|
|
instruction,
|
|
|
|
instruction,
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
]))),
|
|
|
|
]))),
|
|
|
|
T_LOAD => text.push(Box::new(LoadToken)),
|
|
|
|
T_COPY => self
|
|
|
|
T_CLEAR => text.push(Box::new(ClearToken::from_bytecode(&[
|
|
|
|
.text
|
|
|
|
instruction,
|
|
|
|
.borrow_mut()
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
.push(Box::new(CopyToken::from_bytecode(&[
|
|
|
|
]))),
|
|
|
|
instruction,
|
|
|
|
T_WRITE => text.push(Box::new(WriteToken)),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
T_LABEL => text.push(Box::new(LabelToken)),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
T_GOTO => text.push(Box::new(GotoToken)),
|
|
|
|
]))),
|
|
|
|
T_DEBUG => text.push(Box::new(DebugToken)),
|
|
|
|
T_LOAD => self.text.borrow_mut().push(Box::new(LoadToken)),
|
|
|
|
T_ADD => text.push(Box::new(AddToken)),
|
|
|
|
T_CLEAR => self
|
|
|
|
T_SUB => text.push(Box::new(SubToken)),
|
|
|
|
.text
|
|
|
|
T_MUL => text.push(Box::new(MulToken)),
|
|
|
|
.borrow_mut()
|
|
|
|
T_DIV => text.push(Box::new(DivToken)),
|
|
|
|
.push(Box::new(ClearToken::from_bytecode(&[
|
|
|
|
T_MOD => text.push(Box::new(ModToken)),
|
|
|
|
instruction,
|
|
|
|
T_LSH => text.push(Box::new(LshToken)),
|
|
|
|
code_iter.next().unwrap(),
|
|
|
|
T_RSH => text.push(Box::new(RshToken)),
|
|
|
|
]))),
|
|
|
|
T_JG => text.push(Box::new(JgToken)),
|
|
|
|
T_WRITE => self.text.borrow_mut().push(Box::new(WriteToken)),
|
|
|
|
T_JL => text.push(Box::new(JlToken)),
|
|
|
|
T_LABEL => self.text.borrow_mut().push(Box::new(LabelToken)),
|
|
|
|
T_JE => text.push(Box::new(JeToken)),
|
|
|
|
T_GOTO => self.text.borrow_mut().push(Box::new(GotoToken)),
|
|
|
|
T_PAUSE => text.push(Box::new(PauseToken)),
|
|
|
|
T_ADD => self.text.borrow_mut().push(Box::new(AddToken)),
|
|
|
|
T_CMD => text.push(Box::new(CmdToken)),
|
|
|
|
T_SUB => self.text.borrow_mut().push(Box::new(SubToken)),
|
|
|
|
T_SEND => text.push(Box::new(SendToken)),
|
|
|
|
T_MUL => self.text.borrow_mut().push(Box::new(MulToken)),
|
|
|
|
|
|
|
|
T_DIV => self.text.borrow_mut().push(Box::new(DivToken)),
|
|
|
|
|
|
|
|
T_MOD => self.text.borrow_mut().push(Box::new(ModToken)),
|
|
|
|
|
|
|
|
T_LSH => self.text.borrow_mut().push(Box::new(LshToken)),
|
|
|
|
|
|
|
|
T_RSH => self.text.borrow_mut().push(Box::new(RshToken)),
|
|
|
|
|
|
|
|
T_JG => self.text.borrow_mut().push(Box::new(JgToken)),
|
|
|
|
|
|
|
|
T_JL => self.text.borrow_mut().push(Box::new(JlToken)),
|
|
|
|
|
|
|
|
T_JE => self.text.borrow_mut().push(Box::new(JeToken)),
|
|
|
|
|
|
|
|
T_PAUSE => self.text.borrow_mut().push(Box::new(PauseToken)),
|
|
|
|
|
|
|
|
T_CMD => self.text.borrow_mut().push(Box::new(CmdToken)),
|
|
|
|
|
|
|
|
T_SEND => self.text.borrow_mut().push(Box::new(SendToken)),
|
|
|
|
|
|
|
|
_ => panic!("unknown instruction {}", instruction),
|
|
|
|
_ => panic!("unknown instruction {}", instruction),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|