From 6a4c30e49eb292d39cc030da547ae4599d0e4a35 Mon Sep 17 00:00:00 2001 From: sofia Date: Thu, 22 Aug 2024 00:34:58 +0300 Subject: [PATCH] Add scope.inner --- examples/{easy.rs => arithmetic.rs} | 4 ++-- examples/reid/{easy.reid => arithmetic.reid} | 0 examples/reid/fibonacci.reid | 12 ++---------- src/codegen/mod.rs | 19 +++++++++++++------ 4 files changed, 17 insertions(+), 18 deletions(-) rename examples/{easy.rs => arithmetic.rs} (52%) rename examples/reid/{easy.reid => arithmetic.reid} (100%) diff --git a/examples/easy.rs b/examples/arithmetic.rs similarity index 52% rename from examples/easy.rs rename to examples/arithmetic.rs index 747771d..5f9ecd6 100644 --- a/examples/easy.rs +++ b/examples/arithmetic.rs @@ -1,9 +1,9 @@ use reid::compile; -pub static EASY: &str = include_str!("./reid/easy.reid"); +pub static ARITHMETIC: &str = include_str!("./reid/arithmetic.reid"); fn main() { - let text = match compile(EASY) { + let text = match compile(ARITHMETIC) { Ok(t) => t, Err(e) => panic!("{}", e), }; diff --git a/examples/reid/easy.reid b/examples/reid/arithmetic.reid similarity index 100% rename from examples/reid/easy.reid rename to examples/reid/arithmetic.reid diff --git a/examples/reid/fibonacci.reid b/examples/reid/fibonacci.reid index 2033023..11faa1b 100644 --- a/examples/reid/fibonacci.reid +++ b/examples/reid/fibonacci.reid @@ -1,17 +1,9 @@ -// Hello, comment here! - -import std::print; - +// Main fn main() { - let hello = 32 + { - 2 + 3 - }; - let beep = hello + fibonacci(); - return beep; + return fibonacci(10); } // Fibonacci - fn fibonacci(value: i32) -> i32 { if value < 3 { return 1; diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index cbdf29c..88abc3b 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -43,7 +43,7 @@ impl FunctionDefinition { let FunctionDefinition(signature, block, _) = self; let mut ir_function = IRFunction::new(&signature.name, module); let ir_block = IRBlock::new(&mut ir_function); - block.codegen(scope.with_block(ir_block)); + block.codegen(scope.inner(ir_block)); } } @@ -112,11 +112,12 @@ impl ScopeData { } } - fn with_block<'a, 'b, 'c>(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> { - Scope { - data: self.clone(), - block, - } + fn with_block<'a, 'b, 'c>(self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> { + Scope { data: self, block } + } + + fn inner<'a, 'b, 'c>(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> { + self.clone().with_block(block) } fn fetch(&self, name: &String) -> IRValue { @@ -138,3 +139,9 @@ struct Scope<'a, 'b, 'c> { data: ScopeData, block: IRBlock<'a, 'b, 'c>, } + +impl<'a, 'b, 'c> Scope<'a, 'b, 'c> { + fn inner(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> { + self.data.clone().with_block(block) + } +}