WIP, implementing macros

This commit is contained in:
2022-04-05 18:05:24 -05:00
parent 2a9a92a0c8
commit dec2d22b80
6 changed files with 436 additions and 155 deletions
+50 -19
View File
@@ -1,5 +1,6 @@
use vcore::opcodes::Opcode;
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
/// One of the five arithmetical operators
@@ -16,50 +17,80 @@ pub enum Operator {
/// 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> {
pub enum Node {
Number(i32),
Label(&'a str),
RelativeLabel(&'a str),
Label(String),
RelativeLabel(String),
AbsoluteOffset(i32),
RelativeOffset(i32),
Expr(Box<Node<'a>>, Vec<(Operator, Node<'a>)>),
Expr(Box<Node>, Vec<(Operator, Node)>),
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Label<'a>(pub &'a str);
impl Node {
pub fn label(lbl: &str) -> Self {
Self::Label(lbl.to_string())
}
pub fn relative_label(lbl: &str) -> Self {
Self::RelativeLabel(lbl.to_string())
}
}
impl<'a> Display for Label<'a> {
#[derive(Debug, PartialEq, Clone)]
pub struct Label(pub String);
impl Display for Label {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum VASMLine<'a> {
Instruction(Option<Label<'a>>, Opcode, Option<Node<'a>>),
Db(Option<Label<'a>>, Node<'a>),
StringDb(Option<Label<'a>>, String),
Org(Option<Label<'a>>, Node<'a>),
Equ(Label<'a>, Node<'a>),
LabelDef(Label<'a>),
impl From<&str> for Label {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl<'a> VASMLine<'a> {
pub fn label(&self) -> Option<Label<'a>> {
pub type Scope = BTreeMap<String, i32>;
#[derive(Debug, PartialEq, Clone)]
pub enum Macro {
Include(String),
If,
Unless,
Else,
While,
Until,
Do,
End,
}
#[derive(Debug, PartialEq, Clone)]
pub enum VASMLine {
Instruction(Option<Label>, Opcode, Option<Node>),
Db(Option<Label>, Node),
StringDb(Option<Label>, String),
Org(Option<Label>, Node),
Equ(Label, Node),
LabelDef(Label),
Macro(Macro),
}
impl VASMLine {
pub fn label(&self) -> Option<&Label> {
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),
| VASMLine::LabelDef(lbl) => Some(lbl),
_ => None,
}
}
pub fn zero_length(&self) -> bool {
matches!(
self,
VASMLine::Org(_, _) | VASMLine::Equ(_, _) | VASMLine::LabelDef(_)
VASMLine::Org(_, _) | VASMLine::Equ(_, _) | VASMLine::LabelDef(_) | VASMLine::Macro(_)
)
}
}