From ac01eb527e24ad0bd966db8cef5bb04d4e0da97f Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Thu, 7 Apr 2022 17:45:34 -0500 Subject: [PATCH] More macro WIP --- vasm/src/vasm_assembler.rs | 53 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/vasm/src/vasm_assembler.rs b/vasm/src/vasm_assembler.rs index 3650984..bfaa162 100644 --- a/vasm/src/vasm_assembler.rs +++ b/vasm/src/vasm_assembler.rs @@ -61,6 +61,15 @@ enum LoopType { Until, } +impl From for LoopType { + fn from(mac: Macro) -> Self { + match mac { + Macro::While => LoopType::While, + Macro::Until => LoopType::Until, + _ => unreachable!() + } + } +} #[derive(Debug, Clone, PartialEq)] enum ControlStructure { Target(String), @@ -130,8 +139,16 @@ where return Err(AssembleError::MacroError(line_idx + 1)); } } - Macro::While => {} - Macro::Until => {} + Macro::While | + Macro::Until => { + let label = gensym(); + control_stack.push(ControlStructure::Loop(label.clone(), mac.into())); + all_lines.push(Line { + line: VASMLine::LabelDef(Label(label)), + line_num: line_idx + 1, + file: filename_stack.last().unwrap().clone(), + }) + } Macro::Do => {} Macro::End => {} }, @@ -665,4 +682,36 @@ mod test { ]) ) } + + #[test] + fn test_preprocess_while() { + let include = |_name: String| Ok(vec![]); + let lines = vec!["#while"]; + assert_eq!( + preprocess(lines, "blah".to_string(), &include), + Ok(vec![ + Line { + line_num: 1, + file: "blah".to_string(), + line: VASMLine::LabelDef(Label("__gensym_1".to_string())) + } + ]) + ) + } + + #[test] + fn test_preprocess_until() { + let include = |_name: String| Ok(vec![]); + let lines = vec!["#until"]; + assert_eq!( + preprocess(lines, "blah".to_string(), &include), + Ok(vec![ + Line { + line_num: 1, + file: "blah".to_string(), + line: VASMLine::LabelDef(Label("__gensym_1".to_string())) + } + ]) + ) + } }