From efd3082a66c9940168d5210ff17aa2ca4cc7811c Mon Sep 17 00:00:00 2001 From: TheCPP Date: Wed, 10 Jul 2024 18:34:58 +0200 Subject: [PATCH] [INIT] adding short initialization via functions for classes cuz rust doesn't support that out of the box and using new all the time is shit --- examples/simple.rs | 6 +++--- src/IR/block.rs | 5 +++++ src/IR/builder.rs | 5 +++++ src/IR/func.rs | 12 +++++++++++- src/IR/mod.rs | 2 +- src/IR/module.rs | 7 ++++++- src/IR/var.rs | 7 ++++++- src/lib.rs | 6 +----- 8 files changed, 38 insertions(+), 12 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index a49f09f1..a9b2592f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -2,10 +2,10 @@ use std::error::Error; use Ygen::prelude::*; pub fn main() -> Result<(), Box> { - let mut module = Module::new(); - let mut builder = IRBuilder::new(); + let mut module = Module(); + let mut builder = IRBuilder(); - let ty = FunctionType::new(vec![TypeMetadata::i32, TypeMetadata::i32], TypeMetadata::i32); + let ty = FnTy(vec![TypeMetadata::i32, TypeMetadata::i32], TypeMetadata::i32); let func = module.add( "add", &ty ); diff --git a/src/IR/block.rs b/src/IR/block.rs index 27c3a2df..866f920d 100644 --- a/src/IR/block.rs +++ b/src/IR/block.rs @@ -70,4 +70,9 @@ impl Block { Ok(()) } +} + +/// Creates an new block +pub fn Block(name: &str, func: &Function) -> Block { + Block::new(name, func) } \ No newline at end of file diff --git a/src/IR/builder.rs b/src/IR/builder.rs index f8f2dabf..ea2ba6e9 100644 --- a/src/IR/builder.rs +++ b/src/IR/builder.rs @@ -29,4 +29,9 @@ impl<'a> IRBuilder<'a> { self.blocks.push_front(block); self.curr = 0; // Can cause an intenger underflow but shouldn't } +} + +/// Creates an new IRBuilder +pub fn IRBuilder<'a>() -> IRBuilder<'a> { + IRBuilder::new() } \ No newline at end of file diff --git a/src/IR/func.rs b/src/IR/func.rs index 2b0948c0..86aef70f 100644 --- a/src/IR/func.rs +++ b/src/IR/func.rs @@ -61,7 +61,7 @@ pub struct Function { } impl Function { - /// Creates an new Function + /// Creates a new Function pub fn new(name: String, ty: FunctionType) -> Self { Self { ty: ty, @@ -144,3 +144,13 @@ impl Function { Ok(()) } } + +/// Creates a new function type +pub fn FnTy(args: Vec, ret: TypeMetadata) -> FunctionType { + FunctionType::new(args, ret) +} + +/// Creates a new Function +pub fn Func(name: String, ty: FunctionType) -> Function { + Function::new(name, ty) +} \ No newline at end of file diff --git a/src/IR/mod.rs b/src/IR/mod.rs index c5a512a8..408f7f0d 100644 --- a/src/IR/mod.rs +++ b/src/IR/mod.rs @@ -11,7 +11,7 @@ use std::error::Error; use std::fmt::Display; pub use module::Module; -pub use func::{Function, FunctionType}; +pub use func::{Function, FunctionType, FnTy, Func}; pub use typ::Type; pub use typ::TypeMetadata; pub use builder::IRBuilder; diff --git a/src/IR/module.rs b/src/IR/module.rs index 81884b83..7e9ed97c 100644 --- a/src/IR/module.rs +++ b/src/IR/module.rs @@ -9,7 +9,7 @@ pub struct Module { } impl Module { - /// Creates a new Builder + /// Creates a new module pub fn new() -> Self { Self { funcs: HashMap::new(), @@ -72,3 +72,8 @@ impl Module { Ok(()) } } + +/// Creates a new module +pub fn Module() -> Module { + Module::new() +} diff --git a/src/IR/var.rs b/src/IR/var.rs index a1268f16..c0092989 100644 --- a/src/IR/var.rs +++ b/src/IR/var.rs @@ -8,11 +8,16 @@ pub struct Var { } impl Var { - /// Creats an new variable + /// Creats a new variable pub fn new(block: &mut Block, ty: TypeMetadata) -> Self { Self { name: format!("%{}", block.reqVarName()), ty: ty, } } +} + +/// Creates a new variable +pub fn Var(block: &mut Block, ty: TypeMetadata) -> Var { + Var::new(block, ty) } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index ddd7ba6d..aeace7ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,11 +23,7 @@ pub mod Support; /// Most common used functions, classes, enums of this Libary pub mod prelude { - pub use crate::IR::Module; - pub use crate::IR::IRBuilder; - pub use crate::IR::Type; - pub use crate::IR::FunctionType; - pub use crate::IR::TypeMetadata; + pub use crate::IR::*; pub use crate::Target::Triple; pub use crate::Support::PrintErrorAndExit;