Panzerschrek / U-00DC-Sprache

"Ü" programming language development
https://panzerschrek.github.io/U-00DC-Sprache-site/
BSD 3-Clause "New" or "Revised" License
55 stars 2 forks source link

Easy syntax for variable construction in expression context #11

Open Panzerschrek opened 1 year ago

Panzerschrek commented 1 year ago

Right now it is possible to construct temp variable in expression context by using type name and () operator (for constructor call). It works fine, but works only for cases where constructor initializer is applicable - for fundamental types, classes, for copying of arrays/tuples. It might be good to introduce some syntax for construction of temp variable with arbitrary initializer - sequence initializer (arrays, tuples), struct named initializer, zero initializer uninitialized initializer.

For structs it may my possible to allow struct named initializer as expression postfix operator. For now introducing {} in expression context doesn't break something.

Sequence initializer "as is" is not possible to use in expression context. It starts with [ - exactly like indexation operator.

Alternative way to simplify temp variables construction for arrays and tuples is to introduce some special operators for this. Right now there are _makearray and _maketuple standard library functions. They work somehow, but it may be better to implement same functionality inside language core - at least compilation may be faster.

Yet another way - to add some proxy syntax operator. Like some lexem or sequence of lexems that triggers parsing of initializer. For example (with @):

auto some_var = MyStruct @ { .x= 0, .y= 42 };
auto another_var= [ i32, 4 ] @ [ 5, 55, 555, 5555 ];
auto trash= unsafe( MyStruct @ uninitialized );
auto zeros = tup[ f32, i32 ] @ zero_init;
Panzerschrek commented 1 year ago

Approach with _makearray and _maketuple as separate operators seems to be interesting. It doesn't require type name specifying - exact type will be evaluated based on actual argument count and types.

Panzerschrek commented 1 year ago

It may be possible to always parse postfix [ as sequence initializer. Later in CodeBuilder its processing will depend on kind of name before [. If it is variable - process it as indexation operator. If it is type name - process it as sequence initializer.

Panzerschrek commented 7 months ago

Suggestions for initialization invocation syntax:

It's better to avoid lexems like => , because it may be confused with >= and <=> - which both mean comparison.

Panzerschrek commented 7 months ago

https://github.com/Panzerschrek/U-00DC-Sprache/commit/4dc627289da1d56ca1c6d8542a75d5cd4fbb9bb4

Now it's possible to create structs in expression context with {} initializer.