style: code style
This commit is contained in:
@@ -31,7 +31,7 @@ impl Context {
|
||||
}
|
||||
|
||||
pub fn find_label(&mut self, label: isize) -> Option<isize> {
|
||||
self.label_map.get(&label).map(|i| *i)
|
||||
self.label_map.get(&label).copied()
|
||||
}
|
||||
|
||||
pub fn goto(&mut self, p: isize) {
|
||||
@@ -48,7 +48,7 @@ impl Context {
|
||||
|
||||
// stack
|
||||
pub fn get_at(&self, i: usize) -> Option<isize> {
|
||||
self.stack.get(i).map(|i| *i)
|
||||
self.stack.get(i).copied()
|
||||
}
|
||||
|
||||
pub fn push(&mut self, n: isize) {
|
||||
@@ -65,6 +65,6 @@ impl Context {
|
||||
}
|
||||
|
||||
pub fn get_mem(&self, addr: isize) -> Option<isize> {
|
||||
self.mem_map.get(&addr).map(|i| *i)
|
||||
self.mem_map.get(&addr).copied()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ mod compiler;
|
||||
mod context;
|
||||
mod vm;
|
||||
|
||||
use std::{ env, fs::read_to_string, };
|
||||
use std::{ env, fs::read_to_string };
|
||||
use err::*;
|
||||
use parser::*;
|
||||
use compiler::*;
|
||||
@@ -20,7 +20,7 @@ fn main() {
|
||||
let is_help = env::args().any(|a| a == "-h" || a == "--help");
|
||||
let is_debug = env::args().any(|a| a == "-d" || a == "--debug");
|
||||
let is_compile = env::args().any(|a| a == "-c" || a == "--compile");
|
||||
let arg_file = env::args().filter(|a| !a.starts_with("-")).nth(1);
|
||||
let arg_file = env::args().filter(|a| !a.starts_with('-')).nth(1);
|
||||
|
||||
if is_debug {
|
||||
println!("[DEBUG] Run in debug mode");
|
||||
|
||||
28
src/vm.rs
28
src/vm.rs
@@ -9,11 +9,11 @@ impl Instruction {
|
||||
Instruction::Push(i) => context.push(*i),
|
||||
Instruction::Dup => match context.pop() {
|
||||
Some(n) => { context.push(n); context.push(n); }
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Stack is empty when call dup!"))),
|
||||
None => return Err(RuntimeError::ErrorVmState("Stack is empty when call dup!".to_string())),
|
||||
},
|
||||
Instruction::CopyN(i) => match context.get_at(*i as usize) {
|
||||
Some(n) => context.push(n),
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Stack is empty when call dup!"))),
|
||||
None => return Err(RuntimeError::ErrorVmState("Stack is empty when call dup!".to_string())),
|
||||
},
|
||||
Instruction::Swap => {
|
||||
let a = context.pop();
|
||||
@@ -22,7 +22,7 @@ impl Instruction {
|
||||
context.push(a);
|
||||
context.push(b);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call swap!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call swap!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Pop => { context.pop(); },
|
||||
@@ -33,7 +33,7 @@ impl Instruction {
|
||||
if let (Some(a), Some(b)) = (a, b) {
|
||||
context.push(a + b);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call add!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call add!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Sub => {
|
||||
@@ -42,7 +42,7 @@ impl Instruction {
|
||||
if let (Some(a), Some(b)) = (a, b) {
|
||||
context.push(b - a);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call sub!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call sub!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Mul => {
|
||||
@@ -51,7 +51,7 @@ impl Instruction {
|
||||
if let (Some(a), Some(b)) = (a, b) {
|
||||
context.push(a * b);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call mul!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call mul!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Div => {
|
||||
@@ -60,7 +60,7 @@ impl Instruction {
|
||||
if let (Some(a), Some(b)) = (a, b) {
|
||||
context.push(b / a);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call div!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call div!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Mod => {
|
||||
@@ -69,7 +69,7 @@ impl Instruction {
|
||||
if let (Some(a), Some(b)) = (a, b) {
|
||||
context.push(b % a);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call mod!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call mod!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Store => {
|
||||
@@ -78,7 +78,7 @@ impl Instruction {
|
||||
if let (Some(val), Some(addr)) = (val, addr) {
|
||||
context.put_mem(addr, val);
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call store!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call store!".to_string()));
|
||||
}
|
||||
},
|
||||
Instruction::Retrieve => {
|
||||
@@ -89,7 +89,7 @@ impl Instruction {
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Memory addr not found: {}!", addr))),
|
||||
}
|
||||
} else {
|
||||
return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call retrieve!")));
|
||||
return Err(RuntimeError::ErrorVmState("Stack is not enough when call retrieve!".to_string()));
|
||||
}
|
||||
}
|
||||
Instruction::DefineLabel(_) => (), // JUST SKIP!
|
||||
@@ -105,7 +105,7 @@ impl Instruction {
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Label not found: {}", i))),
|
||||
}
|
||||
},
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call gotolabele0!"))),
|
||||
None => return Err(RuntimeError::ErrorVmState("Stack is not enough when call gotolabele0!".to_string())),
|
||||
},
|
||||
Instruction::GotoLabelL0(i) => match context.pop() {
|
||||
Some(v) => if v < 0 {
|
||||
@@ -114,17 +114,17 @@ impl Instruction {
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Label not found: {}", i))),
|
||||
}
|
||||
},
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call gotolabell0!"))),
|
||||
None => return Err(RuntimeError::ErrorVmState("Stack is not enough when call gotolabell0!".to_string())),
|
||||
},
|
||||
Instruction::ReturnCallAt => return Err(RuntimeError::NotImplemented("ReturnCallAt".into())),
|
||||
Instruction::End => return Err(RuntimeError::EndVm),
|
||||
Instruction::StdOutChar => match context.pop() {
|
||||
Some(i) => print!("{}", i as u8 as char),
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call stdoutchar!"))),
|
||||
None => return Err(RuntimeError::ErrorVmState("Stack is not enough when call stdoutchar!".to_string())),
|
||||
},
|
||||
Instruction::StdOutNum => match context.pop() {
|
||||
Some(i) => print!("{}", i),
|
||||
None => return Err(RuntimeError::ErrorVmState(format!("Stack is not enough when call stdoutnum!"))),
|
||||
None => return Err(RuntimeError::ErrorVmState("Stack is not enough when call stdoutnum!".to_string())),
|
||||
},
|
||||
Instruction::StdInChar => return Err(RuntimeError::NotImplemented("StdInChar".into())),
|
||||
Instruction::StdInNum => return Err(RuntimeError::NotImplemented("StdInNum".into())),
|
||||
|
||||
Reference in New Issue
Block a user