Octave formatter

A source-code formatter for GNU Octave (.m files), written in Go. It parses Octave into an AST and re-prints it with consistent indentation, operator spacing, blank-line normalization, and comment formatting — similar in spirit to gofmt.

Install / build

go build -o octfmt ./cmd/octfmt

Usage

octfmt file.m              # print formatted result to stdout
octfmt -w file.m ...       # rewrite files in place
octfmt -l file.m ...       # list files whose formatting would change
octfmt -d file.m ...       # print a unified diff instead of rewriting
cat file.m | octfmt        # read from stdin, write to stdout

Flags:

Exit code is non-zero if any input file fails to parse; the offending file(s) are left untouched and the error is printed to stderr.

What it formats

What it deliberately does not do: reflow/wrap long single-row expressions or argument lists to a maximum line width (no line-width-based prettifying), reorder code, or change string quote style ('/" are semantically different in Octave — double-quoted strings support escapes — so quote style is always preserved).

Known limitations

Project layout

internal/lexer    tokenizer (handles Octave's ' transpose-vs-string
                   ambiguity and matrix-row whitespace significance)
internal/ast      AST node definitions
internal/parser   recursive-descent parser -> ast.File
internal/printer  ast.File -> formatted source
internal/diff     minimal unified-diff generator, used by -d
cmd/octfmt        CLI
testdata/golden   golden-file fixtures (regenerate with `go test ./internal/printer/... -run TestGolden -update`)

Tests

go test ./...

Includes lexer unit tests (transpose/string disambiguation, number formats, comments), parser tests (matrix element-splitting ambiguity, command-syntax detection, malformed-input error recovery), golden-file formatting tests (exact expected output, plus an automatic idempotency check that reformatting the output is a no-op) covering both everyday constructs and edge cases (anonymous functions, dynamic fields, do/until, parfor with a worker count, global/persistent with initializers, unwind_protect), and CLI integration tests.