JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

Ternary expressions #364

Closed JSAbrahams closed 1 year ago

JSAbrahams commented 1 year ago

Relevant issues

Closes #164 Should've been a separate issue but couldn't be bothered to create one.

Introduced bug highlighted in #366 . Discovered bug highlighted in #367

Summary

Treat if expressions as expressions where relevant by converting them to python ternary expressions.

Added Tests

JSAbrahams commented 1 year ago

Hm what about functions which have code blocks as a body but which should be treated as an expression. I.e.:

def my_var := if cond then
    print("then")
    10
else
    print("else")
    20

This should then become the following python code:

if cond then
    print("then")
    my_var = 10
else
    print("else")
    my_var = 20
JSAbrahams commented 1 year ago

Before merging, it'd be nice actually to check other situations where an if expression should be converted to a Python ternary expression. Currently, we only do this within a function argument. What about using an (inline-)if expressions for:

I suspect it will work in these situations as well (and it should, we want to be as generic as possible here!). But alas the project has reached a level of complexity where I can't accurately predict if it will, so we must test.

codecov[bot] commented 1 year ago

Codecov Report

Merging #364 (b52c7e1) into develop (b12963b) will increase coverage by 0.06%. The diff coverage is 92.01%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #364      +/-   ##
===========================================
+ Coverage    86.99%   87.06%   +0.06%     
===========================================
  Files          108      108              
  Lines        11727    11965     +238     
===========================================
+ Hits         10202    10417     +215     
- Misses        1525     1548      +23     
JSAbrahams commented 1 year ago

Looking at codecov, few notes: