SOURCE = ../../m2m2/parse
CFLAGS = -O -DSYM -DTREE -I$(SOURCE)
LDFLAGS =
OBJECTS = main.o module.o block.o type.o formalparam.o\
statement.o ctexpression.o ctterm.o expression.o term.o\
variable.o uconstant.o\
ident.o keyword.o\
symtab.o inseek.o Filbuf.o

Tree.index: Tree fix-index.el
	sed "/ line /d" < Tree | sed "s/| //g" | sed "/\[all module initializations\]/d" | tr "[]" "()" | sort -f +1 > $@
	emacs -batch $@ -l fix-index -f fix-index -f basic-save-buffer -kill

Tree: Tree1 fix-tree.el
	emacs -batch Tree1 -l fix-tree -f fix-tree -f basic-save-buffer -kill
	mv Tree1 $@
	mv Tree1~ Tree1

Tree1: ker.c++
	sed < ker.c++ "s/ switch(/ X_switch(/" | sed "s/^switch(/X_switch(/" | calls | sed "/^$$/d" | sed "s/ \.\.\.//" | sed "/ M_/s/$$/ [module initialization]/" | sed "s/ [MX]_/ /" > $@

ker.c++: ker.c+
	sed "/^P_/!d" < ker.c+ > $@
	sed "/^M_/!d" < ker.c+ >> $@
	sed "/^[MP]_/d" < ker.c+ >> $@

ker.c+: ker.c filter1
	sed < ker.c "/ process /s/^/P_/" | sed "s/		*.*//" | filter1 | sed "s/^GATjump() {/& kerout();/" | sort -f > $@

filter1: filter1.c /usr/include/stdio.h
	$(CC) -O filter1.c -o $@

Names: ker.c
	grep "()	" ker.c | sed "s/^M_//" | sed "s/()//" | sort -f > $@

ker.c: ker.c-like nuc.c
	grep "^	" ker.c-like | grep -v "^		" | sed "s/^	//" > $@
	grep "^		" ker.c-like | grep -v "^			" | sed "s/^		//" >> $@
	grep "^			" ker.c-like | grep -v "^				" | sed "s/^			//" >> $@
	grep "^				" ker.c-like | grep -v "^					" | sed "s/^				//" >> $@
	grep "^					" ker.c-like | grep -v "^						" | sed "s/^					//" >> $@
	grep "^						" ker.c-like | grep -v "^							" | sed "s/^						//" >> $@
	grep "^							" ker.c-like | sed "s/^							//" >> $@
	cat nuc.c >> $@

ker.c-like: tree KER.i
	tree KER.i $@ 2> ker.errors

tree: $(OBJECTS)
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@

$(OBJECTS): /usr/include/stdio.h /usr/include/ctype.h $(SOURCE)/parse.h

main.o: $(SOURCE)/main.c
	$(CC) -c $(CFLAGS) $(SOURCE)/main.c

module.o: $(SOURCE)/module.c
	$(CC) -c $(CFLAGS) $(SOURCE)/module.c

block.o: $(SOURCE)/block.c
	$(CC) -c $(CFLAGS) $(SOURCE)/block.c

type.o: $(SOURCE)/type.c
	$(CC) -c $(CFLAGS) $(SOURCE)/type.c

formalparam.o: $(SOURCE)/formalparam.c
	$(CC) -c $(CFLAGS) $(SOURCE)/formalparam.c

statement.o: $(SOURCE)/statement.c
	$(CC) -c $(CFLAGS) $(SOURCE)/statement.c

ctexpression.o: $(SOURCE)/ctexpression.c
	$(CC) -c $(CFLAGS) $(SOURCE)/ctexpression.c

ctterm.o: $(SOURCE)/ctterm.c
	$(CC) -c $(CFLAGS) $(SOURCE)/ctterm.c

expression.o: $(SOURCE)/expression.c
	$(CC) -c $(CFLAGS) $(SOURCE)/expression.c

term.o: $(SOURCE)/term.c
	$(CC) -c $(CFLAGS) $(SOURCE)/term.c

variable.o: $(SOURCE)/variable.c
	$(CC) -c $(CFLAGS) $(SOURCE)/variable.c

uconstant.o: $(SOURCE)/uconstant.c
	$(CC) -c $(CFLAGS) $(SOURCE)/uconstant.c

ident.o: $(SOURCE)/ident.c
	$(CC) -c $(CFLAGS) $(SOURCE)/ident.c

keyword.o: $(SOURCE)/keyword.c
	$(CC) -c $(CFLAGS) $(SOURCE)/keyword.c

symtab.o: $(SOURCE)/symtab.c
	$(CC) -c $(CFLAGS) $(SOURCE)/symtab.c

inseek.o: $(SOURCE)/inseek.c
	$(CC) -c $(CFLAGS) $(SOURCE)/inseek.c

Filbuf.o: $(SOURCE)/Filbuf.c
	$(CC) -c $(CFLAGS) $(SOURCE)/Filbuf.c
