Log In  

I've started to dip my toe into thinking about token minimization. Now I write most of my code in Emacs' pico8-mode, which I love since it presents the code in the same font as pico-8 so I don't feel I've strayed too far.

But I've been aching for a way to count tokens outside of pico8's editor. I found the p8tool which can do that and more, so I wrote this elisp function to make it accessible for me within Emacs.

(defun pico8-token-count (beginning end)
    "Calculate the number of pico-8 tokens in the file or region."
    (interactive "r")
    (let ((path (if (use-region-p)
                  (let ((temp-file (make-temp-file "token-count-" nil ".p8")))
                      (write-region "pico-8 cartridge // http://www.pico-8.com\nversion 5\n__lua__\n" nil temp-file)
                      (write-region beginning end temp-file t)
        (message (shell-command-to-string (concat "p8tool stats '" path "' | grep tokens | cut -d ' ' -f 3")))))

Here it is in action.

P#136058 2023-10-18 06:17

That's a good idea! I use kakoune (more similar to vim), I'll need to integrate p8tool into my editor to count tokens in a selection.

And I love that PICO-8 mode for emacs, it's my first time seeing it!

P#136071 2023-10-18 12:40

[Please log in to post a comment]