AnyDSL / artic

The AlteRnaTive Impala Compiler
MIT License
25 stars 6 forks source link

Tuple-like structs and struct-like enum options #3

Closed Hugobros3 closed 3 years ago

Hugobros3 commented 3 years ago

This PR adds support for "tuple-like" structures and "struct-like" enum options, as described in https://github.com/AnyDSL/artic/projects/7. This also includes "empty" tuple-like structs, which do not have either parentheses or brackets following them.

struct S { x: i32 }
struct T(i32, f32); // new
struct E; // new
enum E {
  A,
  B(i32),
  C { x: i32 } // new
}

The syntax for constructing and matching on structs or enum options has to match the syntax used when declaring them: This means you cannot initialize something like struct S { x: f32, y: f32, z: f32 } with S(0.0, 0.0, 0.0). Likewise you cannot access the fields of a tuple-like struct by projection (ie s.0), instead you have to match and deconstruct them like tuples instead. It is not possible to match/deconstruct an empty tuple-like struct, and such an action would be pointless anyway since an empty struct carries no information.

In effect those changes make the different data constructor syntaxes (nothing, parentheses with ordered fields and curly brackets with named fields) orthogonal to structs and enums types. We now have "record" and "call" syntax for (de)constructing data, so therefore in the source this is reflected by renaming the AST nodes StructExpr/StructPtrn to RecordExpr/RecordPtrn and EnumPtrn to CallPtrn (matching the existing CallExpr).

madmann91 commented 3 years ago

For reference, from our discussion:

Hugobros3 commented 3 years ago

I still have to look into coverage, and fix the docs

Hugobros3 commented 3 years ago

It occurred to me I can simplify the codegen for constructing tuple-like enum variants, since capturing those constructors is no longer legal, we do not have to emit a continuation for those in Path::emit and can directly build the thorin variant in CallExpr::emit, just like I do for record-like enum variant initialization in RecordExpr: https://github.com/AnyDSL/artic/blob/tuple-like-struct/src/emit.cpp#L912