Skip to content

Commit

Permalink
import struct3
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 30, 2025
1 parent 3f514ef commit 7bf4662
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
21 changes: 21 additions & 0 deletions examples/import_struct.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mod A {
struct X {
a: i32
}

fn hello(x: X) -> i32 {
return x.a;
}
}

mod ImportStruct {
import A.{X, hello};

pub fn main() -> i32 {
let x: X = X {
a: 2,
};

return hello(x);
}
}
45 changes: 29 additions & 16 deletions src/ir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ fn lower_expression(
.symbols
.structs
.get(&info.name.name)
.or_else(|| builder.get_module_body().imports.get(&info.name.name))
.expect("struct not found");
let struct_body = builder.ctx.body.structs.get(&id).unwrap().clone();
let ty = Ty {
Expand Down Expand Up @@ -1960,26 +1961,38 @@ pub fn lower_type(
"char" => Ty::new(span, TyKind::Char),
other => {
let module = ctx.body.modules.get(&module_id).expect("module not found");
// Check if the type is a struct
if let Some(struct_id) = module.symbols.structs.get(other) {
let mut generic_tys = Vec::new();
for generic in &name.generics {
generic_tys.push(lower_type(
ctx,
&TypeDescriptor::Type {
name: generic.clone(),
span: generic.span,
},
module_id,
)?);
}
Ty::new(

// Find on imports or local module
let def_id = module
.imports
.get(other)
.or_else(|| module.symbols.structs.get(other))
.ok_or_else(|| LoweringError::UnrecognizedType {
span: *span,
name: other.to_string(),
program_id: module_id.program_id,
})?;

let mut generic_tys = Vec::new();
for generic in &name.generics {
generic_tys.push(lower_type(
ctx,
&TypeDescriptor::Type {
name: generic.clone(),
span: generic.span,
},
module_id,
)?);
}

if ctx.body.structs.contains_key(def_id) {
return Ok(Ty::new(
span,
TyKind::Struct {
id: *struct_id,
id: *def_id,
generics: generic_tys,
},
)
));
} else {
Err(LoweringError::UnrecognizedType {
span: *span,
Expand Down
1 change: 1 addition & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod common;
#[test_case(include_str!("../examples/factorial_if.con"), "factorial_if", false, 24 ; "factorial_if.con")]
#[test_case(include_str!("../examples/fib_if.con"), "fib_if", false, 55 ; "fib_if.con")]
#[test_case(include_str!("../examples/import.con"), "import", false, 12 ; "import.con")]
#[test_case(include_str!("../examples/import_struct.con"), "import_struct", false, 2 ; "import_struct.con")]
#[test_case(include_str!("../examples/impl_block.con"), "impl_block", false, 8 ; "impl_block.con")]
#[test_case(include_str!("../examples/impl_block_mut.con"), "impl_block_mut", false, 4 ; "impl_block_mut.con")]
#[test_case(include_str!("../examples/simple.con"), "simple", false, 8 ; "simple.con")]
Expand Down

0 comments on commit 7bf4662

Please sign in to comment.