Skip to content

Commit 1ffdb5d

Browse files
committed
Refactor and enhance type system in Morphir Moonbit
- Updated Documented struct and its methods for improved readability. - Introduced FQName and FQNameId structs with associated functions. - Enhanced ModulePath with new methods for string conversion and list handling. - Added codec implementations for FQName and PackageName. - Created new NameId struct for better name handling. - Implemented TypeSpecification and DerivedTypeSpecificationDetails for type definitions. - Expanded Type enum to include Reference, Record, and ExtensibleRecord types. - Added utility functions for iterating over types and converting types to strings. - Introduced tests for various type functionalities including iterators and string conversions. - Added helper structs Length and Index for better type management.
1 parent 252fec5 commit 1ffdb5d

28 files changed

+689
-52
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
///|
12
pub struct Documented[T] {
2-
doc: String
3-
value: T
3+
doc : String
4+
value : T
45
}
56

6-
pub fn[T] Documented::new(doc: String, value: T) -> Documented[T] {
7-
{ doc, value }
7+
///|
8+
pub fn[T] Documented::new(doc : String, value : T) -> Documented[T] {
9+
{ doc, value }
810
}
911

10-
pub fn[T,U] Documented::map(
11-
self: Documented[T],
12-
f: (T) -> U,
12+
///|
13+
pub fn[T, U] Documented::map(
14+
self : Documented[T],
15+
f : (T) -> U,
1316
) -> Documented[U] {
14-
Documented::new(self.doc, f(self.value))
15-
}
17+
Documented::new(self.doc, f(self.value))
18+
}
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
pub impl[T: @codec.ToMorphirJson] @codec.ToMorphirJson for Documented[T] with to_json(
2-
self: Documented[T],
3-
options: @codec.MorphirJsonOptions,
1+
///|
2+
pub impl[T : @codec.ToMorphirJson] @codec.ToMorphirJson for Documented[T] with to_json(
3+
self : Documented[T],
4+
options : @codec.MorphirJsonOptions,
45
) -> Json {
5-
Json::object({
6-
"doc": Json::string(self.doc),
7-
"value": self.value.to_json(options),
8-
})
6+
Json::object({
7+
"doc": Json::string(self.doc),
8+
"value": self.value.to_json(options),
9+
})
910
}
1011

11-
pub impl[T: @codec.FromMorphirJson] @codec.FromMorphirJson for Documented[T] with from_json(
12-
json: Json,
13-
json_path: @json.JsonPath,
14-
options: @codec.MorphirJsonOptions,
12+
///|
13+
pub impl[T : @codec.FromMorphirJson] @codec.FromMorphirJson for Documented[T] with from_json(
14+
json : Json,
15+
json_path : @json.JsonPath,
16+
options : @codec.MorphirJsonOptions,
1517
) -> Documented[T] {
1618
guard json is Json::Object(obj) else {
1719
@codec.decode_error(
18-
json_path,
19-
"Documented::from_json: expected object with 'doc' and 'value' fields",
20+
json_path, "Documented::from_json: expected object with 'doc' and 'value' fields",
2021
)
2122
}
2223
match obj {
2324
{ "doc": Json::String(doc), "value": valueJson, .. } => {
24-
let value =
25-
@codec.FromMorphirJson::from_json(valueJson, json_path.add_key("value"), options);
25+
let value = @codec.FromMorphirJson::from_json(
26+
valueJson,
27+
json_path.add_key("value"),
28+
options,
29+
)
2630
Documented::new(doc, value)
2731
}
28-
_ => {
32+
_ =>
2933
@codec.decode_error(
30-
json_path,
31-
"Documented::from_json: expected object with 'doc' and 'value' fields",
34+
json_path, "Documented::from_json: expected object with 'doc' and 'value' fields",
3235
)
33-
}
3436
}
35-
}
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
///|
2+
pub fn fq_name(
3+
package_name : @package.PackageName,
4+
module_path : @module.ModulePath,
5+
local_name : @name.Name,
6+
) -> FQName {
7+
{ package_name, module_path, local_name }
8+
}
9+
10+
pub struct FQNameId(Int) derive(Eq, Hash, Show)
11+
12+
pub fn fq_name_id(id : Int) -> FQNameId {
13+
FQNameId(id)
14+
}
15+
pub fn FQNameId::from_int(id : Int) -> FQNameId {
16+
FQNameId(id)
17+
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
pub struct FQName(@package.PackageName, @module.ModulePath, @name.Name) derive(Eq, Hash, Show)
2-
3-
pub fn fq_name(package_name:@package.PackageName, module_path: @module.ModulePath, local_name: @name.Name) -> FQName {
4-
FQName(package_name, module_path, local_name)
5-
}
1+
///|
2+
pub struct FQName{
3+
package_name: @package.PackageName
4+
module_path: @module.ModulePath
5+
local_name: @name.Name
6+
} derive (
7+
Eq,
8+
Hash,
9+
Show,
10+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
///|
2+
pub impl @codec.ToMorphirJson for FQName with to_json(
3+
self : FQName,
4+
options : @codec.MorphirJsonOptions,
5+
) -> Json {
6+
Json::array([
7+
@codec.ToMorphirJson::to_json(self.package_name, options),
8+
@codec.ToMorphirJson::to_json(self.module_path, options),
9+
@codec.ToMorphirJson::to_json(self.local_name, options),
10+
])
11+
}

bindings/moonbit/morphir-moonbit/lib/ir/fqname/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"import": [
3+
"Morphir/morphir/lib/ir/codec",
34
"Morphir/morphir/lib/ir/name",
45
"Morphir/morphir/lib/ir/module",
56
"Morphir/morphir/lib/ir/package"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
///|
2+
pub impl @codec.ToMorphirJson for ModulePath with to_json(
3+
self : ModulePath,
4+
options : @codec.MorphirJsonOptions,
5+
) -> Json {
6+
@codec.ToMorphirJson::to_json(self.0, options)
7+
}

bindings/moonbit/morphir-moonbit/lib/ir/module/module_path.mbt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@ pub fn ModulePath::from_list(parts : @list.List[@name.Name]) -> ModulePath {
66
ModulePath(parts)
77
}
88

9+
///|
10+
pub fn ModulePath::from_string(name : String) -> ModulePath {
11+
let path = @path.from_string(name)
12+
ModulePath(path.to_list())
13+
}
14+
915
///|
1016
pub fn ModulePath::to_list(self : ModulePath) -> @list.List[@name.Name] {
1117
self.0
1218
}
1319

20+
pub fn ModulePath::to_string(
21+
self : ModulePath,
22+
renderer : @name.NameRenderer,
23+
separator : String,
24+
) -> String {
25+
self.0.iter().map(renderer).join(separator)
26+
}
27+
1428
///|
1529
pub fn ModulePath::to_path(self : ModulePath) -> @path.Path {
1630
@path.Path::from_list(self.0)

bindings/moonbit/morphir-moonbit/lib/ir/module/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"import": [
3+
"Morphir/morphir/lib/ir/codec",
34
"Morphir/morphir/lib/ir/name",
45
"Morphir/morphir/lib/ir/path"
56
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub struct NameId(Int) derive(Eq, Hash, Show)
2+
3+
pub fn name_id(id : Int) -> NameId {
4+
NameId(id)
5+
}
6+
7+
pub fn NameId::from_int(id : Int) -> NameId {
8+
NameId(id)
9+
}

0 commit comments

Comments
 (0)