Skip to content

Commit

Permalink
feat: upgrading stdlib (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN authored Sep 4, 2024
1 parent f26c61e commit 4bdfb24
Show file tree
Hide file tree
Showing 48 changed files with 5,143 additions and 3,873 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ await txBuilder
Simply run

```sh
aiken add sidan-lab/vodka --version 0.0.1-beta
aiken add sidan-lab/vodka --version 0.1.0-beta
```

or putting the below in you `aiken.toml`

```toml
[[dependencies]]
name = "sidan-lab/vodka"
version = "0.0.1-beta"
version = "0.1.0-beta"
source = "github"
```

Expand Down
5 changes: 3 additions & 2 deletions aiken.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

[[requirements]]
name = "aiken-lang/stdlib"
version = "1.9.0"
version = "v2"
source = "github"

[[packages]]
name = "aiken-lang/stdlib"
version = "1.9.0"
version = "v2"
requirements = []
source = "github"

[etags]
"aiken-lang/stdlib@v2" = [{ secs_since_epoch = 1725465191, nanos_since_epoch = 577001000 }, "d79382d2b6ecb3aee9b0755c31d8a5bbafe88a7b3706d7fb8a52fd4d05818501"]
8 changes: 6 additions & 2 deletions aiken.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name = "sidan-lab/vodka"
version = "0.0.1-beta"
version = "0.1.0-beta"
compiler = "v1.1.0"
plutus = "v3"
license = "Apache-2.0"
description = "Aiken utils for project 'sidan-lab/vodka"

Expand All @@ -10,5 +12,7 @@ platform = "github"

[[dependencies]]
name = "aiken-lang/stdlib"
version = "1.9.0"
version = "v2"
source = "github"

[config]
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ jobs:
uses: actions/configure-pages@v2

- name: 🧰 Install Aiken
uses: aiken-lang/setup-aiken@v1
with:
version: v1.0.28-alpha
run: cargo install --verbose --git https://github.com/aiken-lang/aiken.git

- name: 📝 Run fmt
run: aiken fmt --check
Expand Down
582 changes: 582 additions & 0 deletions build/packages/aiken-lang-stdlib/CHANGELOG.md

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions build/packages/aiken-lang-stdlib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

aiken's version | stdlib's version(s)
--- | ---
`v1.0.28-alpha` | `1.9.0`
`v1.0.26-alpha` | `1.8.0`
`v1.0.29-alpha` | `>= 1.9.0`
`v1.0.28-alpha` | `>= 1.9.0`
`v1.0.26-alpha` | `<= 1.8.0`

## Overview

Expand Down
2 changes: 1 addition & 1 deletion build/packages/aiken-lang-stdlib/aiken.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "aiken-lang/stdlib"
version = "1.9.0"
version = "main"
licences = ["Apache-2.0"]
description = "The Aiken Standard Library"

Expand Down
215 changes: 20 additions & 195 deletions build/packages/aiken-lang-stdlib/lib/aiken/cbor.ak
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use aiken/builtin.{
append_bytearray, choose_data, cons_bytearray, decode_utf8, index_bytearray,
length_of_bytearray, quotient_integer, remainder_integer, serialise_data,
un_b_data, un_constr_data, un_i_data, un_list_data, un_map_data,
}
use aiken/list
use aiken
use aiken/builtin.{decode_utf8, serialise_data}

/// Serialise any value to binary, encoding using [CBOR](https://www.rfc-editor.org/rfc/rfc8949).
///
Expand All @@ -17,14 +13,14 @@ use aiken/list
/// useful for debugging.
///
/// ```aiken
/// serialise(42) == #"182a"
/// serialise(#"a1b2") == #"42a1b2"
/// serialise([]) == #"80"
/// serialise((1, 2)) == #"9f0102ff"
/// serialise((1, #"ff", 3)) == #"9f0141ff03ff"
/// serialise([(1, #"ff")]) == #"a10141ff"
/// serialise(Some(42)) == #"d8799f182aff"
/// serialise(None) == #"d87a80"
/// cbor.serialise(42) == #"182a"
/// cbor.serialise(#"a1b2") == #"42a1b2"
/// cbor.serialise([]) == #"80"
/// cbor.serialise((1, 2)) == #"9f0102ff"
/// cbor.serialise((1, #"ff", 3)) == #"9f0141ff03ff"
/// cbor.serialise([(1, #"ff")]) == #"a10141ff"
/// cbor.serialise(Some(42)) == #"d8799f182aff"
/// cbor.serialise(None) == #"d87a80"
/// ```
pub fn serialise(self: Data) -> ByteArray {
serialise_data(self)
Expand Down Expand Up @@ -77,192 +73,21 @@ test serialise_9() {
/// a good idea in the Cardano world.
///
/// ```aiken
/// diagnostic(42) == "42"
/// diagnostic(#"a1b2") == "h'A1B2'"
/// diagnostic([1, 2, 3]) == "[_ 1, 2, 3]"
/// diagnostic([]) == "[]"
/// diagnostic((1, 2)) == "[_ 1, 2]"
/// diagnostic((1, #"ff", 3)) == "[_ 1, h'FF', 3]"
/// diagnostic([(1, #"ff")]) == "{_ 1: h'FF' }"
/// diagnostic(Some(42)) == "121([_ 42])"
/// diagnostic(None) == "122([])"
/// cbor.diagnostic(42) == "42"
/// cbor.diagnostic(#"a1b2") == "h'A1B2'"
/// cbor.diagnostic([1, 2, 3]) == "[_ 1, 2, 3]"
/// cbor.diagnostic([]) == "[]"
/// cbor.diagnostic((1, 2)) == "[_ 1, 2]"
/// cbor.diagnostic((1, #"ff", 3)) == "[_ 1, h'FF', 3]"
/// cbor.diagnostic([(1, #"ff")]) == "{_ 1: h'FF' }"
/// cbor.diagnostic(Some(42)) == "121([_ 42])"
/// cbor.diagnostic(None) == "122([])"
/// ```
pub fn diagnostic(self: Data) -> String {
do_diagnostic(self, #"")
aiken.diagnostic(self, #"")
|> decode_utf8
}

/// UTF-8 lookup table. Comes in handy to decipher the code below.
///
/// | Symbol | Decimal | Hex |
/// | --- | --- | --- |
/// | | 32 | 0x20 |
/// | ' | 39 | 0x27 |
/// | ( | 40 | 0x28 |
/// | ) | 41 | 0x29 |
/// | , | 44 | 0x2c |
/// | 0 | 48 | 0x30 |
/// | : | 58 | 0x3a |
/// | A | 65 | 0x41 |
/// | [ | 91 | 0x5b |
/// | ] | 93 | 0x5d |
/// | _ | 95 | 0x5f |
/// | h | 104 | 0x68 |
/// | { | 123 | 0x7b |
/// | } | 125 | 0x7d |
fn do_diagnostic(self: Data, builder: ByteArray) -> ByteArray {
choose_data(
self,
{
// -------- Constr
let Pair(constr, fields) = un_constr_data(self)

// NOTE: This is fundamentally the same logic for serializing list. However, the compiler
// doesn't support mutual recursion just yet, so we can't extract that logic in a separate
// function.
//
// See [aiken-lang/aiken#389](https://github.com/aiken-lang/aiken/pull/389)
let builder =
when fields is {
[] -> append_bytearray(#"5b5d29", builder)
_ -> {
let (_, bytes) =
list.foldr(
fields,
(#"5d", append_bytearray(#"29", builder)),
fn(e: Data, st: (ByteArray, ByteArray)) {
(#"2c20", do_diagnostic(e, append_bytearray(st.1st, st.2nd)))
},
)
append_bytearray(#"5b5f20", bytes)
}
}

let constr_tag =
if constr < 7 {
121 + constr
} else if constr < 128 {
1280 + constr - 7
} else {
fail @"What are you doing? No I mean, seriously."
}

builder
|> append_bytearray(#"28", _)
|> from_int(constr_tag, _)
},
{
// -------- Map

let elems = un_map_data(self)
when elems is {
[] -> append_bytearray(#"7b7d", builder)
_ -> {
let (_, bytes) =
list.foldr(
elems,
(#"207d", builder),
fn(e: Pair<Data, Data>, st: (ByteArray, ByteArray)) {
let value =
do_diagnostic(e.2nd, append_bytearray(st.1st, st.2nd))
(
#"2c20",
do_diagnostic(e.1st, append_bytearray(#"3a20", value)),
)
},
)
append_bytearray(#"7b5f20", bytes)
}
}
},
{
// -------- List

let elems = un_list_data(self)
when elems is {
[] -> append_bytearray(#"5b5d", builder)
_ -> {
let (_, bytes) =
list.foldr(
elems,
(#"5d", builder),
fn(e: Data, st: (ByteArray, ByteArray)) {
(#"2c20", do_diagnostic(e, append_bytearray(st.1st, st.2nd)))
},
)
append_bytearray(#"5b5f20", bytes)
}
}
},
// -------- Integer
self
|> un_i_data
|> from_int(builder),
{
// -------- ByteArray
let bytes = un_b_data(self)

bytes
|> encode_base16(
length_of_bytearray(bytes) - 1,
append_bytearray(#"27", builder),
)
|> append_bytearray(#"6827", _)
},
)
}

fn encode_base16(bytes: ByteArray, ix: Int, builder: ByteArray) -> ByteArray {
if ix < 0 {
builder
} else {
let byte = index_bytearray(bytes, ix)
let msb = byte / 16
let lsb = byte % 16
let builder =
cons_bytearray(
msb + if msb < 10 {
48
} else {
55
},
cons_bytearray(
lsb + if lsb < 10 {
48
} else {
55
},
builder,
),
)
encode_base16(bytes, ix - 1, builder)
}
}

fn from_int(i: Int, digits: ByteArray) -> ByteArray {
if i == 0 {
append_bytearray(#"30", digits)
} else if i < 0 {
append_bytearray(#"2d", from_int(-i, digits))
} else {
do_from_int(
quotient_integer(i, 10),
cons_bytearray(remainder_integer(i, 10) + 48, digits),
)
}
}

fn do_from_int(i: Int, digits: ByteArray) -> ByteArray {
if i <= 0 {
digits
} else {
do_from_int(
quotient_integer(i, 10),
cons_bytearray(remainder_integer(i, 10) + 48, digits),
)
}
}

test diagnostic_1() {
diagnostic(42) == @"42"
}
Expand Down
4 changes: 4 additions & 0 deletions build/packages/aiken-lang-stdlib/lib/aiken/collection.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// An positive integer, that materializes the position of an element in a
/// collection.
pub type Index =
Int
Loading

0 comments on commit 4bdfb24

Please sign in to comment.