labast/src/parse.rs

40 lines
1.3 KiB
Rust
Raw Normal View History

2024-02-05 07:05:48 +00:00
use crate::{errors::ParseError, stack::{Instruction, Stack}};
pub fn parse(stack: &mut Stack, file_content: &str) {
for (i, line) in file_content.lines().enumerate() {
if line.trim().starts_with(';') || line.is_empty() {
continue;
}
let inst_line: String;
if let Some(comment_index) = line.trim().find(';') {
inst_line = line.trim().chars().take(comment_index).collect();
} else {
inst_line = line.trim().chars().collect();
}
let command: Vec<String> = inst_line.split_whitespace().map(String::from).collect();
let name: String;
let mut arg: String = String::new();
let mut data: u16 = 0;
name = command[0].clone();
if command.len() >= 2 {
match name.chars().nth(0) {
Some('#') => arg = command[1].to_string(),
2024-02-05 12:13:44 +00:00
_ => {
if command[1] == "-1" { // required for _UNIX_RANDOM instruction
data = 65535;
} else {
data = command[1].parse().expect(&format!("{}", ParseError::DataNotAUInt(command[0].to_string(), i + 1)))
}
}
2024-02-05 07:05:48 +00:00
}
}
let inst = Instruction { name, arg, data };
stack.program.push(inst);
}
}