From dec2d22b80af7299b9b2c8db3970fa49cc7607f8 Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Tue, 5 Apr 2022 18:05:24 -0500 Subject: [PATCH] WIP, implementing macros --- vasm/src/ast.rs | 69 +++++--- vasm/src/parse_error.rs | 10 +- vasm/src/vasm.pest | 6 +- vasm/src/vasm_assembler.rs | 326 ++++++++++++++++++++++++++++++------- vasm/src/vasm_evaluator.rs | 103 +++++++----- vasm/src/vasm_parser.rs | 77 ++++++--- 6 files changed, 436 insertions(+), 155 deletions(-) diff --git a/vasm/src/ast.rs b/vasm/src/ast.rs index 6efd999..e385ee0 100644 --- a/vasm/src/ast.rs +++ b/vasm/src/ast.rs @@ -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>, Vec<(Operator, Node<'a>)>), + Expr(Box, 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>, Opcode, Option>), - Db(Option>, Node<'a>), - StringDb(Option>, String), - Org(Option>, 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> { +pub type Scope = BTreeMap; + +#[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