瀏覽代碼

input working

jmelesky 4 年之前
父節點
當前提交
e762d86921
共有 2 個文件被更改,包括 19 次插入7 次删除
  1. 1 0
      Cargo.toml
  2. 18 7
      src/main.rs

+ 1 - 0
Cargo.toml

@@ -7,3 +7,4 @@ edition = "2018"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+text_io = "0.1.8"

+ 18 - 7
src/main.rs

@@ -1,11 +1,13 @@
 use std::collections::HashMap;
 use std::fmt;
 use std::fs;
+use text_io::read;
 
 struct UMState {
-    pc: usize,       // program counter
-    regs: [u32; 8],  // register contents
-    mem: Vec<u32>    // memory contents
+    pc: usize,                     // program counter
+    regs: [u32; 8],                // register contents
+    arrmap: HashMap<u32, usize>,   // active arrays
+    mem: Vec<Vec<u32>>             // memory contents (array of arrays)
 }
 
 
@@ -151,10 +153,10 @@ fn um_step(um: &mut UMState, op: UMOp) {
             }
         },
         UMOp::ArrayIndex(a, b, c) => {
-            
+            um.regs[a as usize] = um.mem[um.regs[b as usize] as usize][um.regs[c as usize] as usize]
         },
         UMOp::ArrayAmend(a, b, c) => {
-
+            um.mem[um.regs[a as usize] as usize][um.regs[b as usize] as usize] = um.regs[c as usize]
         },
         UMOp::Add(a, b, c) => {
             um.regs[a as usize] = um.regs[b as usize] + um.regs[c as usize]
@@ -180,6 +182,11 @@ fn um_step(um: &mut UMState, op: UMOp) {
             print!("{}", um.regs[c as usize] as u8 as char)
         },
         UMOp::Input(c) => {
+            let input: String = read!();
+            um.regs[c as usize] = input.chars().nth(0).unwrap() as u32 as u8 as u32;
+            if um.regs[c as usize] == 4 {
+                um.regs[c as usize] = 255
+            }
         },
         UMOp::Load(b, c) => {
         },
@@ -192,7 +199,7 @@ fn um_step(um: &mut UMState, op: UMOp) {
 fn um_run(masks: HashMap<&str, (u32,u32)>,
           um: &mut UMState) {
     loop {
-        let instr = um.mem[um.pc];
+        let instr = um.mem[0][um.pc];
         let full_op = parse_instr(&masks, instr);
         //println!("{}", full_op);
 
@@ -225,10 +232,14 @@ fn main() {
             codex8[i*4+3] ]);
     }
 
+    let mut arrmap: HashMap<u32,usize> = HashMap::new();
+    arrmap.insert(0,0);
+
     let mut um = UMState {
         pc: 0,
+        arrmap: arrmap,
         regs: [0,0,0,0,0,0,0,0],
-        mem: codex
+        mem: vec![codex]
     };
 
     um_run(masks_map, &mut um);