LANGUAGE := colorsep
EXT := csg
DIR := .
UPARSE=./

CC=cc 
OPTS=-g -Wall 

all: $(LANGUAGE:%=%) regen Makefile # $(LANGUAGE:%=%.ng)  # mnemalyse
	echo All up to date.

$(LANGUAGE:%=%): $(LANGUAGE:%=%.h) Makefile regexp-lexer.o flex.o uparse.o
	$(CC) $(OPTS) -o $(LANGUAGE:%=%) uparse.o regexp-lexer.o flex.o -lm

# This regenerates the skeleton compiler code (CST to AST conversion) from the grammar.
$(LANGUAGE:%=%-ast.h): regen
	./regen > $(LANGUAGE:%=%-ast.h)

# This regenerates the skeleton compiler code (CST to AST conversion) from the grammar.
$(LANGUAGE:%=%-comp.h): gencomp
	./gencomp > $(LANGUAGE:%=%-comp.h)

# This builds the tool to regenerate the skeleton compiler code from the grammar.
# It currently only builds the CST to AST conversion.
regen: regen.c $(LANGUAGE:%=%.h) parser.h flex.o Makefile
	$(CC) $(SAFEOPTS) -o regen -DGRAMMAR=\"$(LANGUAGE:%=%.h)\" regen.c flex.o -lm

# This builds the tool to regenerate the skeleton compiler code from the grammar.
# It currently only builds the CST to AST conversion.
gencomp: gencomp.c $(LANGUAGE:%=%.h) parser.h flex.o Makefile
	$(CC) $(SAFEOPTS) -o gencomp -DGRAMMAR=\"$(LANGUAGE:%=%.h)\" gencomp.c flex.o -lm

# This is a backup of the grammar extracted from the grammar tables.
# Only the basic rules are saved. Comments and some grammar extensions are dropped.
# It may be useful when making changes to the grammar after the compiler code is written.
$(LANGUAGE:%=%.ng): $(LANGUAGE:%=%-ast.h)
	sed -ne 's|\(.*\)//\\\\ \(.*\)|\2|gp' $(LANGUAGE:%=%-ast.h) > $(LANGUAGE:%=%.ng)


# This is the body of the parser.  It requires the grammar to have been converted to table form.
# uparse-main.c replaced by colorsep-main.c

uparse.o: uparse.c colorsep-main.c parser.h flex.h Makefile $(LANGUAGE:%=%.h) $(LANGUAGE:%=%-ast.h) $(LANGUAGE:%=%-comp.h) $(LANGUAGE:%=%-filter.c)
	$(CC) $(OPTS) -o uparse.o \
                        -DPARSER_MAIN=\"colorsep-main.c\" \
			-DGRAMMAR=\"$(LANGUAGE:%=%.h)\" \
			-DAPPMODULE=\"$(LANGUAGE:%=%-filter.c)\" -DAPPCOMMAND=walk_csg \
			-DCST2AST=\"$(LANGUAGE:%=%-ast.h)\" \
			-Wno-unused-variable -c uparse.c 


# This converts the grammar to table form.
$(LANGUAGE:%=%.h): $(LANGUAGE:%=%.g) takeon Makefile
	./takeon $(LANGUAGE:%=%.g) > $(LANGUAGE:%=%.h)

# This builds the utility which converts the grammar to table form.
# NOTE: -fsanitize=address causes erroneous trap when running takeon.  Do not use that option here.

takeon: takeon.c takeon-output.c takeon-checks.c flex.h Makefile flex.o
	$(CC) $(SAFEOPTS) -o takeon                                 takeon.c flex.o -lm

# This is a utility used in much of the code to support flex arrays, i.e. arrays
# which extend their allocated space as more elements are accessed.  It also
# supplies some checks on array bounds and unassigned variables.  These are
# relatively cheap to implement - the runtime of a program using flex arrays
# and checking is about 3 times that of the same program with static arrays
# and no checks.  Use the "-DFAST" option at the head of this file for a
# faster executable but only once the code is stable and ready for release testing.
flex.o: flex.c flex.h Makefile
	$(CC) $(SAFEOPTS) -c flex.c

# This is an internal regular expression matcher which has been modified
# to support Unicode characters.
regexp-lexer.o: regexp-lexer.c Makefile parser.h # tools/mnemosyne.h
	$(CC) $(OPTS) -c regexp-lexer.c -Itools

# Mr Sheen.
clean:
	rm -f *~ *~ *.o *.o $(LANGUAGE:%=%) takeon regen gencomp \#*\# \#*\# [a-z]*.[chg].html [a-z]*.[chg].html colorsep-ast.h colorsep-comp.h #*.sh.html
