Skip to content

Commit

Permalink
Replace the class keyword with type
Browse files Browse the repository at this point in the history
Now that types are no longer guaranteed to go on the heap, the use of
"class" can be confusing as this is typically used for types that go on
the heap. In addition, our types weren't really traditional classes to
begin with due to the lack of support for inheritance.

The `class` keyword is still supported but is now deprecated, and the
`fmt` command automatically replaces it with the new `type` keyword.

This fixes #779.

Changelog: changed
  • Loading branch information
yorickpeterse committed Dec 24, 2024
1 parent 6f8a503 commit aa8bfbd
Show file tree
Hide file tree
Showing 221 changed files with 4,319 additions and 4,298 deletions.
6 changes: 6 additions & 0 deletions ast/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub enum TokenKind {
Trait,
True,
Try,
Type,
Uni,
UnsignedShr,
UnsignedShrAssign,
Expand Down Expand Up @@ -272,6 +273,7 @@ impl TokenKind {
TokenKind::Extern => "the 'extern' keyword",
TokenKind::Inline => "the 'inline' keyword",
TokenKind::Copy => "the 'copy' keyword",
TokenKind::Type => "the 'type' keyword",
}
}
}
Expand Down Expand Up @@ -341,6 +343,7 @@ impl Token {
| TokenKind::Extern
| TokenKind::Inline
| TokenKind::Copy
| TokenKind::Type
)
}

