Simple text example

This commit is contained in:
2025-10-28 21:17:39 +01:00
parent b1b0a78aad
commit 4ae0189356
3 changed files with 41 additions and 8 deletions
+3
View File
@@ -0,0 +1,3 @@
REG rx, 6
REG ry, 7
ADD rx, ry, 0
+7 -8
View File
@@ -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
View File
@@ -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()
}