erg-lang / erg

A statically typed language compatible with Python
http://erg-lang.org
Apache License 2.0
2.7k stars 55 forks source link

Add syntax sugar for `if`, `for`, `while` and `import` #527

Open mtshiba opened 1 month ago

mtshiba commented 1 month ago

We propose syntax sugar for some of Erg's built-in subroutines, which will make Erg code look more similar to Python and be easier to use for users familiar with Python.

New if syntax

if cond:
   ...
elif cond2: # optional
  ...
else: # optional
  ...

desugared:

if cond:
    do: ...
    else :=
        if cond2:
            do: ...
            else := do: ...

New if! syntax

if! cond:
   ...
elif! cond2: # optional
  ...
else!: # optional
  ...

New while! syntax

while! cond:
   ...
else!: # optional
  ...

desugared:

while! do! cond:
   do!: ...
   else! := do!: ...

New for! syntax

for! i in it:
    ...
else!:
    ...

desugared:

for! it:
    i => ...
    else! := do! ...

New import syntax

import foo
import foo as bar
import foo.bar as bar
from foo import bar
# import foo.bar # synyax error, we don't need this

desugared:

foo = import "foo"
bar = import "foo"
bar = import "foo/bar"
bar = import "foo/bar"