A simple dice roll compiler and VM that (mostly) supports the Roll20 Dice Rolling Language Specification
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/darkliquid/roll"
)
func main() {
rand.Seed(time.Now().UnixNano())
out, err := roll.Parse(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(out)
}Then run echo '3d6+1' | go run main.go to get some output like:
Rolled "3d6+1" and got 6, 6, 4 for a total of 17
If you want the compiled program directly, use the compile/evaluate API:
program, err := roll.CompileString("3d6+1")
if err != nil {
panic(err)
}
result, err := roll.EvaluateProgram(program)
if err != nil {
panic(err)
}
fmt.Println(program.String(), result.Total)An interactive, fully client-side version runs in the browser via WebAssembly at https://darkliquid.github.io/roll/.
To build the site locally:
mise run build-wasmThis compiles cmd/wasm to docs/roll.wasm and copies Go's wasm_exec.js
into docs/. The static site can then be served from docs/ with any file
server, for example python3 -m http.server --directory docs.
Toolchains (Go, golangci-lint, govulncheck) are pinned in mise.toml. Install them
with mise install, then run the checks through mise:
mise run ci # build, vet, lint, vulncheck and test
mise run build # compile every package, including js/wasm
mise run lint # golangci-lint
mise run vulncheck # govulncheck