1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use super::{Context, Op, PasmError, PasmResult};
use std::io::Read;
pub fn end(ctx: &mut Context, _: Op) -> PasmResult {
ctx.increment()
}
pub fn out(ctx: &mut Context, _: Op) -> PasmResult {
let x = ctx.acc;
if x > 127 {
return Err(PasmError::from(format!(
"The value in the ACC, `{}`, is not valid ASCII.",
&x
)));
}
let out = x as u8 as char;
println!("{}", &out);
ctx.increment()
}
pub fn inp(ctx: &mut Context, _: Op) -> PasmResult {
let mut x = [0; 1];
std::io::stdin()
.read_exact(&mut x)
.expect("Unable to read stdin");
ctx.acc = x[0] as usize;
ctx.increment()
}
pub fn dbg(ctx: &mut Context, op: Op) -> PasmResult {
let x = op.ok_or_else(|| PasmError::from("No Operand"))?;
let out = match x.as_str() {
"ix" | "IX" => ctx.ix,
"acc" | "ACC" => ctx.acc,
_ => {
if let Ok(s) = x.parse() {
ctx.mem.get(&s)?
} else {
return Err(PasmError::from(format!(
"{} is not a register or a memory address",
&x
)));
}
}
};
println!("{}", &out);
ctx.increment()
}
pub fn rin(ctx: &mut Context, _: Op) -> PasmResult {
let mut x = String::new();
std::io::stdin()
.read_line(&mut x)
.expect("Unable to read stdin");
x.ends_with('\n').then(|| x.pop());
x.ends_with('\r').then(|| x.pop());
ctx.acc = x
.parse()
.unwrap_or_else(|_| panic!("'{}' is not an integer", &x));
ctx.increment()
}