Website Summary Files Commits Branches @commit @diff @stats
path: root / vm / src
diff options
context:
space:
mode:
Commit1a6b9be6c585f7cfc3351b7554179123f7543174 (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-02-14 16:41:53 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-02-14 16:41:53 +0200
Files096796dcc548896886f54c6b6f0cbe52871f4569 /vm/src
tonkobali-1a6b9be6c585f7cfc3351b7554179123f7543174.tar
tonkobali-1a6b9be6c585f7cfc3351b7554179123f7543174.tar.gz
tonkobali-1a6b9be6c585f7cfc3351b7554179123f7543174.zip
Parent2653f4e53f9e878b8c4d09088fd79b4e22a48865 (diff)
feat(vmm): Range addresses and better address io
1 files changed, 41 insertions, 6 deletions
-rw-r--r--vm/src/vmm/command.rs47
diff --git a/vm/src/vmm/command.rs b/vm/src/vmm/command.rs
index 0811da3..63bf562 100644
--- a/vm/src/vmm/command.rs
+++ b/vm/src/vmm/command.rs
@@ -1,5 +1,7 @@
use crate::vm::{ VirtualMachineInterface, VirtualMachine };
+static MAX_VALUE_WIDTH: usize = 8;
+
fn expects_n_args(args: &Vec<&str>, n: usize) -> Result<(), String> {
if args.len() - 1 != n {
Err(format!("Command '{}' expected {n} arguments, {} were given!", args[0], args.len() - 1))
@@ -9,6 +11,43 @@ fn expects_n_args(args: &Vec<&str>, n: usize) -> Result<(), String> {
}
}
+fn parse_location(arg: &str) -> Result<Vec<usize>, String> {
+ let nums = arg.to_string();
+ let nums = nums
+ .split('.')
+ .collect::<Vec<&str>>();
+
+ let start = nums[0].parse::<usize>()
+ .map_err(|_| format!("Expected location!"))?;
+
+ // Single location
+ if nums.len() == 1 {
+ Ok(vec![start])
+ }
+ // Range location
+ else {
+ let end = nums[1].parse::<usize>()
+ .map_err(|_| format!("Expected location!"))?;
+
+ Ok(Vec::from_iter(start..=end))
+ }
+}
+
+fn print_locations(locs: Vec<usize>, vm: &mut VirtualMachine) {
+ let padding = format!("{}", locs.last().unwrap()).len();
+
+ for (i, loc) in locs.into_iter().enumerate() {
+ if i % MAX_VALUE_WIDTH == 0 {
+ if i > 0 {
+ println!();
+ }
+ print!("{:indent$}:", loc, indent=padding);
+ }
+ print!(" {:ident$}", vm.get_word(loc), ident=6);
+ }
+ println!();
+}
+
pub(super) fn parse_and_exec(input: String, vm: &mut VirtualMachine) -> Result<(), String> {
let args = input
.split(&[' ', '\t', '\n', '\r'])
@@ -36,12 +75,8 @@ pub(super) fn parse_and_exec(input: String, vm: &mut VirtualMachine) -> Result<(
"q" | "quit" | "e" | "exit" => return Err(String::new()),
_ => {
for arg in args {
- if let Some(num) = arg.parse::<usize>().ok() {
- println!("{}", vm.get_word(num));
- }
- else {
- return Err(format!("Unknown command '{}'!", arg));
- }
+ let locs = parse_location(arg)?;
+ print_locations(locs, vm);
}
},
}