Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Mar 29, 2022
1 parent bbdedfc commit 96bc925
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 98 deletions.
23 changes: 16 additions & 7 deletions crates/lowering/src/ast_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fe_analyzer::namespace::types::FixedSize;
use fe_parser::ast::{BoolOperator, CallArg, Expr, FuncStmt, UnaryOperator, VarDeclTarget};
use fe_parser::node::{Node, NodeId};
use fe_parser::node::{Node, NodeId, Span};

use crate::names;
use crate::utils::ZeroSpanNode;
Expand Down Expand Up @@ -90,7 +90,13 @@ where
error: error.map(|val| map_ast_node(val.into(), map_fn).as_expr()),
},
FuncStmt::Unsafe(body) => FuncStmt::Unsafe(map_body(body, map_fn)),
FuncStmt::VarDecl { target, typ, value } => FuncStmt::VarDecl {
FuncStmt::VarDecl {
mut_,
target,
typ,
value,
} => FuncStmt::VarDecl {
mut_,
target,
typ,
value: value.map(|val| map_ast_node(val.into(), map_fn).as_expr()),
Expand Down Expand Up @@ -448,6 +454,7 @@ pub fn ternary_to_if(
} = &expr.kind
{
let mut stmts = vec![FuncStmt::VarDecl {
mut_: Some(Span::dummy()),
target: VarDeclTarget::Name(result_name.into()).into_node(),
typ: names::fixed_size_type_desc(&ternary_type).into_node(),
value: None,
Expand Down Expand Up @@ -505,6 +512,7 @@ pub fn boolean_expr_to_if(
// res = right

let mut stmts = vec![FuncStmt::VarDecl {
mut_: Some(Span::dummy()),
target: VarDeclTarget::Name(result_name.into()).into_node(),
typ: names::fixed_size_type_desc(&expr_type).into_node(),
value: Some(Expr::Bool(false).into_node()),
Expand Down Expand Up @@ -533,6 +541,7 @@ pub fn boolean_expr_to_if(
// if not left:
// res = right
let mut stmts = vec![FuncStmt::VarDecl {
mut_: Some(Span::dummy()),
target: VarDeclTarget::Name(result_name.into()).into_node(),
typ: names::fixed_size_type_desc(&expr_type).into_node(),
value: Some(Expr::Bool(true).into_node()),
Expand Down Expand Up @@ -892,7 +901,7 @@ else:

assert_eq!(
to_code(&transformed_outer),
"let outer: u256
"let mut outer: u256
if true:
outer = 1
else:
Expand All @@ -907,7 +916,7 @@ else:

assert_eq!(
to_code(&new_body),
"let outer: u256
"let mut outer: u256
if true:
outer = 1
else:
Expand All @@ -921,7 +930,7 @@ foo(1 if true else 1 if true else 2)"

assert_eq!(
to_code(&new_body),
"let outer: u256
"let mut outer: u256
if true:
outer = 1
else:
Expand All @@ -942,11 +951,11 @@ foo(outer)"

assert_eq!(
to_code(&new_body),
r#"let outer: u256
r#"let mut outer: u256
if true:
outer = 1
else:
let inner: u256
let mut inner: u256
if true:
inner = 1
else:
Expand Down
51 changes: 38 additions & 13 deletions crates/lowering/src/mappers/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::utils::ZeroSpanNode;
use fe_analyzer::namespace::items::FunctionId;
use fe_analyzer::namespace::types::{Base, Type};
use fe_analyzer::namespace::types::{FixedSize, TypeDowncast};
use fe_parser::ast::{self as fe, Expr, FuncStmt, RegularFunctionArg, SmolStr};
use fe_parser::node::Node;
use fe_parser::ast::{self as fe, Expr, FuncStmt, SmolStr};
use fe_parser::node::{Node, Span};

/// Lowers a function definition.
pub fn func_def(context: &mut ModuleContext, function: FunctionId) -> Node<fe::Function> {
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn func_def(context: &mut ModuleContext, function: FunctionId) -> Node<fe::F
if matches!(
args.first(),
Some(Node {
kind: fe::FunctionArg::Self_,
kind: fe::FunctionArg::Self_ { .. },
..
})
) {
Expand All @@ -88,12 +88,19 @@ pub fn func_def(context: &mut ModuleContext, function: FunctionId) -> Node<fe::F
.iter()
.zip(param_types)
.map(|(pnode, ptype)| {
if let fe::FunctionArg::Regular(regular) = &pnode.kind {
fe::FunctionArg::Regular(RegularFunctionArg {
label: regular.label.clone(),
name: regular.name.clone(),
typ: types::type_desc(fn_ctx.module, regular.typ.clone(), &ptype),
})
if let fe::FunctionArg::Regular {
mut_,
label,
name,
typ,
} = &pnode.kind
{
fe::FunctionArg::Regular {
mut_: *mut_,
label: label.clone(),
name: name.clone(),
typ: types::type_desc(fn_ctx.module, typ.clone(), &ptype),
}
.into_node()
} else {
// the self arg remains the same
Expand Down Expand Up @@ -164,7 +171,12 @@ fn lower_iteratively(
fn func_stmt(context: &mut FnContext, stmt: Node<fe::FuncStmt>) -> Vec<Node<fe::FuncStmt>> {
let lowered_kinds = match stmt.kind {
fe::FuncStmt::Return { value } => stmt_return(context, value),
fe::FuncStmt::VarDecl { target, typ, value } => {
fe::FuncStmt::VarDecl {
mut_,
target,
typ,
value,
} => {
let var_type = context
.var_decl_type(typ.id)
.expect("missing var decl type")
Expand All @@ -173,13 +185,20 @@ fn func_stmt(context: &mut FnContext, stmt: Node<fe::FuncStmt>) -> Vec<Node<fe::

match target.kind {
fe::VarDeclTarget::Name(_) => vec![fe::FuncStmt::VarDecl {
mut_,
target,
typ: types::type_desc(context.module, typ, &var_type),
value: expressions::optional_expr(context, value),
}],
fe::VarDeclTarget::Tuple(_) => {
lower_tuple_destructuring(context, target, typ, &var_type, value, stmt.span)
}
fe::VarDeclTarget::Tuple(_) => lower_tuple_destructuring(
context,
mut_.is_some(),
target,
typ,
&var_type,
value,
stmt.span,
),
}
}
fe::FuncStmt::ConstantDecl { name, typ, value } => {
Expand Down Expand Up @@ -318,6 +337,7 @@ fn is_last_statement_return(stmts: &[Node<fe::FuncStmt>]) -> bool {
/// ```
fn lower_tuple_destructuring(
context: &mut FnContext,
mut_: bool,
target: Node<fe::VarDeclTarget>,
type_desc: Node<fe::TypeDesc>,
typ: &Type,
Expand All @@ -327,13 +347,15 @@ fn lower_tuple_destructuring(
let mut stmts = vec![];
let tmp_tuple: SmolStr = context.make_unique_name("tmp_tuple").into();
stmts.push(fe::FuncStmt::VarDecl {
mut_: mut_.then(|| Span::dummy()),
target: Node::new(fe::VarDeclTarget::Name(tmp_tuple.clone()), span),
typ: types::type_desc(context.module, type_desc.clone(), typ),
value: expressions::optional_expr(context, value),
});

declare_tuple_items(
context,
mut_,
target,
type_desc,
typ,
Expand All @@ -346,6 +368,7 @@ fn lower_tuple_destructuring(

fn declare_tuple_items(
context: &mut FnContext,
mut_: bool,
target: Node<fe::VarDeclTarget>,
type_desc: Node<fe::TypeDesc>,
typ: &Type,
Expand All @@ -365,6 +388,7 @@ fn declare_tuple_items(
}

stmts.push(fe::FuncStmt::VarDecl {
mut_: mut_.then(|| Span::dummy()),
target,
typ: types::type_desc(context.module, type_desc, typ),
value: expressions::optional_expr(context, Some(value)),
Expand All @@ -389,6 +413,7 @@ fn declare_tuple_items(
indices.push(index);
declare_tuple_items(
context,
mut_,
target,
type_desc,
&typ.clone().into(),
Expand Down
8 changes: 5 additions & 3 deletions crates/lowering/src/mappers/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fe_analyzer::namespace::items::{Item, ModuleId, TypeDef};
use fe_analyzer::namespace::types::{Array, Base, FixedSize, Tuple};
use fe_analyzer::AnalyzerDb;
use fe_parser::ast::{self, SmolStr};
use fe_parser::node::Node;
use fe_parser::node::{Node, Span};

/// Lowers a module.
pub fn module(db: &dyn AnalyzerDb, module: ModuleId) -> ast::Module {
Expand Down Expand Up @@ -154,18 +154,20 @@ fn list_expr_to_fn_def(array: &Array) -> ast::Function {
// Built the AST nodes for the function arguments
let args = (0..array.size)
.map(|index| {
ast::FunctionArg::Regular(ast::RegularFunctionArg {
ast::FunctionArg::Regular {
mut_: None,
label: Some(SmolStr::new("_").into_node()),
name: SmolStr::new(format!("val{}", index)).into_node(),
typ: names::fixed_size_type_desc(&FixedSize::Base(array.inner)).into_node(),
})
}
.into_node()
})
.collect::<Vec<_>>();

// Build the AST node for the array declaration
let var_decl_name = "generated_array";
let var_decl = ast::FuncStmt::VarDecl {
mut_: Some(Span::dummy()),
target: ast::VarDeclTarget::Name(var_decl_name.into()).into_node(),
typ: names::fixed_size_type_desc(&FixedSize::Array(array.clone())).into_node(),
value: None,
Expand Down
Loading

0 comments on commit 96bc925

Please sign in to comment.