Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ jobs:
run: if [ -f scripts/test ]; then nix develop -c bash ./scripts/test; fi

- name: Luacheck
run: nix develop -c luacheck --quiet --std lua51 --no-unused-args src/
run: nix develop -c luacheck --quiet --std lua51 --no-unused-args --max-line-length 130 src/

- name: Format check
run: nix fmt && git diff --exit-code
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.*
!/.gitignore
!/.github/
!/.tidyrc.json
!/.lua-format
/output/
10 changes: 10 additions & 0 deletions .lua-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# LuaFormatter config for the hand-written FFI under src/.
# 2-space indent. Keep simple functions on one line; column_limit sits a few
# columns under luacheck's 130 limit because lua-format under-counts the leading
# indent and trailing comma, so this keeps every emitted line within 130.
indent_width: 2
use_tab: false
column_limit: 126
continuation_indent_width: 2
keep_simple_function_one_line: true
keep_simple_control_block_one_line: true
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "source",
"width": 80
}
16 changes: 13 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ A PureScript→Lua FFI fork in the [`purescript-lua`](https://github.com/purescr

## Commands

All commands run inside the nix dev shell:

- Build: `nix develop -c ./scripts/build`
- Test (only if the fork has `scripts/test`): `nix develop -c bash ./scripts/test`
- Lint: `nix develop -c luacheck --quiet --std lua51 --no-unused-args src/`
- Lint: `nix develop -c luacheck --quiet --std lua51 --no-unused-args --max-line-length 130 src/`
- Format: `nix fmt` (check: `nix fmt && git diff --exit-code`)

## Formatting

`nix fmt` runs treefmt (`treefmt.nix`): nixfmt for Nix, `dhall format`, purs-tidy
for `*.purs` (config in `.tidyrc.json`), and LuaFormatter for the `*.lua` FFI
(config in `.lua-format`). LuaFormatter is used over StyLua because it keeps the
parentheses pslua's foreign-file parser requires. The Lua line budget is 130
columns, matching the `luacheck --max-line-length` above. The check is
content-based (`nix fmt && git diff --exit-code`) rather than `treefmt --ci`,
since the in-place formatters bump mtime even when content is unchanged, which
trips treefmt's `--fail-on-change`. CI and the pre-commit hook use it.

## Lua 5.1 target

Expand Down
23 changes: 22 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 39 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,33 @@
inputs.nixpkgs.follows = "nixpkgs";
};
pslua.url = "github:purescript-lua/purescript-lua";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, purescript-overlay, pslua }:
flake-utils.lib.eachDefaultSystem (system:
outputs =
{
self,
nixpkgs,
flake-utils,
purescript-overlay,
pslua,
treefmt-nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ purescript-overlay.overlays.default ];
};
in {
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
in
{
formatter = treefmtEval.config.build.wrapper;
checks.formatting = treefmtEval.config.build.check self;
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
dhall
Expand All @@ -31,8 +48,26 @@
spago-bin.spago-0_21_0
treefmt
];
# Install a content-based pre-commit hook. It compares the working
# tree diff before and after `nix fmt`, so it only objects to changes
# the formatter itself introduces (not the developer's existing
# unstaged work) and is not fooled by formatters that only bump mtime.
# Rewritten each shell entry to stay in sync with this flake.
shellHook = ''
hook=.git/hooks/pre-commit
if [ -d .git ]; then
printf '%s\n' \
Comment thread
Unisay marked this conversation as resolved.
'#!/usr/bin/env bash' \
'before=$(git diff)' \
'nix fmt >/dev/null 2>&1 || exit 0' \
Comment thread
Unisay marked this conversation as resolved.
'[ "$before" = "$(git diff)" ] || { echo "nix fmt changed files; re-stage them, then commit." >&2; exit 1; }' \
> "$hook"
chmod +x "$hook"
fi
'';
};
});
}
);

