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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2021 Saadi Save
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#![allow(clippy::module_name_repetitions)]

use std::{
    error::Error,
    fmt::{Debug, Display, Formatter, Result as FmtResult},
    ops::Deref,
};

#[derive(Debug)]
pub enum PasmError {
    Str(String),
    InvalidUtf8Byte(usize),
    InvalidLiteral,
    InvalidOperand,
    NoOpInst,
    NoOperand,
    InvalidMemoryLoc(usize),
    InvalidIndirectAddress(usize),
    InvalidIndexedAddress(usize, usize),
    InvalidMultiOp,
}

impl Display for PasmError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        use PasmError::*;

        match self {
            Str(s) => f.write_str(s),
            InvalidUtf8Byte(b) => f.write_fmt(format_args!("#x{b:X} is not a valid UTF-8 byte.")),
            InvalidLiteral => f.write_str("Operand is not a decimal, hexadecimal, or binary number."),
            InvalidOperand => f.write_str("Operand is not a memory address, register, or literal. If you wanted to use a label, please double-check the label."),
            NoOperand => f.write_str("Operand missing."),
            NoOpInst => f.write_str("Instruction takes no operand."),
            InvalidMemoryLoc(addr) => f.write_fmt(format_args!("Memory address {addr} does not exist.")),
            InvalidIndirectAddress(addr) => f.write_fmt(format_args!("The value at memory address {addr} does not point to a valid memory address")),
            InvalidIndexedAddress(addr, offset) => f.write_fmt(format_args!("The memory address {addr} offset by IX value {offset} is not a valid memory address ({addr} + {offset} = {})", addr + offset)),
            InvalidMultiOp => f.write_str("Operand sequence is invalid"),
        }
    }
}

impl Error for PasmError {}

impl<T: Deref<Target = str>> From<T> for PasmError {
    fn from(s: T) -> Self {
        PasmError::Str(s.to_string())
    }
}

pub type PasmResult<T = ()> = Result<T, PasmError>;

#[derive(Debug)]
#[repr(transparent)]
pub struct Source(Vec<String>);

impl Source {
    pub fn handle_err(&self, write: &mut impl std::io::Write, err: &PasmError, pos: usize) {
        let mk_line =
            |inst: &str, num: usize| format!("\n{num:>w$}    {inst}", w = self.whitespace());

        let mut out = String::new();
        out.push_str("Runtime Error:\n");

        for (i, s) in self.0.iter().enumerate() {
            if pos == i {
                if let Some(prev) = self.0.get(i - 1) {
                    out.push_str(&mk_line(prev, i));
                }
                out.push_str(&format!(
                    "\n{num:>w$}    {s} <-",
                    num = i + 1,
                    w = self.whitespace()
                ));
                if let Some(next) = self.0.get(i + 1) {
                    out.push_str(&format!(
                        "\n{num:>w$}    {next}",
                        num = i + 2,
                        w = self.whitespace()
                    ));
                }
                out.push_str(&format!("\n\nmessage: {err}"));
                break;
            }
        }
        writeln!(write, "{}", out).unwrap();
    }

    fn whitespace(&self) -> usize {
        self.0.len().to_string().len()
    }
}

impl<T: Deref<Target = str>> From<T> for Source {
    fn from(s: T) -> Self {
        Source(
            s.to_string()
                .lines()
                .filter(|&el| !el.starts_with("//"))
                .map(String::from)
                .collect(),
        )
    }
}

impl Display for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        for inst in &self.0 {
            f.write_fmt(format_args!("    {inst}\n"))?;
        }

        f.write_str("")
    }
}