Simple text example
This commit is contained in:
+7
-8
@@ -2,21 +2,20 @@ mod binary;
|
|||||||
mod emulator;
|
mod emulator;
|
||||||
mod text;
|
mod text;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::{fs, sync::Arc};
|
||||||
|
|
||||||
use osui::prelude::*;
|
use osui::prelude::*;
|
||||||
|
|
||||||
use crate::emulator::{instruction::Instruction, registry::Registry};
|
use crate::{
|
||||||
|
emulator::{instruction::Instruction, registry::Registry},
|
||||||
|
text::from_text,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let screen = Screen::new();
|
let screen = Screen::new();
|
||||||
let instructions = vec![
|
let s = fs::read_to_string("input.fg").unwrap();
|
||||||
Instruction::REG(0, 6),
|
|
||||||
Instruction::REG(1, 7),
|
|
||||||
Instruction::ADD(0, 1, 2), // 6 + 7
|
|
||||||
];
|
|
||||||
|
|
||||||
app(&screen, instructions).draw(&screen);
|
app(&screen, from_text(&s)).draw(&screen);
|
||||||
|
|
||||||
screen.run().unwrap();
|
screen.run().unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
use crate::emulator::instruction::Instruction;
|
||||||
|
|
||||||
|
pub fn reg(r: &str) -> u8 {
|
||||||
|
match r.to_lowercase().as_str() {
|
||||||
|
"rx" => 0,
|
||||||
|
"ry" => 1,
|
||||||
|
"rz" => 2,
|
||||||
|
"ra" => 3,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn num(r: &str) -> usize {
|
||||||
|
r.parse().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_text(s: &str) -> Vec<Instruction> {
|
||||||
|
s.lines()
|
||||||
|
.filter_map(|v| {
|
||||||
|
let (name, args) = v.split_once(' ')?;
|
||||||
|
let args: Vec<&str> = args.split(',').map(str::trim).collect();
|
||||||
|
|
||||||
|
match name.to_lowercase().as_str() {
|
||||||
|
"hlt" => Some(Instruction::HLT),
|
||||||
|
"reg" => Some(Instruction::REG(reg(args[0]), num(args[1]))),
|
||||||
|
"add" => Some(Instruction::ADD(reg(args[0]), reg(args[1]), reg(args[2]))),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user