# --- Flake Local Nix Configuration ----------------------------
nixConfig = {
Expand Down
2 changes: 1 addition & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ name = "purescript-lua-integers"
, dependencies = [ "maybe" , "numbers" , "prelude" ]
, dependencies = [ "maybe", "numbers", "prelude" ]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs" ]
}
5 changes: 3 additions & 2 deletions src/Data/Int.purs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ base36 = Radix 36

-- | Create a `Radix` from a number between 2 and 36.
radix :: Int -> Maybe Radix
radix n | n >= 2 && n <= 36 = Just (Radix n)
| otherwise = Nothing
radix n
| n >= 2 && n <= 36 = Just (Radix n)
| otherwise = Nothing

-- | Like `fromString`, but the integer can be specified in a different base.
-- |
Expand Down
9 changes: 6 additions & 3 deletions src/Data/Int/Bits.purs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
-- | This module defines bitwise operations for the `Int` type.
module Data.Int.Bits
( and, (.&.)
, or, (.|.)
, xor, (.^.)
( and
, (.&.)
, or
, (.|.)
, xor
, (.^.)
, shl
, shr
, zshr
Expand Down
33 changes: 18 additions & 15 deletions test/Test/Data/Int.purs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@ testInt = do
assert $ floor 0.7 == 0

log "round, ceil, and floor should clamp values outside the Int range"
let testClamping f = do
let low = toNumber bottom - 1.5
assert $ f low == bottom
let
testClamping f = do
let low = toNumber bottom - 1.5
assert $ f low == bottom

let high = toNumber top + 1.5
assert $ f high == top
let high = toNumber top + 1.5
assert $ f high == top

testClamping round
testClamping ceil
testClamping floor


log "round, ceil, and floor should return 0 for NaN and Infinities"
let testNonNumber f = do
assert $ f nan == 0
assert $ f infinity == 0
assert $ f (-infinity) == 0
let
testNonNumber f = do
assert $ f nan == 0
assert $ f infinity == 0
assert $ f (-infinity) == 0

testNonNumber round
testNonNumber ceil
Expand Down Expand Up @@ -135,9 +136,10 @@ testInt = do

log "parity is a ring homomorphism"
do
let go x y = do
assert $ parity x + parity y == parity (x + y)
assert $ parity x * parity y == parity (x * y)
let
go x y = do
assert $ parity x + parity y == parity (x + y)
assert $ parity x * parity y == parity (x * y)

go 0 0
go 0 1
Expand All @@ -155,8 +157,9 @@ testInt = do
let
q = quot a b
r = rem a b
in do
assert $ q * b + r == a
in
do
assert $ q * b + r == a
-- Check when dividend goes into divisor exactly
go 8 2
go (-8) 2
Expand Down
43 changes: 43 additions & 0 deletions treefmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ pkgs, ... }:
{
projectRootFile = "flake.nix";

# Nix — RFC 166 formatter.
programs.nixfmt.enable = true;

# Dhall — spago.dhall / packages.dhall layout.
programs.dhall.enable = true;

# PureScript — purs-tidy is not a first-class treefmt program, so wire it via
# the generic mechanism. It picks up `.tidyrc.json` from the project root.
settings.formatter.purs-tidy = {
command = "${pkgs.purs-tidy}/bin/purs-tidy";
options = [ "format-in-place" ];
includes = [ "*.purs" ];
Comment thread
Unisay marked this conversation as resolved.
};

# Lua FFI — LuaFormatter keeps the parentheses pslua's foreign-file parser
# requires (unlike StyLua, which strips them). Config in `.lua-format`.
settings.formatter.lua-format = {
command = "${pkgs.luaformatter}/bin/lua-format";
options = [
"-i"
"-c"
".lua-format"
];
includes = [ "*.lua" ];
Comment thread
Unisay marked this conversation as resolved.
};

# Never format generated output or vendored trees.
settings.global.excludes = [
"dist/*"
"output/*"
".spago/*"
"node_modules/*"
"*.lock"
"flake.lock"
"spago.lock"
".tidyrc.json"
".lua-format"
];
}
Loading