From 4fb13af49f0ca8a99b486ce41e73087754121e74 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 13 Sep 2024 18:31:53 +0200 Subject: [PATCH] Fix type reification of PRNG. --- CHANGELOG.md | 7 ++++++- crates/aiken-lang/src/ast.rs | 21 +++++++++++++++++++-- crates/aiken-lang/src/builtins.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01fa5120..cd60dc66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## v1.1.2 - 2024-09-DD +## v1.1.2 - UNRELEASED ### Added @@ -10,8 +10,13 @@ - **aiken-lang**: Fix issues with static recursive optimization. See [#1009](https://github.com/aiken-lang/aiken/issues/1009) @Microproofs - **aiken-lang**: Aiken IR now interns variables while building up to ensure uniqueness for local vars. @Microproofs +- **aiken-lang**: Fix reification of `Data` (failing to reify) & `PRNG` (missing variants' arguments). @KtorZ +- **aiken-lang**: Adjust reification of `String` to be shown as plain UTF-8 text strings (instead of hex-encoded byte array). @KtorZ +### Removed + +- N/A ## v1.1.1 - 2024-09-10 diff --git a/crates/aiken-lang/src/ast.rs b/crates/aiken-lang/src/ast.rs index 02675c0e..b74f4d04 100644 --- a/crates/aiken-lang/src/ast.rs +++ b/crates/aiken-lang/src/ast.rs @@ -420,9 +420,13 @@ pub type TypedDataType = DataType>; impl TypedDataType { pub fn known_enum(name: &str, constructors: &[&str]) -> Self { + Self::known_data_type(name, &RecordConstructor::known_enum(constructors)) + } + + pub fn known_data_type(name: &str, constructors: &[RecordConstructor>]) -> Self { Self { name: name.to_string(), - constructors: RecordConstructor::known_enum(constructors), + constructors: constructors.to_vec(), location: Span::empty(), opaque: false, public: true, @@ -946,7 +950,10 @@ pub struct RecordConstructor { pub sugar: bool, } -impl RecordConstructor { +impl RecordConstructor +where + A: Clone, +{ pub fn put_doc(&mut self, new_doc: String) { self.doc = Some(new_doc); } @@ -963,6 +970,16 @@ impl RecordConstructor { }) .collect() } + + pub fn known_record(name: &str, args: &[RecordConstructorArg]) -> Self { + RecordConstructor { + location: Span::empty(), + name: name.to_string(), + arguments: args.to_vec(), + doc: None, + sugar: false, + } + } } #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] diff --git a/crates/aiken-lang/src/builtins.rs b/crates/aiken-lang/src/builtins.rs index 592b41be..dccffdc3 100644 --- a/crates/aiken-lang/src/builtins.rs +++ b/crates/aiken-lang/src/builtins.rs @@ -1588,7 +1588,35 @@ impl TypedDataType { } pub fn prng() -> Self { - DataType::known_enum(well_known::PRNG, well_known::PRNG_CONSTRUCTORS) + let bytearray_arg = |label: &str| RecordConstructorArg { + label: Some(label.to_string()), + doc: None, + annotation: Annotation::bytearray(Span::empty()), + location: Span::empty(), + tipo: Type::byte_array(), + }; + + let int_arg = |label: &str| RecordConstructorArg { + label: Some(label.to_string()), + doc: None, + annotation: Annotation::int(Span::empty()), + location: Span::empty(), + tipo: Type::int(), + }; + + DataType::known_data_type( + well_known::PRNG, + &[ + RecordConstructor::known_record( + well_known::PRNG_CONSTRUCTORS[0], + &[bytearray_arg("seed"), bytearray_arg("choices")], + ), + RecordConstructor::known_record( + well_known::PRNG_CONSTRUCTORS[1], + &[int_arg("cursor"), bytearray_arg("choices")], + ), + ], + ) } pub fn ordering() -> Self {