Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 22, 2024
1 parent cad8186 commit b7f7014
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
3 changes: 3 additions & 0 deletions crates/concrete_ast/src/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::HashMap;

use crate::{
common::Ident,
statements::Statement,
structs::Field,
types::{RefType, TypeSpec},
};

Expand Down
19 changes: 19 additions & 0 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
common::Ident,
expressions::{Expression, FnCallOp, IfExpr, MatchExpr, PathOp},
structs::Field,
types::TypeSpec,
};

Expand All @@ -26,6 +27,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 Down
25 changes: 15 additions & 10 deletions crates/concrete_codegen_mlir/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use concrete_ast::{
},
functions::FunctionDef,
modules::{Module, ModuleDefItem},
statements::{AssignStmt, LetStmt, LetStmtTarget, ReturnStmt, Statement, WhileStmt},
statements::{AssignStmt, LetStmt, LetStmtTarget, LetValue, ReturnStmt, Statement, WhileStmt},
types::TypeSpec,
Program,
};
Expand Down Expand Up @@ -430,15 +430,20 @@ fn compile_let_stmt<'ctx, 'parent: 'ctx>(
LetStmtTarget::Simple { name, r#type } => {
let location = get_location(context, session, name.span.from);

let value = compile_expression(
session,
context,
scope_ctx,
helper,
block,
&info.value,
Some(r#type),
)?;
let value = match &info.value {
LetValue::Expr(value) => compile_expression(
session,
context,
scope_ctx,
helper,
block,
value,
Some(r#type),
)?,
LetValue::StructConstruct(_) => {
todo!()
}
};
let memref_type = MemRefType::new(value.r#type(), &[], None, None);

let alloca: Value = block
Expand Down
12 changes: 9 additions & 3 deletions crates/concrete_codegen_mlir/src/scope_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ impl<'ctx, 'parent> ScopeContext<'ctx, 'parent> {
}
}

/// Returns the struct type along with the field indexes.
fn get_struct_type(
&self,
context: &'ctx MeliorContext,
strct: &StructDecl,
) -> Result<Type<'ctx>, Box<dyn Error>> {
) -> Result<(Type<'ctx>, HashMap<String, usize>), Box<dyn Error>> {
let mut fields = Vec::with_capacity(strct.fields.len());

let mut field_indexes = HashMap::new();
let mut size: u32 = 0;

for field in &strct.fields {
Expand All @@ -148,6 +150,7 @@ impl<'ctx, 'parent> ScopeContext<'ctx, 'parent> {

size += pad;
size += ty_size;
field_indexes.insert(field.name.name.clone(), fields.len());
fields.push(ty);
}

Expand All @@ -165,7 +168,10 @@ impl<'ctx, 'parent> ScopeContext<'ctx, 'parent> {
));
}

Ok(llvm::r#type::r#struct(context, &fields, false))
Ok((
llvm::r#type::r#struct(context, &fields, false),
field_indexes,
))
}

pub fn resolve_type(
Expand All @@ -183,7 +189,7 @@ impl<'ctx, 'parent> ScopeContext<'ctx, 'parent> {
"bool" => IntegerType::new(context, 1).into(),
name => {
if let Some(strct) = self.module_info.structs.get(name) {
self.get_struct_type(context, strct)?
self.get_struct_type(context, strct)?.0
} else if let Some(module) = self.imports.get(name) {
// a import
self.resolve_type_spec(
Expand Down
27 changes: 25 additions & 2 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(crate) TypeSpec: ast::types::TypeSpec = {
},
<lo:@L> <is_ref:RefType?> "[" <of_type:TypeSpec> <size:(";" <"integer">)?> "]"<hi:@R> => ast::types::TypeSpec::Array {
of_type: Box::new(of_type),
size,
size: size.map(|x| x.try_into().expect("size is bigger than u32::MAX")),
is_ref,
span: Span::new(lo, hi),
}
Expand Down Expand Up @@ -480,8 +480,31 @@ pub(crate) LetStmt: ast::statements::LetStmt = {
name,
r#type: target_type
},
value
value: ast::statements::LetValue::Expr(value)
},
"let" <is_mutable:"mut"?> <name:Ident> ":" <target_type:TypeSpec> "=" <value:StructConstruct> => ast::statements::LetStmt {
is_mutable: is_mutable.is_some(),
target: ast::statements::LetStmtTarget::Simple {
name,
r#type: target_type
},
value: ast::statements::LetValue::StructConstruct(value)
},
}

pub(crate) StructConstruct: ast::statements::StructConstruct = {
<name:Ident> "{" <fields:Comma<FieldConstruct>> "}" => ast::statements::StructConstruct {
name,
fields,
}
}


pub(crate) FieldConstruct: ast::statements::FieldConstruct = {
<name:Ident> ":" <value:Expression> => ast::statements::FieldConstruct {
name,
value
}
}

pub(crate) AssignStmt: ast::statements::AssignStmt = {
Expand Down
6 changes: 6 additions & 0 deletions examples/structs.con
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ mod Fibonacci {
}

fn main() -> i64 {

let x: Node = Node {
a: 2,
b: 2,
};

return 1;
}
}

0 comments on commit b7f7014

Please sign in to comment.