From 35256904c49a961fbaebbd134539516725c53455 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Fri, 20 Dec 2024 10:52:27 +0100 Subject: Find variable values on parent scopes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a given variable cannot be found on the current scope, attempt to go up the context hierarchy to find it. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/assembler.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lib/xixanta/src/assembler.rs') diff --git a/lib/xixanta/src/assembler.rs b/lib/xixanta/src/assembler.rs index 4e33d66..888e122 100644 --- a/lib/xixanta/src/assembler.rs +++ b/lib/xixanta/src/assembler.rs @@ -1774,6 +1774,40 @@ adc Variable assert_eq!(instr.bytes[1], 0x04); } + #[test] + fn reference_outer_variables() { + let mut asm = Assembler::new(EMPTY.to_vec()); + asm.mappings[0].segments[0].bundles = minimal_header(); + asm.mappings[0].offset = 6; + asm.current_mapping = 1; + let res = &asm + .assemble( + std::env::current_dir().unwrap().to_path_buf(), + r#" +foo: + rts + +.proc inner + jsr foo +.endproc +"# + .as_bytes(), + ) + .unwrap()[0x10..]; + + assert_eq!(res.len(), 2); + + // foo: rts + assert_eq!(res[0].size, 1); + assert_eq!(res[0].bytes[0], 0x60); + + // inner: jsr foo + assert_eq!(res[1].size, 3); + assert_eq!(res[1].bytes[0], 0x20); + assert_eq!(res[1].bytes[1], 0x00); + assert_eq!(res[1].bytes[2], 0x80); + } + #[test] fn bad_variable_but_valid_identifier_in_instruction() { assert_eval_error( -- cgit v1.2.3