2022-03-14 01:04:08 -05:00
|
|
|
use vcore::opcodes::Opcode;
|
|
|
|
|
|
2022-03-16 23:13:02 -05:00
|
|
|
use std::fmt::{Display, Formatter};
|
2022-03-14 01:04:08 -05:00
|
|
|
|
|
|
|
|
/// One of the five arithmetical operators
|
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
2022-03-14 19:13:14 -05:00
|
|
|
pub enum Operator {
|
|
|
|
|
Add,
|
|
|
|
|
Sub,
|
|
|
|
|
Mul,
|
|
|
|
|
Div,
|
|
|
|
|
Mod,
|
|
|
|
|
}
|
2022-03-14 01:04:08 -05:00
|
|
|
|
|
|
|
|
/// An AST node for an instruction argument. This can be a string, number, label reference,
|
|
|
|
|
/// line offset, or an expr containing a sequence of other Nodes joined by same-precedence
|
|
|
|
|
/// `Operator`s.
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
|
pub enum Node<'a> {
|
|
|
|
|
Number(i32),
|
|
|
|
|
Label(&'a str),
|
|
|
|
|
RelativeLabel(&'a str),
|
|
|
|
|
AbsoluteOffset(i32),
|
|
|
|
|
RelativeOffset(i32),
|
|
|
|
|
Expr(Box<Node<'a>>, Vec<(Operator, Node<'a>)>),
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-27 23:10:08 -05:00
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
2022-03-15 22:50:19 -05:00
|
|
|
pub struct Label<'a>(pub &'a str);
|
2022-03-14 01:04:08 -05:00
|
|
|
|
2022-03-16 23:13:02 -05:00
|
|
|
impl<'a> Display for Label<'a> {
|
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 22:50:19 -05:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
|
pub enum VASMLine<'a> {
|
|
|
|
|
Instruction(Option<Label<'a>>, Opcode, Option<Node<'a>>),
|
|
|
|
|
Db(Option<Label<'a>>, Node<'a>),
|
2022-03-19 13:07:27 -05:00
|
|
|
StringDb(Option<Label<'a>>, String),
|
2022-03-15 22:50:19 -05:00
|
|
|
Org(Option<Label<'a>>, Node<'a>),
|
|
|
|
|
Equ(Label<'a>, Node<'a>),
|
|
|
|
|
LabelDef(Label<'a>),
|
2022-03-14 01:04:08 -05:00
|
|
|
}
|
2022-03-27 23:10:08 -05:00
|
|
|
|
|
|
|
|
impl<'a> VASMLine<'a> {
|
|
|
|
|
pub fn label(&self) -> Option<Label<'a>> {
|
|
|
|
|
match self {
|
|
|
|
|
VASMLine::Instruction(Some(lbl), _, _)
|
|
|
|
|
| VASMLine::Db(Some(lbl), _)
|
|
|
|
|
| VASMLine::StringDb(Some(lbl), _)
|
|
|
|
|
| VASMLine::Org(Some(lbl), _)
|
|
|
|
|
| VASMLine::Equ(lbl, _)
|
|
|
|
|
| VASMLine::LabelDef(lbl) => Some(*lbl),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|