From f997fdaf64d48bb308bbacaec8838fe49005aa0e Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sat, 10 Sep 2022 11:58:16 +0200 Subject: [PATCH] Add string reference word --- henceforth/examples/test.fth | 3 ++- henceforth/src/lib/compiler.rs | 6 +++++- henceforth/src/lib/compiler/instruction.rs | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/henceforth/examples/test.fth b/henceforth/examples/test.fth index dda7c08..5e95ade 100644 --- a/henceforth/examples/test.fth +++ b/henceforth/examples/test.fth @@ -1,2 +1,3 @@ -depth dup dup dup dup dup debug +." test-string" cr +r" bliblablub" debug diff --git a/henceforth/src/lib/compiler.rs b/henceforth/src/lib/compiler.rs index d3a321d..2bb6273 100644 --- a/henceforth/src/lib/compiler.rs +++ b/henceforth/src/lib/compiler.rs @@ -55,7 +55,11 @@ impl Data { instructions.push(match mode.as_str() { "." => { let id = self.strings.insert_full(string).0; - Instruction::DotQuote(id) + Instruction::StringPrint(id) + } + "r" => { + let id = self.strings.insert_full(string).0; + Instruction::StringReference(id) } "asm" => Instruction::AsmQuote(string), _ => bail!("Unknown string mode: {}", mode), diff --git a/henceforth/src/lib/compiler/instruction.rs b/henceforth/src/lib/compiler/instruction.rs index c87114b..08fd3f9 100644 --- a/henceforth/src/lib/compiler/instruction.rs +++ b/henceforth/src/lib/compiler/instruction.rs @@ -31,7 +31,8 @@ pub enum Instruction { Space, Spaces, Cr, - DotQuote(usize), + StringPrint(usize), + StringReference(usize), Count, Not, And, @@ -653,7 +654,7 @@ impl compiler::Compilable for Instruct args: vec![hence::arg::Arg::Variable("CORE_MEM_CHR".to_string())], }, ]), - Instruction::DotQuote(x) => { + Instruction::StringPrint(x) => { let loop_label = format!("loop_strings_{}", x); let data_label = format!("data_strings_{}", x); let data_end_label = format!("data_strings_end_{}", x); @@ -719,6 +720,16 @@ impl compiler::Compilable for Instruct }, ]) } + Instruction::StringReference(x) => Ok(vec![ + hence::parser::ast::Node::Call { + name: "push".to_string(), + arg: Some(hence::arg::Arg::Variable(format!("data_strings_end_{}", x))), + }, + hence::parser::ast::Node::Call { + name: "push".to_string(), + arg: Some(hence::arg::Arg::Variable(format!("data_strings_{}", x))), + }, + ]), Instruction::Count => Ok(vec![]), Instruction::Not => Ok(vec![ hence::parser::ast::Node::MacroCall {