A Lisp implemented in AWK
# SPDX-License-Identifier: BSD-2-Clause

AWK = original-awk

all: glotawk check benchmark

help = "\n\
Targets:\n\
        glotawk:       build the interpreter, a single large\n\
                       executable AWK script\n\
        check:         run tests\n\
        benchmark:     run a few benchmarks\n\
        all (default): glotawk, check, and benchmark\n\
        clean:         get rid of build products\n\
        heapviz:       fancy Graphviz view of initial heap and GC\n\
"
help:
	@echo ${help} >&2

depend: .depend
	:

.depend:
	awk -f tmpl-depends.awk *.tmpl.awk > .depend

glotawk-build.awk:
	$(AWK) -f tmpl.awk glotawk-build.tmpl.awk > $@

image.awk: glotawk-build.awk lib-eval.awk
	(echo "(dump-dot \"before-gc.dot\")"; echo "(gc-dot \"marks.dot\" \"sweeps.dot\")"; echo "(dump \"$@\")"; echo "(dump-dot \"after-gc.dot\")") | $(AWK) -f glotawk-build.awk

glotawk: glotawk-build.awk
	$(AWK) -f tmpl.awk glotawk-run.tmpl.awk > $@
	chmod a+x $@

test: check
check: glotawk sane_lisp
	./sane_lisp; x=$$?; if [ $$x -eq 0 ]; then echo --pass--; else echo --FAIL--; fi; return $$x

clean:
	rm -f glotawk glotawk-build.awk image.awk heap-nomarks.dot gc-marks.dot gc-sweeps.dot heap.dot .depend

A_HUNDRED_THINGS = 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 \
                   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 \
                   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 \
                   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 \
                   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

benchmark: glotawk
	@echo --- To run benchmark.glotawk ---
	bash -c "time for x in $(A_HUNDRED_THINGS); do ./glotawk benchmark.glotawk >/dev/null; echo -n .; done; echo"
	@echo --- Just to start up ---
	bash -c "time for x in $(A_HUNDRED_THINGS); do echo 3 | ./glotawk >/dev/null; echo -n .; done; echo"

heap-nomarks.dot: glotawk
	echo "(dump-dot \"$@\")" | ./glotawk

gc-marks.dot gc-sweeps.dot: glotawk
	echo "(gc-dot \"gc-marks.dot\" \"gc-sweeps.dot\")" | ./glotawk -v LOG_LEVEL=3

heap.dot: glotawk heap-nomarks.dot gc-marks.dot gc-sweeps.dot
	cat heap-nomarks.dot | \
		sed -e '/^\/\*mark colors\*\/$$/r gc-marks.dot' | \
		sed -e '/^\/\*sweep colors\*\/$$/r gc-sweeps.dot' > $@

heapviz: heap.dot
	dot -Tx11 heap.dot

.PHONY: all depend check test clean benchmark heapviz
.MAKE: .depend