1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
pub mod arg_matches;
pub mod line;
pub mod registers;
pub mod tokens;

use crate::arg_matches::get_op_code;
use crate::line::parse_line;
use crate::tokens::{to_args_str, Token};
use crate::ParserError::{EmptyLine, General, Language};
use maikor_language::LangError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ParserError {
    #[error("Line was empty (internal parser error)")]
    EmptyLine,
    #[error("")]
    Language(#[source] LangError),
    #[error("Unable to parse: '{0}'")]
    General(String),
    #[error("Invalid address: '{0}'")]
    InvalidAddress(String),
    #[error("Invalid address: '{0}'")]
    InvalidNumber(String),
    #[error("Only AX-DX can be indirect, was {0}")]
    NotExtRegister(String),
    #[error("Invalid register: '{0}'")]
    InvalidRegister(String),
}

impl From<LangError> for ParserError {
    fn from(err: LangError) -> Self {
        Language(err)
    }
}

pub struct ParserOutput {
    pub bytes: Vec<u8>,
    pub op_count: usize,
}

pub fn parse_lines(lines: &[&str]) -> Result<ParserOutput, ParserError> {
    let mut op_count = 0;
    let mut bytes = vec![];

    for line in lines {
        let trimmed = line.trim();
        if !trimmed.starts_with('#') && !trimmed.is_empty() {
            let (op, args) = parse_line(trimmed)?;
            op_count += 1;
            bytes.push(op);
            bytes.extend_from_slice(&args);
        }
    }

    Ok(ParserOutput { bytes, op_count })
}

#[cfg(test)]
mod test {
    use super::*;
    use maikor_language::ops::{CMP_REG_NUM_BYTE, INC_REG_BYTE, JE_ADDR};
    use maikor_language::registers::id::AL;

    #[test]
    fn basic_test() {
        let lines = vec!["# test program", "INC.B AL", "CMP.B AL 1", "JE $50"];
        let output = parse_lines(&lines).unwrap();
        assert_eq!(output.op_count, 3);
        assert_eq!(
            output.bytes,
            vec![
                INC_REG_BYTE,
                AL as u8,
                CMP_REG_NUM_BYTE,
                AL as u8,
                1,
                JE_ADDR,
                0,
                50
            ]
        );
    }
}