You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

554 lines
11 KiB
Rust

use crate::registers::{Register, RCS};
use crate::runtime::Runtime;
use std::io;
use std::thread::sleep;
use std::time::Duration;
pub const T_NOP: u8 = 0x00;
pub const T_EXIT: u8 = 0x01;
pub const T_SET: u8 = 0x02;
pub const T_COPY: u8 = 0x03;
pub const T_LOAD: u8 = 0x04;
pub const T_CLEAR: u8 = 0x05;
pub const T_WRITE: u8 = 0x06;
pub const T_LABEL: u8 = 0x07;
pub const T_GOTO: u8 = 0x08;
pub const T_ADD: u8 = 0x10;
pub const T_SUB: u8 = 0x11;
pub const T_MUL: u8 = 0x12;
pub const T_DIV: u8 = 0x13;
pub const T_MOD: u8 = 0x14;
pub const T_LSH: u8 = 0x15;
pub const T_RSH: u8 = 0x16;
pub const T_JG: u8 = 0x20;
pub const T_JL: u8 = 0x21;
pub const T_JE: u8 = 0x22;
pub const T_PAUSE: u8 = 0xF0;
pub const T_CMD: u8 = 0xF1;
pub const T_SEND: u8 = 0xF2;
5 years ago
pub trait Token {
fn to_bytecode(&self) -> Vec<u8>;
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()>;
5 years ago
}
pub trait FromBytecode {
fn from_bytecode(code: &[&u8]) -> Self;
}
#[derive(Debug, Clone)]
5 years ago
pub struct NopToken;
impl Token for NopToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_NOP]
}
fn invoke(&self, _: &mut Runtime) -> io::Result<()> {
Ok(())
}
5 years ago
}
impl FromBytecode for NopToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct ExitToken {
pub register: u8,
}
impl Token for ExitToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_EXIT, self.register]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
let mut exit_code = 0;
if let Some(rg) = runtime.get_1byte_register(self.register) {
exit_code = rg.get();
} else if let Some(rg) = runtime.get_4byte_register(self.register) {
exit_code = rg.get() as u8;
} else if self.register == RCS {
exit_code = runtime.rcs.get() as u8;
}
runtime.exit(exit_code);
Ok(())
}
5 years ago
}
impl FromBytecode for ExitToken {
fn from_bytecode(code: &[&u8]) -> Self {
Self { register: *code[1] }
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct SetToken {
pub value: u8,
pub register: u8,
}
impl Token for SetToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_SET, self.value, self.register]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
if let Some(mut rg) = runtime.get_1byte_register(self.register) {
rg.set(self.value);
} else if let Some(mut rg) = runtime.get_4byte_register(self.register) {
rg.set(self.value as u32);
} else if self.register == RCS {
runtime.rcs.set(self.value == 0);
}
Ok(())
}
5 years ago
}
impl FromBytecode for SetToken {
fn from_bytecode(code: &[&u8]) -> Self {
Self {
value: *code[1],
register: *code[2],
}
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct CopyToken {
pub register_1: u8,
pub register_2: u8,
}
impl Token for CopyToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_COPY, self.register_1, self.register_2]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
let mut value = 0u32;
if let Some(rg) = runtime.get_1byte_register(self.register_1) {
value = rg.get() as u32;
} else if let Some(rg) = runtime.get_4byte_register(self.register_1) {
value = rg.get();
} else if self.register_1 == RCS {
value = runtime.rcs.get() as u32;
}
if let Some(mut rg) = runtime.get_1byte_register(self.register_2) {
rg.set(value as u8);
} else if let Some(mut rg) = runtime.get_4byte_register(self.register_2) {
rg.set(value);
} else if self.register_2 == RCS {
runtime.rcs.set(value == 0);
}
Ok(())
}
5 years ago
}
impl FromBytecode for CopyToken {
fn from_bytecode(code: &[&u8]) -> Self {
Self {
register_1: *code[1],
register_2: *code[2],
}
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct LoadToken;
impl Token for LoadToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_LOAD]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
let pointer = runtime.rgp.get();
if let Some(value) = runtime.memory.get(&pointer) {
runtime.rgd.set(*value);
} else {
runtime.rgd.set(0);
}
Ok(())
}
5 years ago
}
impl FromBytecode for LoadToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct ClearToken {
pub register: u8,
}
impl Token for ClearToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_CLEAR, self.register]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
if let Some(mut rg) = runtime.get_1byte_register(self.register) {
rg.set(0u8);
} else if let Some(mut rg) = runtime.get_4byte_register(self.register) {
rg.set(0u32);
} else if self.register == RCS {
runtime.rcs.set(false);
}
Ok(())
}
5 years ago
}
impl FromBytecode for ClearToken {
fn from_bytecode(code: &[&u8]) -> Self {
Self { register: *code[1] }
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct WriteToken;
impl Token for WriteToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_WRITE]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.memory.insert(runtime.rgp.get(), runtime.rgd.get());
Ok(())
}
5 years ago
}
impl FromBytecode for WriteToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct LabelToken;
impl Token for LabelToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_LABEL]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.create_label(runtime.rgl.get());
Ok(())
}
5 years ago
}
impl FromBytecode for LabelToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct GotoToken;
impl Token for GotoToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_GOTO]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.jump(runtime.rgl.get())
}
5 years ago
}
impl FromBytecode for GotoToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct AddToken;
impl Token for AddToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_ADD]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgo.get() + runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for AddToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct SubToken;
impl Token for SubToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_SUB]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgd.get() - runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for SubToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct MulToken;
impl Token for MulToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_MUL]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgd.get() * runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for MulToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct DivToken;
impl Token for DivToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_DIV]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgd.get() / runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for DivToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct ModToken;
impl Token for ModToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_MOD]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgd.get() % runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for ModToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct LshToken;
impl Token for LshToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_LSH]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgd.get() << runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for LshToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct RshToken;
impl Token for RshToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_RSH]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
runtime.rgo.set(runtime.rgd.get() >> runtime.rgi.get());
Ok(())
}
5 years ago
}
impl FromBytecode for RshToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct JgToken;
impl Token for JgToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_JG]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
if runtime.rgd.get() > runtime.rgi.get() {
runtime.jump(runtime.rgl.get())?;
}
Ok(())
}
5 years ago
}
impl FromBytecode for JgToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct JlToken;
impl Token for JlToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_JL]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
if runtime.rgd.get() < runtime.rgi.get() {
runtime.jump(runtime.rgl.get())?;
}
Ok(())
}
5 years ago
}
impl FromBytecode for JlToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct JeToken;
impl Token for JeToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_JE]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
if runtime.rgd.get() == runtime.rgi.get() {
runtime.jump(runtime.rgl.get())?;
}
Ok(())
}
5 years ago
}
impl FromBytecode for JeToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct PauseToken;
impl Token for PauseToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_PAUSE]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
sleep(Duration::from_millis(runtime.rgd.get() as u64));
Ok(())
}
5 years ago
}
impl FromBytecode for PauseToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
#[derive(Debug, Clone)]
5 years ago
pub struct CmdToken;
impl Token for CmdToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_CMD]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
unimplemented!();
Ok(())
}
5 years ago
}
impl FromBytecode for CmdToken {
fn from_bytecode(_: &[&u8]) -> Self {
Self
}
}
pub struct SendToken;
impl Token for SendToken {
fn to_bytecode(&self) -> Vec<u8> {
vec![T_SEND]
}
fn invoke(&self, runtime: &mut Runtime) -> io::Result<()> {
let r = runtime.rcr.get();
let g = runtime.rcg.get();
let b = runtime.rcb.get();
runtime
.strip_controller
.borrow_mut()
.send_rgb_color(r, g, b)
}
}