Add scope.inner

This commit is contained in:
Sofia 2024-08-22 00:34:58 +03:00
parent 8b8cd2c464
commit 6a4c30e49e
4 changed files with 17 additions and 18 deletions

View File

@ -1,9 +1,9 @@
use reid::compile; use reid::compile;
pub static EASY: &str = include_str!("./reid/easy.reid"); pub static ARITHMETIC: &str = include_str!("./reid/arithmetic.reid");
fn main() { fn main() {
let text = match compile(EASY) { let text = match compile(ARITHMETIC) {
Ok(t) => t, Ok(t) => t,
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
}; };

View File

@ -1,17 +1,9 @@
// Hello, comment here! // Main
import std::print;
fn main() { fn main() {
let hello = 32 + { return fibonacci(10);
2 + 3
};
let beep = hello + fibonacci();
return beep;
} }
// Fibonacci // Fibonacci
fn fibonacci(value: i32) -> i32 { fn fibonacci(value: i32) -> i32 {
if value < 3 { if value < 3 {
return 1; return 1;

View File

@ -43,7 +43,7 @@ impl FunctionDefinition {
let FunctionDefinition(signature, block, _) = self; let FunctionDefinition(signature, block, _) = self;
let mut ir_function = IRFunction::new(&signature.name, module); let mut ir_function = IRFunction::new(&signature.name, module);
let ir_block = IRBlock::new(&mut ir_function); 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> { fn with_block<'a, 'b, 'c>(self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
Scope { Scope { data: self, block }
data: self.clone(), }
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 { fn fetch(&self, name: &String) -> IRValue {
@ -138,3 +139,9 @@ struct Scope<'a, 'b, 'c> {
data: ScopeData, data: ScopeData,
block: IRBlock<'a, 'b, 'c>, 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)
}
}