Home›Blog›Larzscript
Larzscript

Building a Budget Tracker in Larzscript, Part 1: Accounts and Pay

2026-08-01 · Larzscript

This starts a short series building a real, public Larzscript project from scratch: [larzscript-budget](https://github.com/larz-scripter/larzscript-budget), a personal budget tracker CLI. Not a toy - every expense in it is a real pay statement between real wallet values, and every spending limit is a real require/unless guardrail. Part 1 covers the foundation: accounts, your first transaction, and the one real engineering problem the whole project has to solve.

The idea: a category is a wallet

Larzscript's wallet type isn't a balance field with extra steps - pay records a real ledger entry every time money moves. So a budget category can just *be* a wallet: an expense is pay $12.00 from checking to food, and food's balance becomes the running total spent in that category, for free, from the language's own primitive. No separate "sum up the transactions" logic needed for that part.

The real problem: wallets don't survive between runs

A CLI tool runs once per command and exits. wallet/pay are runtime constructs scoped to *that one execution* - there's no built-in way for a balance to persist to the next larzscript budget.lz ... invocation. Worse: wallet NAME binds to a literal identifier in your source code. You can't write wallet accounts[user_input] = ... - so an unbounded, user-defined set of account names (exactly what a budget app needs) can't each get a permanent wallet variable.

The fix: persist money as plain integer cents in a JSON file between runs. For any single command, rehydrate just the one or two wallets involved as real, literal wallet values, do the real pay, then write the resulting balance back out:

fn wallet_from_cents(c) {
  wallet w = money(c / 100.0)
  return w
}

wallet_from_cents() returns an actual wallet - a first-class value, just like a list or a dict. The caller doesn't re-declare it with wallet again (that would try to use the wallet as a *starting balance* for a new wallet, which fails type-checking); it's just bound with let:

let acct = wallet_from_cents(ledger["accounts"][account])
let cat = wallet_from_cents(ledger["categories"][category])

unless acct.balance >= money(abs_cents / 100.0) {
  fail("insufficient funds in '" + account + "'")
}

pay money(abs_cents / 100.0) from acct to cat

(That unless instead of require's built-in comma-message is deliberate - require's optional message has to be a literal string, not a concatenated one with the account name spliced in. unless + a normal function call has no such restriction.)

Getting the balance back out

Reading a Money value back out as a plain integer for JSON storage needs one more small trick: there's no Money / Money division to get a plain ratio. str(money_value) always formats as "$D.CC" or "-$D.CC", so cents_of() round-trips through that string and parses it back - verified exact from $0.01 to $1,234,567.89 before relying on it.

Try it

$ larzscript budget.lz init
$ larzscript budget.lz account-add checking 5000.00
added account 'checking': $5000.00
$ larzscript budget.lz category-add food
$ larzscript budget.lz add "coffee" -4.50 food
spent $4.50 on 'coffee' (food)

Part 2 covers spending limits enforced by require/unless as a real guardrail, not a check bolted on after the fact. Full source on GitHub.

Ready to get started?

See the full source on GitHub →

Explore the apps