Expand Down Expand Up @@ -989,6 +992,7 @@ impl Lexer {
"case" => TokenKind::Case,
"enum" => TokenKind::Enum,
"copy" => TokenKind::Copy,
"type" => TokenKind::Type,
_ => TokenKind::Identifier,
},
5 => match value.as_str() {
Expand Down Expand Up @@ -1359,6 +1363,7 @@ mod tests {
assert!(tok(TokenKind::Nil, "", 1..=1, 1..=1).is_keyword());
assert!(tok(TokenKind::Inline, "", 1..=1, 1..=1).is_keyword());
assert!(tok(TokenKind::Copy, "", 1..=1, 1..=1).is_keyword());
assert!(tok(TokenKind::Type, "", 1..=1, 1..=1).is_keyword());
}

#[test]
Expand Down Expand Up @@ -1987,6 +1992,7 @@ mod tests {
assert_token!("case", Case, "case", 1..=1, 1..=4);
assert_token!("enum", Enum, "enum", 1..=1, 1..=4);
assert_token!("copy", Copy, "copy", 1..=1, 1..=4);
assert_token!("type", Type, "type", 1..=1, 1..=4);

assert_token!("class", Class, "class", 1..=1, 1..=5);
assert_token!("async", Async, "async", 1..=1, 1..=5);
Expand Down
54 changes: 27 additions & 27 deletions ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,45 +450,45 @@ impl Node for DefineField {
}

#[derive(Debug, PartialEq, Eq)]
pub enum ClassExpression {
pub enum TypeExpression {
DefineMethod(Box<DefineMethod>),
DefineField(Box<DefineField>),
DefineConstructor(Box<DefineConstructor>),
Comment(Box<Comment>),
}

impl Node for ClassExpression {
impl Node for TypeExpression {
fn location(&self) -> &Location {
match self {
ClassExpression::DefineMethod(n) => &n.location,
ClassExpression::DefineField(n) => &n.location,
ClassExpression::DefineConstructor(n) => &n.location,
ClassExpression::Comment(n) => &n.location,
TypeExpression::DefineMethod(n) => &n.location,
TypeExpression::DefineField(n) => &n.location,
TypeExpression::DefineConstructor(n) => &n.location,
TypeExpression::Comment(n) => &n.location,
}
}
}

#[derive(Debug, PartialEq, Eq)]
pub struct ClassExpressions {
pub values: Vec<ClassExpression>,
pub struct TypeExpressions {
pub values: Vec<TypeExpression>,
pub location: Location,
}

impl Node for ClassExpressions {
impl Node for TypeExpressions {
fn location(&self) -> &Location {
&self.location
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum ClassSemantics {
pub enum TypeSemantics {
Default,
Inline,
Copy,
}

#[derive(Debug, PartialEq, Eq)]
pub enum ClassKind {
pub enum TypeKind {
Async,
Builtin,
Enum,
Expand All @@ -497,17 +497,17 @@ pub enum ClassKind {
}

#[derive(Debug, PartialEq, Eq)]
pub struct DefineClass {
pub semantics: ClassSemantics,
pub struct DefineType {
pub semantics: TypeSemantics,
pub public: bool,
pub kind: ClassKind,
pub kind: TypeKind,
pub name: Constant,
pub type_parameters: Option<TypeParameters>,
pub body: ClassExpressions,
pub body: TypeExpressions,
pub location: Location,
}

impl Node for DefineClass {
impl Node for DefineType {
fn location(&self) -> &Location {
&self.location
}
Expand Down Expand Up @@ -574,9 +574,9 @@ impl Node for DefineTrait {
pub enum TopLevelExpression {
DefineConstant(Box<DefineConstant>),
DefineMethod(Box<DefineMethod>),
DefineClass(Box<DefineClass>),
DefineType(Box<DefineType>),
DefineTrait(Box<DefineTrait>),
ReopenClass(Box<ReopenClass>),
ReopenType(Box<ReopenType>),
ImplementTrait(Box<ImplementTrait>),
Import(Box<Import>),
ExternImport(Box<ExternImport>),
Expand All @@ -588,9 +588,9 @@ impl Node for TopLevelExpression {
match self {
TopLevelExpression::DefineConstant(ref n) => n.location(),
TopLevelExpression::DefineMethod(ref n) => n.location(),
TopLevelExpression::DefineClass(ref n) => n.location(),
TopLevelExpression::DefineType(ref n) => n.location(),
TopLevelExpression::DefineTrait(ref n) => n.location(),
TopLevelExpression::ReopenClass(ref n) => n.location(),
TopLevelExpression::ReopenType(ref n) => n.location(),
TopLevelExpression::ImplementTrait(ref n) => n.location(),
TopLevelExpression::Import(ref n) => n.location(),
TopLevelExpression::ExternImport(ref n) => n.location(),
Expand Down Expand Up @@ -627,14 +627,14 @@ impl Node for ImplementationExpressions {
}

#[derive(Debug, PartialEq, Eq)]
pub struct ReopenClass {
pub class_name: Constant,
pub struct ReopenType {
pub type_name: Constant,
pub body: ImplementationExpressions,
pub location: Location,
pub bounds: Option<TypeBounds>,
}

impl Node for ReopenClass {
impl Node for ReopenType {
fn location(&self) -> &Location {
&self.location
}
Expand Down Expand Up @@ -698,7 +698,7 @@ impl Node for TypeBounds {
#[derive(Debug, PartialEq, Eq)]
pub struct ImplementTrait {
pub trait_name: TypeName,
pub class_name: Constant,
pub type_name: Constant,
pub body: ImplementationExpressions,
pub location: Location,
pub bounds: Option<TypeBounds>,
Expand Down Expand Up @@ -1474,7 +1474,7 @@ pub struct FieldPattern {
}

#[derive(Debug, PartialEq, Eq)]
pub struct ClassPattern {
pub struct TypePattern {
pub values: Vec<FieldPattern>,
pub location: Location,
}
Expand All @@ -1495,7 +1495,7 @@ pub struct StringPattern {
pub enum Pattern {
Constant(Box<Constant>),
Constructor(Box<ConstructorPattern>),
Class(Box<ClassPattern>),
Type(Box<TypePattern>),
Int(Box<IntLiteral>),
True(Box<True>),
False(Box<False>),
Expand All @@ -1511,7 +1511,7 @@ impl Pattern {
match self {
Pattern::Constant(ref n) => &n.location,
Pattern::Constructor(ref n) => &n.location,
Pattern::Class(ref n) => &n.location,
Pattern::Type(ref n) => &n.location,
Pattern::Int(ref n) => n.location(),
Pattern::True(ref n) => n.location(),
Pattern::False(ref n) => n.location(),
Expand Down
Loading

0 comments on commit aa8bfbd

Please sign in to comment.