8000 GitHub - cladam/koka-labs: My explorations of the koka language. · GitHub
[go: up one dir, main page]

Skip to content
cladamPublic

Latest commit

 

History

140 Commits

Folders and files

Repository files navigation

koka-labs

Exploring the Koka language by rebuilding GNU ls.

The goal is to follow the GNU coreutils philosophy, reimplemented piece by piece in Koka's effect-typed, functional style.

Building

git clone https://github.com/cladam/koka-labs.git
cd koka-labs

Requirements

  • macOS / Linux POSIX
  • coreutils installed
  • Koka the language installed: curl -sSL https://github.com/koka-lang/koka/releases/latest/download/install.sh | sudo sh

Running

From source

# use the bash script to run kls
./exec-ls ls              # build and run ls (debug)
./exec-ls ls /tmp         # with arguments
./exec-ls ls -ar /tmp     # with flags

###

# Build and produce a binary
koka src/ls.kk -o kls
chmod +x kls

Note: Optimised builds (koka -O1/-O2) currently segfault due to a Koka compiler bug in kk_cctx_copy when stacking effect handlers. Use debug builds for now.

From built binary

./kls
# See implemented flags with --help

macOS: downloaded binary

macOS Gatekeeper quarantines binaries downloaded from the internet. After downloading a release, run:

xattr -d com.apple.quarantine koka-ls

Testing

I am testing kls with both integration and unit tests, see tests/test-ls.kk. Integration tests use the bio effect to capture list-dir output into a string buffer instead of printing to stdout, making output assertions straightforward.

Run all tests with the helper script:

./test-ls

The testing framework is kunit, managed as a git submodule under lib/kunit/.


Programs

Program Description
ls.kk List directory contents

ls — Progress

Phase 0 - Infrastructure

  • Create a testing framework based on XUnit
  • Create a CI workflow and publish binaries

Phase 1 — Foundation

  • Default directory listing (. when no args)
  • Hide dotfiles by default
  • Alphabetical sort (default)
  • Show basenames, not full paths
  • Combined short flags (-ar, -ra)
  • -- stops flag parsing
  • Multiple path arguments — ls dir1 dir2
  • Distinguish files vs directories in arguments
  • Error handling (ls: cannot access ...)
  • Exit codes (0 success, 1 minor problems, 2 serious trouble)

Phase 2 — Which files are listed

GNU §10.1.1

  • -a, --all — in directories, do not ignore names starting with .
  • -A, --almost-all — like -a, but ignore . and ..
  • -B, --ignore-backups — ignore entries ending with ~
  • -d, --directory — list directories themselves, not their contents
  • -H, --dereference-command-line — follow symlinks listed on the command line
  • --dereference-command-line-symlink-to-dir — follow command-line symlinks that point to directories
  • --hide=PATTERN — hide matching entries (overridden by -a/-A)
  • -I, --ignore=PATTERN — do not list entries matching pattern
  • -L, --dereference — show information for link target, not the link itself
  • -R, --recursive — list subdirectories recursively

Phase 3 — What information is listed

GNU §10.1.2 · GNU §10.1.5

  • -l, --format=long — long format (type, mode, links, owner, group, size, timestamp, name)
  • -g — long format, omit owner
  • -o — long format, omit group (equivalent to -l -G)
  • -G, --no-group — suppress group in long listing
  • -n, --numeric-uid-gid — long format with numeric user and group IDs
  • --author — with -l, print author of each file
  • -i, --inode — print inode number of each file
  • -s, --size — print allocated size of each file in blocks
  • -h, --human-readable — print sizes like 1K 234M 2G (powers of 1024)
  • --si — like -h, but use powers of 1000
  • --block-size=SIZE — scale sizes by SIZE when printing
  • --full-time — long format with --time-style=full-iso
  • --time-style=STYLE — time/date format with -l (full-iso, long-iso, iso, locale, +FORMAT)
  • -Z, --context — print security context of each file
  • -D, --dired — generate output for Emacs dired mode

Phase 4 — Sorting the output

GNU §10.1.3

  • -r, --reverse — reverse whatever the sorting method is
  • -S, --sort=size — sort by file size, largest first
  • -t, --sort=time — sort by modification time, newest first
  • -U, --sort=none — do not sort; list entries in directory order
  • -v, --sort=version — natural sort of version numbers within text
  • -X, --sort=extension — sort alphabetically by file extension
  • --sort=name — sort by name (default; explicit override)
  • --sort=width — sort by printed width of file name
  • -c, --time=ctime — use/sort by status change time
  • -u, --time=atime — use/sort by access time
  • --time=WORD — select timestamp (atime, ctime, mtime, birth)
  • -f — do not sort; enable -a and -U
  • --group-directories-first — group directories before files

Phase 5 — General output formatting

GNU §10.1.4

  • -1 — one file per line
  • -C, --format=vertical — list in columns, sorted vertically (default for terminal)
  • -F, --classify — append indicator (/ dir, * exec, @ symlink, = socket, | FIFO, > door)
  • -p, --indicator-style=slash — append / to directories
  • -m, --format=commas — comma-separated list, filling width
  • -x, --format=across — like -C, but sorted across rather than down columns
  • --format=WORD — select format (across, commas, long, single-column, verbose, vertical)
  • --file-type, --indicator-style=file-type — like -F, but do not append *
  • --indicator-style=WORD — none, slash, file-type, classify
  • -k, --kibibytes — default to 1024-byte blocks for -s and per-directory totals
  • --color[=WHEN] — colorize output (none, auto, always)
  • --hyperlink[=WHEN] — hyperlink file names (none, auto, always)
  • -T, --tabsize=COLS — assume tab stops at each COLS instead of 8
  • -w, --width=COLS — set output width (0 = no limit)
  • --zero — end each line with NUL, not newline (implies -1, --color=none, -N)

Phase 6 — Formatting the file names

GNU §10.1.6

  • -b, --escape — C-style backslash escapes for nongraphic characters
  • -N, --literal — print entry names without quoting
  • -q, --hide-control-chars — print ? instead of nongraphic characters
  • -Q, --quote-name — enclose entry names in double quotes
  • --quoting-style=WORD — literal, shell, shell-always, shell-escape, shell-escape-always, c, escape, clocale, locale
  • --show-control-chars — print nongraphic characters as-is (default for non-terminal)

Releases

Sponsor this project

Contributors

Languages

0