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

# if parameter "DEBUG" is set, we set debug flags for lex/yacc
ifneq (${DEBUG},)
DEBUG_FLAGS += -d
endif

all: ${C2IEC}

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

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

y.tab.c: ${C2IEC}.y
	yacc ${DEBUG_FLAGS} $^

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}
	rm -f lex.yy.c
	rm -f y.tab.c

.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} < $< > $@


