cargo fmt
This commit is contained in:
parent
d98334244e
commit
e21e0bc03e
|
@ -1,4 +1,4 @@
|
|||
use std::{collections::VecDeque};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
/// The type we use to represent values stored in out CPU.
|
||||
pub type Word = i32;
|
||||
|
@ -15,14 +15,13 @@ pub enum RegRef {
|
|||
Indirect(Register),
|
||||
}
|
||||
|
||||
|
||||
/// Our whole instruction set. Consult ISA.md for an explaination.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Instruction {
|
||||
READ(RegRef), // read n
|
||||
WRITE(RegRef), // write n
|
||||
LOAD(RegRef), // load =4
|
||||
LOADI(Word), // load 4 (load immediate)
|
||||
LOADI(Word), // load 4 (load immediate)
|
||||
STORE(RegRef), //
|
||||
ADD(RegRef),
|
||||
ADDI(Word),
|
||||
|
@ -37,8 +36,6 @@ pub enum Instruction {
|
|||
|
||||
use Instruction::*;
|
||||
|
||||
|
||||
|
||||
type InstructionNumber = usize;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -195,15 +192,13 @@ impl CPU {
|
|||
}
|
||||
|
||||
fn deref(&self, addref: &RegRef) -> Result<Register, CpuError> {
|
||||
|
||||
match addref {
|
||||
RegRef::Direct(r) => return Ok(*r),
|
||||
RegRef::Indirect(addr) => {
|
||||
let reg = self.readmemory(addr)?;
|
||||
return Ok(reg as Register)
|
||||
},
|
||||
return Ok(reg as Register);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Executes a single statement.
|
||||
|
@ -213,7 +208,7 @@ impl CPU {
|
|||
|
||||
let pc_val = self.readmemory(pc)? as usize;
|
||||
if pc_val + 1 > self.program.len() {
|
||||
return Err(ProgramIsOver(pc_val))
|
||||
return Err(ProgramIsOver(pc_val));
|
||||
}
|
||||
let op = &self.program[pc_val]; // fetch
|
||||
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
use crate::parser::{parse_line, parse_program, ParseError};
|
||||
use js_sys::{Int32Array, Array};
|
||||
use js_sys::{Array, Int32Array};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
|
||||
use super::cpu::*;
|
||||
use super::cpu::CpuError::*;
|
||||
use super::cpu::*;
|
||||
|
||||
use Instruction::*;
|
||||
use RegRef::*;
|
||||
|
||||
|
||||
impl std::fmt::Display for RegRef {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
|
@ -46,7 +44,6 @@ extern "C" {
|
|||
fn signalerror(msg: &str, l: Option<u32>);
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
|
||||
|
@ -68,7 +65,6 @@ impl WrapCpu {
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.cpu = CPU::default();
|
||||
}
|
||||
|
@ -89,7 +85,6 @@ impl WrapCpu {
|
|||
return Int32Array::from(&k[..]);
|
||||
}
|
||||
|
||||
|
||||
pub fn showprogram(&self) -> Array {
|
||||
let (_, _, _, k) = self.cpu.exposestate();
|
||||
let arr = Array::new_with_length(k.len() as u32);
|
||||
|
@ -104,7 +99,6 @@ impl WrapCpu {
|
|||
self.cpu.add_input(x)
|
||||
}
|
||||
|
||||
|
||||
pub fn set_input(&mut self, x: Int32Array) {
|
||||
self.cpu.set_input(x.to_vec());
|
||||
}
|
||||
|
@ -120,7 +114,6 @@ impl WrapCpu {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn run(&mut self) {
|
||||
match self.cpu.exec() {
|
||||
Ok(()) => {}
|
||||
|
@ -131,21 +124,27 @@ impl WrapCpu {
|
|||
pub fn load_program(&mut self, prg: &str) {
|
||||
match parse_program(prg) {
|
||||
Ok(p) => self.cpu.load_new(&p),
|
||||
Err(ParseError::ErrorAtLine(l)) => {signalparseerr(l as u32)}
|
||||
Err(ParseError::ErrorAtLine(l)) => signalparseerr(l as u32),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn cpuErrDispatch(err: CpuError) {
|
||||
|
||||
match err {
|
||||
EndOfInput(n) => {signalerror("End of input! Add more input?", Some(n as u32))}
|
||||
BadRegister(n, r) => {signalerror(&format!("The register {} doesn't exist!", r), Some(n as u32))}
|
||||
OverFlow(n) => {signalerror("Overflow! Did you go into the negative numbers?", Some(n as u32))}
|
||||
AccAccess(n) => {signalerror("Tried to access register 0. That's illegal!", Some(n as u32))}
|
||||
BadJump(n) => {signalerror("Bad jump instruction!", Some(n as u32))}
|
||||
ProgramIsOver(n) => {signalerror("The program is over! Forgot to HALT?", Some(n as u32))}
|
||||
EndOfInput(n) => signalerror("End of input! Add more input?", Some(n as u32)),
|
||||
BadRegister(n, r) => signalerror(
|
||||
&format!("The register {} doesn't exist!", r),
|
||||
Some(n as u32),
|
||||
),
|
||||
OverFlow(n) => signalerror(
|
||||
"Overflow! Did you go into the negative numbers?",
|
||||
Some(n as u32),
|
||||
),
|
||||
AccAccess(n) => signalerror(
|
||||
"Tried to access register 0. That's illegal!",
|
||||
Some(n as u32),
|
||||
),
|
||||
BadJump(n) => signalerror("Bad jump instruction!", Some(n as u32)),
|
||||
ProgramIsOver(n) => signalerror("The program is over! Forgot to HALT?", Some(n as u32)),
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -84,33 +84,78 @@ pub fn parse_line(line: &str) -> Option<Instruction> {
|
|||
// COULD BE BETTER!!!!!!!!!
|
||||
// We have to parse differently in case of references..
|
||||
let patternspairs = [
|
||||
(FnWrap::new(|x| READ(Direct(x.parse().unwrap()))), r"^READ\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| READ(Indirect(x.parse().unwrap()))), r"^READ\s+\*(\d+)\s*(?:#.*)?$"),
|
||||
|
||||
(FnWrap::new(|x| WRITE(Direct(x.parse().unwrap()))), r"^WRITE\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| WRITE(Indirect(x.parse().unwrap()))), r"^WRITE\s+\*(\d+)\s*(?:#.*)?$"),
|
||||
|
||||
(FnWrap::new(|x| LOAD(Direct(x.parse().unwrap()))), r"^LOAD\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| LOAD(Indirect(x.parse().unwrap()))), r"^LOAD\s+\*(\d+)\s*(?:#.*)?$"),
|
||||
(
|
||||
FnWrap::new(|x| READ(Direct(x.parse().unwrap()))),
|
||||
r"^READ\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| READ(Indirect(x.parse().unwrap()))),
|
||||
r"^READ\s+\*(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| WRITE(Direct(x.parse().unwrap()))),
|
||||
r"^WRITE\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| WRITE(Indirect(x.parse().unwrap()))),
|
||||
r"^WRITE\s+\*(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| LOAD(Direct(x.parse().unwrap()))),
|
||||
r"^LOAD\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| LOAD(Indirect(x.parse().unwrap()))),
|
||||
r"^LOAD\s+\*(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| LOADI(x.parse().unwrap())),
|
||||
r"^LOAD\s+=\s*([-+]?\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(FnWrap::new(|x| STORE(Direct(x.parse().unwrap()))), r"^STORE\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| STORE(Indirect(x.parse().unwrap()))), r"^STORE\s+\*(\d+)\s*(?:#.*)?$"),
|
||||
|
||||
(FnWrap::new(|x| ADD(Direct(x.parse().unwrap()))), r"^ADD\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| ADD(Indirect(x.parse().unwrap()))), r"^ADD\s+\*(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| ADDI(x.parse().unwrap())), r"^ADD\s=\s*([-+]?\d+)\s*(?:#.*)?$"),
|
||||
|
||||
(FnWrap::new(|x| SUB(Direct(x.parse().unwrap()))), r"^SUB\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| SUB(Indirect(x.parse().unwrap()))), r"^SUB\s+\*(\d+)\s*(?:#.*)?$"),
|
||||
|
||||
(FnWrap::new(|x| SUBI(x.parse().unwrap())), r"^SUB\s=\s*([-+]?\d+)\s*(?:#.*)?$"),
|
||||
|
||||
(FnWrap::new(|x| JUMP(x.parse().unwrap())), r"^JUMP\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| JZERO(x.parse().unwrap())), r"^JZERO\s+(\d+)\s*(?:#.*)?$"),
|
||||
(FnWrap::new(|x| JGTZ(x.parse().unwrap())), r"^JGTZ\s+(\d+)\s*(?:#.*)?$"),
|
||||
(
|
||||
FnWrap::new(|x| STORE(Direct(x.parse().unwrap()))),
|
||||
r"^STORE\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| STORE(Indirect(x.parse().unwrap()))),
|
||||
r"^STORE\s+\*(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| ADD(Direct(x.parse().unwrap()))),
|
||||
r"^ADD\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| ADD(Indirect(x.parse().unwrap()))),
|
||||
r"^ADD\s+\*(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| ADDI(x.parse().unwrap())),
|
||||
r"^ADD\s=\s*([-+]?\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| SUB(Direct(x.parse().unwrap()))),
|
||||
r"^SUB\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| SUB(Indirect(x.parse().unwrap()))),
|
||||
r"^SUB\s+\*(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| SUBI(x.parse().unwrap())),
|
||||
r"^SUB\s=\s*([-+]?\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| JUMP(x.parse().unwrap())),
|
||||
r"^JUMP\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| JZERO(x.parse().unwrap())),
|
||||
r"^JZERO\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(
|
||||
FnWrap::new(|x| JGTZ(x.parse().unwrap())),
|
||||
r"^JGTZ\s+(\d+)\s*(?:#.*)?$",
|
||||
),
|
||||
(FnWrap::new(|_| HALT), r"^HALT\s*(?:#.*)?$"),
|
||||
];
|
||||
|
||||
|
@ -161,7 +206,6 @@ fn single_line_test() {
|
|||
assert_eq!(parse_line("SUB *3"), Some(SUB(Indirect(3))));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn program_test() {
|
||||
let code = "# this is a comment. \nREAD 1 # this is another comment.\nLOAD 1\nJZERO 8\nSUB =-1\nREAD 1\nWRITE *1\nJUMP 3\nHALT";
|
||||
|
|
Loading…
Reference in New Issue