CharlyCst / zephyr

A Programming Language built for WebAssembly
https://charlycst.github.io/zephyr-playground/
10 stars 0 forks source link

Allow shorthand in Struct literals #95

Closed CharlyCst closed 3 years ago

CharlyCst commented 3 years ago

What?

Struct literal require to specify the field for each value, even when it is redundant with the name of the variable:

struct Point {
    x: i32
    y: i32
}

Point { x: x, y: y }

With shorthand (the Rust way) this could be simplified to:

Point { x, y }

How?

Update the grammar (notice the optionnal part in a field):

struct_literal -> IDENTIFIER "{" (field ( ("," | ";") field )* ("," | ";")?)? "}"
field          -> IDENTIFIER ( ":" expression )?

Then if the field is missing, take the identifier as the field name.