Files
vulcan/vasm/src/ast.rs
T

46 lines
1.1 KiB
Rust
Raw Normal View History

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),
2022-03-16 23:13:02 -05:00
String(String), // TODO: this shouldn't exist
2022-03-14 01:04:08 -05:00
Expr(Box<Node<'a>>, Vec<(Operator, Node<'a>)>),
}
#[derive(Debug, PartialEq, Clone)]
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)
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum VASMLine<'a> {
Instruction(Option<Label<'a>>, Opcode, Option<Node<'a>>),
Db(Option<Label<'a>>, Node<'a>),
Org(Option<Label<'a>>, Node<'a>),
Equ(Label<'a>, Node<'a>),
LabelDef(Label<'a>),
2022-03-14 01:04:08 -05:00
}