Skip to content

Commit

Permalink
Support block exprssions
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-phillips committed Aug 23, 2024
1 parent d195553 commit 5c1faf5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ PRs are welcome.

### Some of the things I know are missing

- no case expressions
- block expressions aren't supported yet
- no destructuring/pattern matching in let
- no let assert
- no case expressions
- label aliases aren't supported yet (ie `fn foo(bar bas: Str)`)
- const definitions aren't supported yet (module.constants)
- type aliases aren't supported yet (module.type_aliases)
Expand Down
22 changes: 20 additions & 2 deletions src/compiler/internal/transformer/statements.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ fn transform_expression(
glance.Fn(arguments:, return_annotation: _, body:) ->
transform_fn(context, arguments, body)

glance.Block(statements) -> transform_block(context, statements)

glance.TupleIndex(tuple, index) -> {
transform_expression(context, tuple)
|> internal.map_return(python.TupleIndex(_, index))
Expand Down Expand Up @@ -320,8 +322,7 @@ fn transform_fn(
}
})

let function_name =
"_fn_def_" <> { context.next_function_id |> int.to_string }
let function_name = "_fn_def_" <> int.to_string(context.next_function_id)
let function =
python.Function(function_name, parameters, transform_statement_block(body))

Expand All @@ -335,6 +336,23 @@ fn transform_fn(
)
}

fn transform_block(
context: internal.TransformerContext,
body: List(glance.Statement),
) {
let function_name = "_fn_block_" <> int.to_string(context.next_block_id)
let function =
python.Function(function_name, [], transform_statement_block(body))
internal.ExpressionReturn(
context: internal.TransformerContext(
..context,
next_block_id: context.next_block_id + 1,
),
statements: [python.FunctionDef(function)],
expression: python.Call(python.Variable(function_name), []),
)
}

fn transform_pipe(
context: internal.TransformerContext,
left: glance.Expression,
Expand Down
56 changes: 56 additions & 0 deletions test/expression_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,59 @@ def main():
foo = _fn_def_0",
)
}

pub fn simple_block_test() {
"pub fn main() {
let foo = {1}
}"
|> compiler.compile
|> should.be_ok
|> should.equal(
"from gleam_builtins import *
def main():
def _fn_block_0():
return 1
foo = _fn_block_0()",
)
}

pub fn multiple_block_test() {
"pub fn main() {
let foo = {1}
let bar = {2}
}"
|> compiler.compile
|> should.be_ok
|> should.equal(
"from gleam_builtins import *
def main():
def _fn_block_0():
return 1
foo = _fn_block_0()
def _fn_block_1():
return 2
bar = _fn_block_1()",
)
}

pub fn nested_block_test() {
"pub fn main() {
let foo = {
{ 1 }
}
}"
|> compiler.compile
|> should.be_ok
|> should.equal(
"from gleam_builtins import *
def main():
def _fn_block_0():
def _fn_block_0():
return 1
return _fn_block_0()
foo = _fn_block_0()",
)
}

0 comments on commit 5c1faf5

Please sign in to comment.