forked from n3tael/labast
34 lines
1.1 KiB
Rust
34 lines
1.1 KiB
Rust
|
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(),
|
||
|
_ => data = command[1].parse().expect(&format!("{}", ParseError::DataNotAUInt(command[0].to_string(), i + 1))),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let inst = Instruction { name, arg, data };
|
||
|
stack.program.push(inst);
|
||
|
}
|
||
|
}
|