C2IEC=c2iec
TESTS=$(patsubst %.c, %.test, $(wildcard test/*.c))
TEST_LOGS=$(patsubst %.c, %.test.log, $(wildcard test/*.c))

all: ${C2IEC}

${C2IEC}: lex.yy.c y.tab.c
	gcc -o $@ $^

lex.yy.c: ${C2IEC}.l
	flex -d $^

y.tab.c: ${C2IEC}.y
	yacc -d $^

y.dot: ${C2IEC}.y
	yacc -g $^

commit: ${TEST_LOGS}
	for i in ${TEST_LOGS}; do \
		cp $$i $$(dirname $$i)/$$(basename $$i .log).expected; \
	done
clean:
	rm -f ${C2IEC}
	rm -f ${TEST_LOGS}

.PHONY: test
test: ${TESTS}

%.test: %.test.log
	@echo "========================================"
	@echo "checking output of test $*"
	@diff $< $*.test.expected

%.test.log: %.test.txt
	@echo "========================================"
	@echo "output of test $*"
	@echo "========================================"
	@cat $< | tee $@

%.test.txt: %.c ${C2IEC}
	./${C2IEC} < $< > $@


