style: code style
This commit is contained in:
@@ -31,7 +31,7 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_label(&mut self, label: isize) -> Option<isize> {
|
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) {
|
pub fn goto(&mut self, p: isize) {
|
||||||
@@ -48,7 +48,7 @@ impl Context {
|
|||||||
|
|
||||||
// stack
|
// stack
|
||||||
pub fn get_at(&self, i: usize) -> Option<isize> {
|
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) {
|
pub fn push(&mut self, n: isize) {
|
||||||
@@ -65,6 +65,6 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mem(&self, addr: isize) -> Option<isize> {
|
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 context;
|
||||||
mod vm;
|
mod vm;
|
||||||
|
|
||||||
use std::{ env, fs::read_to_string, };
|
use std::{ env, fs::read_to_string };
|
||||||
use err::*;
|
use err::*;
|
||||||
use parser::*;
|
use parser::*;
|
||||||
use compiler::*;
|
use compiler::*;
|
||||||
@@ -20,7 +20,7 @@ fn main() {
|
|||||||
let is_help = env::args().any(|a| a == "-h" || a == "--help");
|
let is_help = env::args().any(|a| a == "-h" || a == "--help");
|
||||||
let is_debug = env::args().any(|a| a == "-d" || a == "--debug");
|
let is_debug = env::args().any(|a| a == "-d" || a == "--debug");
|
||||||
let is_compile = env::args().any(|a| a == "-c" || a == "--compile");
|
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 {
|
if is_debug {
|
||||||
println!("[DEBUG] Run in debug mode");
|
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::Push(i) => context.push(*i),
|
||||||
Instruction::Dup => match context.pop() {
|
Instruction::Dup => match context.pop() {
|
||||||
Some(n) => { context.push(n); context.push(n); }
|
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) {
|
Instruction::CopyN(i) => match context.get_at(*i as usize) {
|
||||||
Some(n) => context.push(n),
|
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 => {
|
Instruction::Swap => {
|
||||||
let a = context.pop();
|
let a = context.pop();
|
||||||
@@ -22,7 +22,7 @@ impl Instruction {
|
|||||||
context.push(a);
|
context.push(a);
|
||||||
context.push(b);
|
context.push(b);
|
||||||
} else {
|
} 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(); },
|
Instruction::Pop => { context.pop(); },
|
||||||
@@ -33,7 +33,7 @@ impl Instruction {
|
|||||||
if let (Some(a), Some(b)) = (a, b) {
|
if let (Some(a), Some(b)) = (a, b) {
|
||||||
context.push(a + b);
|
context.push(a + b);
|
||||||
} else {
|
} 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 => {
|
Instruction::Sub => {
|
||||||
@@ -42,7 +42,7 @@ impl Instruction {
|
|||||||
if let (Some(a), Some(b)) = (a, b) {
|
if let (Some(a), Some(b)) = (a, b) {
|
||||||
context.push(b - a);
|
context.push(b - a);
|
||||||
} else {
|
} 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 => {
|
Instruction::Mul => {
|
||||||
@@ -51,7 +51,7 @@ impl Instruction {
|
|||||||
if let (Some(a), Some(b)) = (a, b) {
|
if let (Some(a), Some(b)) = (a, b) {
|
||||||
context.push(a * b);
|
context.push(a * b);
|
||||||
} else {
|
} 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 => {
|
Instruction::Div => {
|
||||||
@@ -60,7 +60,7 @@ impl Instruction {
|
|||||||
if let (Some(a), Some(b)) = (a, b) {
|
if let (Some(a), Some(b)) = (a, b) {
|
||||||
context.push(b / a);
|
context.push(b / a);
|
||||||
} else {
|
} 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 => {
|
Instruction::Mod => {
|
||||||
@@ -69,7 +69,7 @@ impl Instruction {
|
|||||||
if let (Some(a), Some(b)) = (a, b) {
|
if let (Some(a), Some(b)) = (a, b) {
|
||||||
context.push(b % a);
|
context.push(b % a);
|
||||||
} else {
|
} 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 => {
|
Instruction::Store => {
|
||||||
@@ -78,7 +78,7 @@ impl Instruction {
|
|||||||
if let (Some(val), Some(addr)) = (val, addr) {
|
if let (Some(val), Some(addr)) = (val, addr) {
|
||||||
context.put_mem(addr, val);
|
context.put_mem(addr, val);
|
||||||
} else {
|
} 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 => {
|
Instruction::Retrieve => {
|
||||||
@@ -89,7 +89,7 @@ impl Instruction {
|
|||||||
None => return Err(RuntimeError::ErrorVmState(format!("Memory addr not found: {}!", addr))),
|
None => return Err(RuntimeError::ErrorVmState(format!("Memory addr not found: {}!", addr))),
|
||||||
}
|
}
|
||||||
} else {
|
} 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!
|
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!("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() {
|
Instruction::GotoLabelL0(i) => match context.pop() {
|
||||||
Some(v) => if v < 0 {
|
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!("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::ReturnCallAt => return Err(RuntimeError::NotImplemented("ReturnCallAt".into())),
|
||||||
Instruction::End => return Err(RuntimeError::EndVm),
|
Instruction::End => return Err(RuntimeError::EndVm),
|
||||||
Instruction::StdOutChar => match context.pop() {
|
Instruction::StdOutChar => match context.pop() {
|
||||||
Some(i) => print!("{}", i as u8 as char),
|
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() {
|
Instruction::StdOutNum => match context.pop() {
|
||||||
Some(i) => print!("{}", i),
|
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::StdInChar => return Err(RuntimeError::NotImplemented("StdInChar".into())),
|
||||||
Instruction::StdInNum => return Err(RuntimeError::NotImplemented("StdInNum".into())),
|
Instruction::StdInNum => return Err(RuntimeError::NotImplemented("StdInNum".into())),
|
||||||
|
|||||||
Reference in New Issue
Block a user