mirror of
https://github.com/Noratrieb/m8db.git
synced 2026-01-16 00:05:03 +01:00
added file load mode
This commit is contained in:
parent
e48bf1ee09
commit
5d31ce90d6
3 changed files with 88 additions and 26 deletions
91
src/db.rs
91
src/db.rs
|
|
@ -1,5 +1,7 @@
|
||||||
|
use crate::stmt;
|
||||||
use crate::stmt::{Code, LineNumber, Span, Stmt};
|
use crate::stmt::{Code, LineNumber, Span, Stmt};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Vm<'a> {
|
struct Vm<'a> {
|
||||||
|
|
@ -9,6 +11,7 @@ struct Vm<'a> {
|
||||||
pc: usize,
|
pc: usize,
|
||||||
registers: Vec<usize>,
|
registers: Vec<usize>,
|
||||||
breakpoints: Vec<usize>,
|
breakpoints: Vec<usize>,
|
||||||
|
file_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
|
@ -71,14 +74,66 @@ enum VmInstruction {
|
||||||
Run(VmRunKind),
|
Run(VmRunKind),
|
||||||
Break(usize),
|
Break(usize),
|
||||||
Set(usize, usize),
|
Set(usize, usize),
|
||||||
|
Stop,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(code: Code) {
|
pub fn start(program_path: Option<String>) {
|
||||||
|
if let Some(path) = program_path {
|
||||||
|
read_and_run(&path);
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match loading_input() {
|
||||||
|
LoadInstruction::Quit => return,
|
||||||
|
LoadInstruction::Load(path) => read_and_run(&path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_and_run<'a>(path: &str) {
|
||||||
|
let path = Path::new(path);
|
||||||
|
|
||||||
|
match std::fs::read_to_string(path) {
|
||||||
|
Ok(content) => match stmt::parse(&content, filename(&path)) {
|
||||||
|
Ok(stmts) => run(stmts),
|
||||||
|
Err(why) => eprintln!("parse error: {}", why),
|
||||||
|
},
|
||||||
|
Err(why) => eprintln!("file error: {}", why),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
enum LoadInstruction {
|
||||||
|
Quit,
|
||||||
|
Load(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn loading_input() -> LoadInstruction {
|
||||||
|
loop {
|
||||||
|
let input = get_input(None);
|
||||||
|
let mut iter = input.split_whitespace();
|
||||||
|
if let Some(str) = iter.next() {
|
||||||
|
match str {
|
||||||
|
"l" | "load" => match iter.next() {
|
||||||
|
Some(path) => return LoadInstruction::Load(path.to_owned()),
|
||||||
|
None => println!("No file path provided to load from"),
|
||||||
|
},
|
||||||
|
"h" | "help" => print_load_help(),
|
||||||
|
"q" | "quit" => return LoadInstruction::Quit,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(code: Code) {
|
||||||
|
println!("Loaded {}.", code.file_name);
|
||||||
let max_register_index = max_register(&code.stmts);
|
let max_register_index = max_register(&code.stmts);
|
||||||
let mut vm = Vm {
|
let mut vm = Vm {
|
||||||
stmts: code.stmts,
|
stmts: code.stmts,
|
||||||
span: code.span,
|
span: code.span,
|
||||||
code_lines: code.code_lines,
|
code_lines: code.code_lines,
|
||||||
|
file_name: code.file_name,
|
||||||
pc: 0,
|
pc: 0,
|
||||||
registers: vec![0; max_register_index + 1],
|
registers: vec![0; max_register_index + 1],
|
||||||
breakpoints: vec![],
|
breakpoints: vec![],
|
||||||
|
|
@ -86,6 +141,7 @@ pub fn run(code: Code) {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match debug_input(&vm) {
|
match debug_input(&vm) {
|
||||||
|
VmInstruction::Stop => break,
|
||||||
VmInstruction::Run(time_kind) => match vm.run(time_kind) {
|
VmInstruction::Run(time_kind) => match vm.run(time_kind) {
|
||||||
VmState::Stop => break,
|
VmState::Stop => break,
|
||||||
VmState::OutOfBounds => {
|
VmState::OutOfBounds => {
|
||||||
|
|
@ -119,17 +175,18 @@ pub fn run(code: Code) {
|
||||||
VmInstruction::Set(r, value) => vm.registers[r] = value,
|
VmInstruction::Set(r, value) => vm.registers[r] = value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
println!("Execution finished.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn debug_input(vm: &Vm) -> VmInstruction {
|
fn debug_input(vm: &Vm) -> VmInstruction {
|
||||||
loop {
|
loop {
|
||||||
let input = get_input();
|
let input = get_input(Some(&vm.file_name));
|
||||||
let mut iter = input.split_ascii_whitespace();
|
let mut iter = input.split_whitespace();
|
||||||
if let Some(str) = iter.next() {
|
if let Some(str) = iter.next() {
|
||||||
match str {
|
match str {
|
||||||
"r" | "register" => print_registers(vm),
|
"r" | "register" => print_registers(vm),
|
||||||
"p" | "program" => print_program(vm),
|
"p" | "program" => print_program(vm),
|
||||||
"h" | "?" | "help" => print_help(),
|
"h" | "?" | "help" => print_debug_help(),
|
||||||
"b" | "break" => match iter.next() {
|
"b" | "break" => match iter.next() {
|
||||||
Some(line_number) => match line_number.parse::<usize>() {
|
Some(line_number) => match line_number.parse::<usize>() {
|
||||||
Ok(line_number) => {
|
Ok(line_number) => {
|
||||||
|
|
@ -162,6 +219,7 @@ fn debug_input(vm: &Vm) -> VmInstruction {
|
||||||
return VmInstruction::Run(VmRunKind::WithoutTime);
|
return VmInstruction::Run(VmRunKind::WithoutTime);
|
||||||
}
|
}
|
||||||
"s" | "step" => return VmInstruction::Step,
|
"s" | "step" => return VmInstruction::Step,
|
||||||
|
"q" | "quit" => return VmInstruction::Stop,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -234,7 +292,18 @@ fn print_breakpoints(vm: &Vm) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_help() {
|
fn print_load_help() {
|
||||||
|
println!(
|
||||||
|
"List of commands and their aliases:
|
||||||
|
|
||||||
|
load (l) <filename> -- Load and run a program
|
||||||
|
quit (q) -- Quits the program
|
||||||
|
help (h, ?) -- Shows this help page
|
||||||
|
"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_debug_help() {
|
||||||
println!(
|
println!(
|
||||||
"List of commands and their aliases:
|
"List of commands and their aliases:
|
||||||
|
|
||||||
|
|
@ -244,15 +313,23 @@ fn print_help() {
|
||||||
continue (c) (time) -- Run the program until the next breakpoint, add 'time' to display execution time
|
continue (c) (time) -- Run the program until the next breakpoint, add 'time' to display execution time
|
||||||
register (r) -- Shows the contents of the registers
|
register (r) -- Shows the contents of the registers
|
||||||
program (p) -- Shows where the program currently is
|
program (p) -- Shows where the program currently is
|
||||||
|
quit (q) -- Stop execution of the current program
|
||||||
help (h, ?) -- Shows this help page
|
help (h, ?) -- Shows this help page
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_input() -> String {
|
fn get_input(prompt: Option<&str>) -> String {
|
||||||
let mut input_buf = String::new();
|
let mut input_buf = String::new();
|
||||||
print!("(m8db) ");
|
match prompt {
|
||||||
|
None => print!("(m8db) "),
|
||||||
|
Some(text) => print!("(m8db - {}) ", text),
|
||||||
|
}
|
||||||
std::io::stdout().flush().unwrap();
|
std::io::stdout().flush().unwrap();
|
||||||
std::io::stdin().read_line(&mut input_buf).unwrap();
|
std::io::stdin().read_line(&mut input_buf).unwrap();
|
||||||
input_buf.trim().to_owned()
|
input_buf.trim().to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn filename(path: &Path) -> String {
|
||||||
|
path.file_stem().unwrap().to_str().unwrap().to_owned()
|
||||||
|
}
|
||||||
|
|
|
||||||
19
src/main.rs
19
src/main.rs
|
|
@ -2,14 +2,6 @@ mod db;
|
||||||
mod stmt;
|
mod stmt;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let filename = match std::env::args().nth(1) {
|
|
||||||
Some(name) => name,
|
|
||||||
None => {
|
|
||||||
eprintln!("error: no file provided.\nUsage: <filename>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"m8db - M8 Debugger
|
"m8db - M8 Debugger
|
||||||
(C) Nilstrieb (https://github.com/Nilstrieb/m8db)
|
(C) Nilstrieb (https://github.com/Nilstrieb/m8db)
|
||||||
|
|
@ -17,14 +9,5 @@ Type 'help' for help
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
|
|
||||||
let program = std::fs::read_to_string(filename).unwrap();
|
db::start(std::env::args().nth(1));
|
||||||
let statements = match stmt::parse(&program) {
|
|
||||||
Ok(stmts) => stmts,
|
|
||||||
Err(str) => {
|
|
||||||
eprintln!("{}", str);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
db::run(statements);
|
|
||||||
println!("Execution finished.");
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ pub struct Code<'a> {
|
||||||
/// Has the same length as `stmts`, points to line numbers where the instructions come from
|
/// Has the same length as `stmts`, points to line numbers where the instructions come from
|
||||||
pub span: Vec<Span>,
|
pub span: Vec<Span>,
|
||||||
pub code_lines: Vec<&'a str>,
|
pub code_lines: Vec<&'a str>,
|
||||||
|
pub file_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum IrStmt<'a> {
|
enum IrStmt<'a> {
|
||||||
|
|
@ -50,7 +51,7 @@ enum IrStmt<'a> {
|
||||||
Stop,
|
Stop,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(text: &str) -> Result<Code, String> {
|
pub fn parse(text: &str, file_name: String) -> Result<Code, String> {
|
||||||
let mut labels = HashMap::new();
|
let mut labels = HashMap::new();
|
||||||
|
|
||||||
let mut statements = Vec::new();
|
let mut statements = Vec::new();
|
||||||
|
|
@ -159,6 +160,7 @@ pub fn parse(text: &str) -> Result<Code, String> {
|
||||||
stmts,
|
stmts,
|
||||||
span,
|
span,
|
||||||
code_lines,
|
code_lines,
|
||||||
|
file_name,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue