Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Structs #85

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ pub enum LetStmtTarget {
pub struct LetStmt {
pub is_mutable: bool,
pub target: LetStmtTarget,
pub value: LetValue,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LetValue {
Expr(Expression),
StructConstruct(StructConstruct),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StructConstruct {
pub name: Ident,
pub fields: Vec<FieldConstruct>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FieldConstruct {
pub name: Ident,
pub value: Expression,
}

Expand All @@ -38,6 +56,7 @@ pub struct ReturnStmt {
pub struct AssignStmt {
pub target: PathOp,
pub value: Expression,
pub is_deref: bool,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions crates/concrete_ast/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StructDecl {
pub is_pub: bool,
pub doc_string: Option<DocString>,
pub name: Ident,
pub type_params: Vec<GenericParam>,
Expand Down
42 changes: 41 additions & 1 deletion crates/concrete_ast/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum TypeSpec {
},
Array {
of_type: Box<Self>,
size: Option<u64>,
size: Option<u32>,
is_ref: Option<RefType>,
span: Span,
},
Expand All @@ -36,13 +36,53 @@ impl TypeSpec {
}
}

pub fn is_mut_ref(&self) -> bool {
matches!(self.is_ref(), Some(RefType::MutBorrow))
}

pub fn get_name(&self) -> String {
match self {
TypeSpec::Simple { name, .. } => name.name.clone(),
TypeSpec::Generic { name, .. } => name.name.clone(),
TypeSpec::Array { of_type, .. } => format!("[{}]", of_type.get_name()),
}
}

pub fn to_nonref_type(&self) -> TypeSpec {
match self {
TypeSpec::Simple {
name,
is_ref: _,
span,
} => TypeSpec::Simple {
name: name.clone(),
is_ref: None,
span: *span,
},
TypeSpec::Generic {
name,
is_ref: _,
type_params,
span,
} => TypeSpec::Generic {
name: name.clone(),
is_ref: None,
type_params: type_params.clone(),
span: *span,
},
TypeSpec::Array {
of_type,
size,
is_ref: _,
span,
} => TypeSpec::Array {
of_type: of_type.clone(),
size: *size,
is_ref: None,
span: *span,
},
}
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down
4 changes: 3 additions & 1 deletion crates/concrete_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ impl<'parent> ScopeContext<'parent> {
"f64" => name.to_string(),
"bool" => name.to_string(),
name => {
if let Some(module) = self.imports.get(name) {
if let Some(x) = self.module_info.structs.get(name) {
name.to_string()
} else if let Some(module) = self.imports.get(name) {
// a import
self.resolve_type_spec(&module.types.get(name)?.value)?
} else {
Expand Down
Loading
Loading