mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-19 10:09:47 +00:00
add derive copy and cleanup naming
This commit is contained in:
parent
fb657f101d
commit
48ce687aa6
2 changed files with 36 additions and 9 deletions
|
@ -77,11 +77,11 @@ RocType : [
|
||||||
EmptyTagUnion,
|
EmptyTagUnion,
|
||||||
Struct {
|
Struct {
|
||||||
name: Str,
|
name: Str,
|
||||||
fields: List { name: Str, type: TypeId }
|
fields: List { name: Str, id: TypeId }
|
||||||
},
|
},
|
||||||
TagUnionPayload {
|
TagUnionPayload {
|
||||||
name: Str,
|
name: Str,
|
||||||
fields: List { discriminant: Nat, type: TypeId },
|
fields: List { discriminant: Nat, id: TypeId },
|
||||||
},
|
},
|
||||||
## A recursive pointer, e.g. in StrConsList : [Nil, Cons Str StrConsList],
|
## A recursive pointer, e.g. in StrConsList : [Nil, Cons Str StrConsList],
|
||||||
## this would be the field of Cons containing the (recursive) StrConsList type,
|
## this would be the field of Cons containing the (recursive) StrConsList type,
|
||||||
|
|
|
@ -82,14 +82,14 @@ generateStruct = \buf, types, id, name, fields, visibility ->
|
||||||
structType = getType types id
|
structType = getType types id
|
||||||
|
|
||||||
buf
|
buf
|
||||||
|> addDeriveStr structType types IncludeDebug
|
|> addDeriveStr types structType IncludeDebug
|
||||||
|> Str.concat "#[repr(\(repr))]\n\(pub) struct \(escapedName) {\n"
|
|> Str.concat "#[repr(\(repr))]\n\(pub) struct \(escapedName) {\n"
|
||||||
|> \b -> List.walk fields b (generateStructFields types)
|
|> \b -> List.walk fields b (generateStructFields types)
|
||||||
|> Str.concat "}\n\n"
|
|> Str.concat "}\n\n"
|
||||||
|
|
||||||
generateStructFields = \types ->
|
generateStructFields = \types ->
|
||||||
\accum, { name: fieldName, type: fieldType } ->
|
\accum, { name: fieldName, id } ->
|
||||||
typeStr = typeName types fieldType
|
typeStr = typeName types id
|
||||||
escapedFieldName = escapeKW fieldName
|
escapedFieldName = escapeKW fieldName
|
||||||
|
|
||||||
Str.concat accum "\(indent)pub \(escapedFieldName): \(typeStr),\n"
|
Str.concat accum "\(indent)pub \(escapedFieldName): \(typeStr),\n"
|
||||||
|
@ -97,12 +97,13 @@ generateStructFields = \types ->
|
||||||
nameTagUnionPayloadFields = \fields ->
|
nameTagUnionPayloadFields = \fields ->
|
||||||
# Tag union payloads have numbered fields, so we prefix them
|
# Tag union payloads have numbered fields, so we prefix them
|
||||||
# with an "f" because Rust doesn't allow struct fields to be numbers.
|
# with an "f" because Rust doesn't allow struct fields to be numbers.
|
||||||
List.map fields \{ discriminant, type } ->
|
List.map fields \{ discriminant, id } ->
|
||||||
discStr = Num.toStr discriminant
|
discStr = Num.toStr discriminant
|
||||||
|
|
||||||
{ name: "f\(discStr)", type }
|
{ name: "f\(discStr)", id }
|
||||||
|
|
||||||
addDeriveStr = \buf, _type, _types, includeDebug ->
|
addDeriveStr = \buf, types, type, includeDebug ->
|
||||||
|
# TODO: full derive impl porting.
|
||||||
buf
|
buf
|
||||||
|> Str.concat "#[derive(Clone, "
|
|> Str.concat "#[derive(Clone, "
|
||||||
|> \b ->
|
|> \b ->
|
||||||
|
@ -111,9 +112,35 @@ addDeriveStr = \buf, _type, _types, includeDebug ->
|
||||||
Str.concat b "Debug, "
|
Str.concat b "Debug, "
|
||||||
|
|
||||||
ExcludeDebug ->
|
ExcludeDebug ->
|
||||||
b # TODO: full derive impl porting.
|
b
|
||||||
|
|> \b ->
|
||||||
|
if !(cannotDeriveCopy types type) then
|
||||||
|
Str.concat b "Copy, "
|
||||||
|
else
|
||||||
|
b
|
||||||
|> Str.concat "PartialEq, PartialOrd)]\n"
|
|> Str.concat "PartialEq, PartialOrd)]\n"
|
||||||
|
|
||||||
|
cannotDeriveCopy = \types, type ->
|
||||||
|
when type is
|
||||||
|
Unit | EmptyTagUnion | Bool | Num _ | TagUnion (Enumeration _) | Function _ -> Bool.false
|
||||||
|
RocStr | RocList _ | RocDict _ _ | RocSet _ | RocBox _ | TagUnion ( NullableUnwrapped _ ) | TagUnion ( NullableWrapped _ ) | TagUnion ( Recursive _ ) | TagUnion ( NonNullableUnwrapped _) | RecursivePointer _ -> Bool.true
|
||||||
|
TagUnion (SingleTagStruct { payloadFields }) ->
|
||||||
|
List.any payloadFields \id -> cannotDeriveCopy types (getType types id)
|
||||||
|
TagUnion (NonRecursive {tags}) ->
|
||||||
|
List.any tags \{payload} ->
|
||||||
|
when payload is
|
||||||
|
Some id -> cannotDeriveCopy types (getType types id)
|
||||||
|
None -> Bool.false
|
||||||
|
RocResult okId errId ->
|
||||||
|
cannotDeriveCopy types (getType types okId)
|
||||||
|
|| cannotDeriveCopy types (getType types errId)
|
||||||
|
Struct { fields} ->
|
||||||
|
List.any fields \{ id } -> cannotDeriveCopy types (getType types id)
|
||||||
|
TagUnionPayload { fields} ->
|
||||||
|
List.any fields \{ id } -> cannotDeriveCopy types (getType types id)
|
||||||
|
_ -> crash "ugh"
|
||||||
|
|
||||||
|
|
||||||
typeName = \types, id ->
|
typeName = \types, id ->
|
||||||
when getType types id is
|
when getType types id is
|
||||||
Unit -> "()"
|
Unit -> "()"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue