From 4ae018935676c20429389785788f5e0a90943ff8 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 28 Oct 2025 21:17:39 +0100 Subject: [PATCH] Simple text example --- input.fg | 3 +++ src/main.rs | 15 +++++++-------- src/text.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 input.fg diff --git a/input.fg b/input.fg new file mode 100644 index 0000000..a86a632 --- /dev/null +++ b/input.fg @@ -0,0 +1,3 @@ +REG rx, 6 +REG ry, 7 +ADD rx, ry, 0 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 917a131..713933e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,21 +2,20 @@ mod binary; mod emulator; mod text; -use std::sync::Arc; +use std::{fs, sync::Arc}; use osui::prelude::*; -use crate::emulator::{instruction::Instruction, registry::Registry}; +use crate::{ + emulator::{instruction::Instruction, registry::Registry}, + text::from_text, +}; fn main() { let screen = Screen::new(); - let instructions = vec![ - Instruction::REG(0, 6), - Instruction::REG(1, 7), - Instruction::ADD(0, 1, 2), // 6 + 7 - ]; + let s = fs::read_to_string("input.fg").unwrap(); - app(&screen, instructions).draw(&screen); + app(&screen, from_text(&s)).draw(&screen); screen.run().unwrap(); } diff --git a/src/text.rs b/src/text.rs index e69de29..9610ae1 100644 --- a/src/text.rs +++ b/src/text.rs @@ -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 { + 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() +}