# Hop — a packet-pipeline language, and Denis Lemire's prime.c rewritten in it.
#
# Declarative-ish targets in the spirit of the GitOps workflows Denis runs:
# describe what you want, then `make` it.

HOP        := bin/hop
REF        := bin/prime_ref
PRIME      := examples/prime.hop
REF_SRC    := examples/reference/prime.c
EXPECTED   := examples/reference/prime.expected.txt
# candidates to emit for bounded targets
LIMIT      ?= 100

# verify uses bash process substitution
SHELL      := /bin/bash

.PHONY: all build test vet fmt run demo factorial verify reference clean

all: build test vet

## build: compile the hop interpreter to bin/hop
build:
	@mkdir -p bin
	go build -o $(HOP) ./cmd/hop

## test: run the Go test suite (prime fidelity, general-purpose, error handling)
test:
	go test ./...

## vet: static checks
vet:
	go vet ./...

## fmt: gofmt the tree
fmt:
	go fmt ./...

## run: run the prime scanner forever, like the original prime.c (Ctrl-C to stop)
run: build
	$(HOP) run $(PRIME) --metrics

## demo: a short, bounded showcase — primes + live metrics + the other example
demo: build
	@echo "== prime pipeline (first $(LIMIT) candidates) =="
	$(HOP) run $(PRIME) --limit $(LIMIT) --metrics
	@echo
	@echo "== factorial (proof the language is general purpose) =="
	$(HOP) run examples/factorial.hop

## factorial: run the second example on its own
factorial: build
	$(HOP) run examples/factorial.hop

## reference: build the original C as an oracle, regenerate the golden prime lines
reference:
	@mkdir -p bin
	cc -O2 -o $(REF) $(REF_SRC)
	$(REF) | grep --line-buffered "is prime" | head -n 30 > $(EXPECTED)
	@echo "wrote $(EXPECTED)"

## verify: prove Hop's prime lines match the compiled C oracle exactly
verify: build
	@diff <(cat $(EXPECTED)) <($(HOP) run $(PRIME) --limit 200 | grep "is prime" | head -n 30) \
		&& echo "OK: Hop output is identical to the reference prime.c"

clean:
	rm -rf bin
