CC = gcc
YACC = yacc
LEX = flex

CFLAGS = -O2 -g3 -Ih -Wall -Wshadow -Werror
# verbose header debug
YFLAGS = -vdt
# no-default-rule case-insensitive
FFLAGS = -si

O_FILES  = y.tab.o lex.yy.o yyerror.o symtab.o block.o stack.o label.o queue.o
O_FILES += asm_assign.o asm_math.o asm_cmp.o asm_log.o asm_procedure.o
OFILES = $(addprefix o/,$(O_FILES))

LIBS = -lfl

# Start the rules.

test: algol60 fixperms
	@test/run

test-rewrite: algol60
	@test/rewrite
	@echo 'Read and verify all the test cases now.'
	@make test-plan

test-plan: algol60
	@test/nplan index > report/index.html
	@test/nplan complete > report/complete.html

again: clean algol60

fixperms:
	-@find . -type f ! -group san474 -print0 | xargs -r0 chgrp san474
	-@find . -type f ! -perm -664 -print0 | xargs -r0 chmod ug+rw,o+r

clean:
	rm -f y.tab.c h/y.tab.h lex.yy.c o/*.o *.o algol60 y.output

.PHONY:	test fixperms again

# The specific targets.

algol60: $(OFILES)
	$(CC) $(CFLAGS) -o $@ $(OFILES) $(LIBS)

lex.yy.c: algol60.l
	$(LEX) $(FFLAGS) algol60.l

y.tab.c: algol60.y
	$(YACC) $(YFLAGS) algol60.y
	@mv -f y.tab.h h/y.tab.h
#	@perl -i -pe 's/yyerror\("syntax error"\);//' $@
	
o/y.tab.o: y.tab.c h/algol60.h
	$(CC) $(CFLAGS) -Wno-unused -Wno-parentheses -c $< -o $@

o/lex.yy.o: lex.yy.c h/y.tab.h h/algol60.h
	$(CC) $(CFLAGS) -Wno-unused -Wno-uninitialized -c $< -o $@

o/%.o: %.c h/%.h h/algol60.h
	$(CC) $(CFLAGS) -c $< -o $@
