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;
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),
};

View File

@ -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;

View File

@ -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)
}
}