basic support for structures through the IR

This commit is contained in:
2024-03-16 16:41:23 -07:00
parent b0cc2fc26b
commit a7b85d37da
12 changed files with 572 additions and 94 deletions

View File

@@ -33,7 +33,7 @@ pub enum TopLevel {
Option<Type>,
Expression,
),
Structure(Location, Option<Name>, Vec<(Name, Type)>),
Structure(Location, Name, Vec<(Name, Type)>),
}
/// A Name.
@@ -212,5 +212,5 @@ pub enum Value {
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Type {
Named(Name),
Struct(Option<Name>, Vec<(Option<Name>, Option<Type>)>),
Struct(Vec<(Name, Option<Type>)>),
}

View File

@@ -100,8 +100,8 @@ OptionalComma: () = {
}
Structure: TopLevel = {
<s:@L> "struct" <on: TypeName?> "{" <fields: Field*> "}" <e:@L> => {
TopLevel::Structure(Location::new(file_idx, s..e), on, fields)
<s:@L> "struct" <n: TypeName> "{" <fields: Field*> "}" <e:@L> => {
TopLevel::Structure(Location::new(file_idx, s..e), n, fields)
}
}
@@ -113,14 +113,13 @@ Field: (Name, Type) = {
Type: Type = {
<name:Name> => Type::Named(name),
<t:TypeName> => Type::Named(t),
"struct" <on: TypeName?> "{" <fields: TypeField*> "}" =>
Type::Struct(on, fields),
"struct" "{" <fields: TypeField*> "}" =>
Type::Struct(fields),
}
TypeField: (Option<Name>, Option<Type>) = {
<name: Name> ":" <ty: Type> ";" => (Some(name), Some(ty)),
<name: Name> (":" "_")? ";" => (Some(name), None),
"_" ":" <ty: Type> ";" => (None, Some(ty)),
TypeField: (Name, Option<Type>) = {
<name: Name> ":" <ty: Type> ";" => (name, Some(ty)),
<name: Name> (":" "_")? ";" => (name, None),
}
Name: Name = {

View File

@@ -65,11 +65,7 @@ impl TopLevel {
TopLevel::Structure(_, name, fields) => allocator
.text("struct")
.append(allocator.space())
.append(
name.as_ref()
.map(|x| allocator.text(x.to_string()))
.unwrap_or_else(|| allocator.nil()),
)
.append(allocator.text(name.to_string()))
.append(allocator.space())
.append(allocator.text("{"))
.append(allocator.hardline())
@@ -224,22 +220,13 @@ impl Type {
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
match self {
Type::Named(x) => allocator.text(x.to_string()),
Type::Struct(name, fields) => allocator
Type::Struct(fields) => allocator
.text("struct")
.append(allocator.space())
.append(
name.as_ref()
.map(|x| allocator.text(x.to_string()))
.unwrap_or_else(|| allocator.nil()),
)
.append(allocator.intersperse(
fields.iter().map(|(name, ty)| {
allocator
.text(
name.as_ref()
.map(|x| x.to_string())
.unwrap_or_else(|| "_".to_string()),
)
.text(name.to_string())
.append(allocator.text(":"))
.append(allocator.space())
.append(