diff --git a/src/preserves.nim b/src/preserves.nim index 9b51fd0..8136e2b 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[options, sets, sequtils, strutils, tables, typetraits] +import std/[assertions, options, sets, sequtils, strutils, tables, typetraits] from std/algorithm import sort from std/json import escapeJson, escapeJsonUnquoted import bigints diff --git a/src/preserves/private/buffering.nim b/src/preserves/private/buffering.nim index 3a5dff8..a24c0c3 100644 --- a/src/preserves/private/buffering.nim +++ b/src/preserves/private/buffering.nim @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[endians, options, streams, strutils] +import std/[assertions, endians, options, streams, strutils] import bigints import ./decoding, ./parsing, ./values diff --git a/src/preserves/private/encoding.nim b/src/preserves/private/encoding.nim index 872c2c0..c26157f 100644 --- a/src/preserves/private/encoding.nim +++ b/src/preserves/private/encoding.nim @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[endians, streams] +import std/[assertions, endians, streams] import bigints import ./values diff --git a/src/preserves/private/macros.nim b/src/preserves/private/macros.nim index 60c3601..1b42ad8 100644 --- a/src/preserves/private/macros.nim +++ b/src/preserves/private/macros.nim @@ -7,7 +7,7 @@ # distribution, for details about the copyright. # -import std/macros +import std/[assertions, macros] const nnkPragmaCallKinds = {nnkExprColonExpr, nnkCall, nnkCallStrLit} diff --git a/src/preserves/private/parsing.nim b/src/preserves/private/parsing.nim index d6a70ad..4d9500e 100644 --- a/src/preserves/private/parsing.nim +++ b/src/preserves/private/parsing.nim @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[base64, options, parseutils, strutils, unicode] +import std/[assertions, base64, options, parseutils, strutils, unicode] from std/sequtils import insert import bigints, npeg diff --git a/src/preserves/private/texts.nim b/src/preserves/private/texts.nim index e612b9e..fdb8020 100644 --- a/src/preserves/private/texts.nim +++ b/src/preserves/private/texts.nim @@ -1,7 +1,9 @@ # SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[base64, endians, math, sequtils, streams, strutils] +import std/[assertions, base64, endians, sequtils, streams, strutils] +when not defined(nimNoLibc): + import std/math import bigints import ./values @@ -42,6 +44,15 @@ proc writeSymbol(stream: Stream; sym: string) = writeEscaped(stream, sym, '|') write(stream, '|') +proc writeFloatBytes(stream: Stream; f: float) = + var buf: array[8, byte] + bigEndian64(addr buf[0], addr f) + write(stream, "#xd\"") + for b in buf: + write(stream, hexAlphabet[b shr 4]) + write(stream, hexAlphabet[b and 0xf]) + write(stream, '"') + proc writeText*(stream: Stream; pr: Value; mode = textPreserves) = ## Encode Preserves to a `Stream` as text. if pr.embedded: write(stream, "#:") @@ -51,17 +62,14 @@ proc writeText*(stream: Stream; pr: Value; mode = textPreserves) = of false: write(stream, "#f") of true: write(stream, "#t") of pkFloat: - case pr.float.classify: - of fcNormal, fcZero, fcNegZero: - write(stream, $pr.float) + when defined(nimNoLibc): + writeFloatBytes(stream, pr.float) + # IEE754-to-decimal is non-trivial else: - var buf: array[8, byte] - bigEndian64(addr buf[0], addr pr.float) - write(stream, "#xd\"") - for b in buf: - write(stream, hexAlphabet[b shr 4]) - write(stream, hexAlphabet[b and 0xf]) - write(stream, '"') + if pr.float.classify in {fcNormal, fcZero, fcNegZero}: + write(stream, $pr.float) + else: + writeFloatBytes(stream, pr.float) of pkRegister: write(stream, $pr.register) of pkBigInt: diff --git a/src/preserves/private/values.nim b/src/preserves/private/values.nim index 87feeb2..0b88815 100644 --- a/src/preserves/private/values.nim +++ b/src/preserves/private/values.nim @@ -1,8 +1,9 @@ # SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[algorithm, hashes, math, options, sets, sequtils, tables] - +import std/[algorithm, hashes, options, sets, sequtils, tables] +when not defined(nimNoLibc): + import std/math import bigints type @@ -80,11 +81,6 @@ type ## Object refs embedded in Preserves `Value`s must inherit from `EmbeddedObj`. ## At the moment this is just an alias to `RootObj` but this may change in the future. -func `===`[T: SomeFloat](a, b: T): bool = - ## Compare where Nan == NaN. - let class = a.classify - (class == b.classify) and ((class notin {fcNormal,fcSubnormal}) or (a == b)) - func `==`*(x, y: Value): bool = ## Check `x` and `y` for equivalence. if x.kind == y.kind and x.embedded == y.embedded: @@ -92,7 +88,7 @@ func `==`*(x, y: Value): bool = of pkBoolean: result = x.bool == y.bool of pkFloat: - result = x.float === y.float + result = cast[uint64](x.float) == cast[uint64](y.float) of pkRegister: result = x.register == y.register of pkBigInt: