Website Summary Files Commits Branches @commit @diff @stats
path: root / vm / src
diff options
context:
space:
mode:
Commit4c9b4a8e7acff16e7f8afeabadb99904feda36fa (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-02-14 18:29:34 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-02-14 18:29:34 +0200
Files58c2d263102874a9a0bfde7d1a0649784213b98e /vm/src
tonkobali-4c9b4a8e7acff16e7f8afeabadb99904feda36fa.tar
tonkobali-4c9b4a8e7acff16e7f8afeabadb99904feda36fa.tar.gz
tonkobali-4c9b4a8e7acff16e7f8afeabadb99904feda36fa.zip
Parentce0636266d416103c8827bd6cfa570ae89e19bfc (diff)
feat(vmm): Replace s command with memory deposition
2 files changed, 29 insertions, 16 deletions
-rw-r--r--vm/src/vm/mod.rs2
-rw-r--r--vm/src/vmm/command.rs43
diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs
index 000e350..b5675ea 100644
--- a/vm/src/vm/mod.rs
+++ b/vm/src/vm/mod.rs
@@ -6,7 +6,7 @@ use std::{
};
use primitive::PrimitiveVirtualMachine;
-use memory::Word;
+pub(crate) use memory::Word;
pub(crate) type VirtualMachine = Arc<Mutex<PrimitiveVirtualMachine>>;
diff --git a/vm/src/vmm/command.rs b/vm/src/vmm/command.rs
index 63bf562..628a30f 100644
--- a/vm/src/vmm/command.rs
+++ b/vm/src/vmm/command.rs
@@ -1,4 +1,4 @@
-use crate::vm::{ VirtualMachineInterface, VirtualMachine };
+use crate::vm::{ VirtualMachineInterface, VirtualMachine, Word };
static MAX_VALUE_WIDTH: usize = 8;
@@ -11,14 +11,14 @@ 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('.')
+fn parse_location(arg: &str, assignment: bool) -> Result<Vec<usize>, String> {
+ let nums_str = arg.to_string();
+ let nums = nums_str
+ .split(&['.', ':'])
.collect::<Vec<&str>>();
let start = nums[0].parse::<usize>()
- .map_err(|_| format!("Expected location!"))?;
+ .map_err(|_| format!("Unknown command '{}'!", nums[0]))?;
// Single location
if nums.len() == 1 {
@@ -29,11 +29,22 @@ fn parse_location(arg: &str) -> Result<Vec<usize>, String> {
let end = nums[1].parse::<usize>()
.map_err(|_| format!("Expected location!"))?;
- Ok(Vec::from_iter(start..=end))
+ if assignment {
+ Ok(vec![start, end])
+ }
+ // Range addresses
+ else {
+ Ok(Vec::from_iter(start..=end))
+ }
}
}
-fn print_locations(locs: Vec<usize>, vm: &mut VirtualMachine) {
+fn assign_location(locs: Vec<usize>, vm: &mut VirtualMachine) {
+ print_locations(vec![locs[0]], vm);
+ vm.set_word(locs[0], locs[1] as Word);
+}
+
+fn print_locations(locs: Vec<usize>, vm: &VirtualMachine) {
let padding = format!("{}", locs.last().unwrap()).len();
for (i, loc) in locs.into_iter().enumerate() {
@@ -67,16 +78,18 @@ pub(super) fn parse_and_exec(input: String, vm: &mut VirtualMachine) -> Result<(
}
vm.set_start(args[1].parse().unwrap());
},
- "s" | "store" => {
- expects_n_args(&args, 2)?;
-
- vm.set_word(args[1].parse().unwrap(), args[2].parse().unwrap());
- },
"q" | "quit" | "e" | "exit" => return Err(String::new()),
_ => {
for arg in args {
- let locs = parse_location(arg)?;
- print_locations(locs, vm);
+ let assignment = arg.contains(':');
+ let locs = parse_location(arg, assignment)?;
+
+ if assignment {
+ assign_location(locs, vm);
+ }
+ else {
+ print_locations(locs, vm);
+ }
}
},
}