Skip to content

Commit

Permalink
[INIT] adding short initialization via functions for classes cuz rust…
Browse files Browse the repository at this point in the history
… doesn't support that out of the box and using new all the time is shit
  • Loading branch information
Cr0a3 committed Jul 10, 2024
1 parent 6673d08 commit efd3082
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
6 changes: 3 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::error::Error;
use Ygen::prelude::*;

pub fn main() -> Result<(), Box<dyn Error>> {
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
);
Expand Down
5 changes: 5 additions & 0 deletions src/IR/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ impl Block {

Ok(())
}
}

/// Creates an new block
pub fn Block(name: &str, func: &Function) -> Block {
Block::new(name, func)
}
5 changes: 5 additions & 0 deletions src/IR/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
12 changes: 11 additions & 1 deletion src/IR/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -144,3 +144,13 @@ impl Function {
Ok(())
}
}

/// Creates a new function type
pub fn FnTy(args: Vec<TypeMetadata>, ret: TypeMetadata) -> FunctionType {
FunctionType::new(args, ret)
}

/// Creates a new Function
pub fn Func(name: String, ty: FunctionType) -> Function {
Function::new(name, ty)
}
2 changes: 1 addition & 1 deletion src/IR/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/IR/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -72,3 +72,8 @@ impl Module {
Ok(())
}
}

/// Creates a new module
pub fn Module() -> Module {
Module::new()
}
7 changes: 6 additions & 1 deletion src/IR/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit efd3082

Please sign in to comment.