# XORCE - XOR Algebra Compiler
# Build System

CC = gcc
CFLAGS = -Wall -Wextra -O2 -std=c11
LDFLAGS = -lm

SRCS = xorce.c lex.c parse.c holo.c codegen.c core.c
OBJS = $(SRCS:.c=.o)
HDRS = core.h types.h
TARGET = xorce

.PHONY: all test clean examples

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

%.o: %.c $(HDRS)
	$(CC) $(CFLAGS) -c -o $@ $<

# Run all test programs
test: $(TARGET) examples
	@echo "=== Running XORCE Tests ==="
	@echo ""
	@echo "--- Test 1: Flat Logic Algebra ---"
	./$(TARGET) examples/flat.xor
	@echo ""
	@echo "--- Test 2: Pauli (2 qubits) ---"
	./$(TARGET) examples/pauli.xor
	@echo ""
	@echo "--- Test 3: Clifford Cl(3,0) ---"
	./$(TARGET) examples/clifford.xor
	@echo ""
	@echo "--- Test 4: Octonion (Cayley level 3) ---"
	./$(TARGET) examples/octonion.xor
	@echo ""
	@echo "=== All Tests Completed ==="

# Ensure examples directory exists
examples:
	@mkdir -p examples

# Clean build artifacts
clean:
	rm -f $(OBJS) $(TARGET) *.xorc examples/*.xorc

# Debug build
debug: CFLAGS = -Wall -Wextra -g -O0 -std=c11 -DDEBUG
debug: clean $(TARGET)

# Inspect a chip file
inspect: $(TARGET)
	@if [ -z "$(CHIP)" ]; then \
		echo "Usage: make inspect CHIP=<file.xorc>"; \
	else \
		./$(TARGET) -i $(CHIP); \
	fi

# Verify a chip file
verify: $(TARGET)
	@if [ -z "$(CHIP)" ]; then \
		echo "Usage: make verify CHIP=<file.xorc>"; \
	else \
		./$(TARGET) --verify $(CHIP); \
	fi

# Show help
help:
	@echo "XORCE Build System"
	@echo ""
	@echo "Targets:"
	@echo "  all      - Build the xorce compiler (default)"
	@echo "  test     - Build and run all test programs"
	@echo "  clean    - Remove build artifacts and chip files"
	@echo "  debug    - Build with debug symbols"
	@echo "  inspect  - Inspect a chip file (make inspect CHIP=file.xorc)"
	@echo "  verify   - Verify a chip seal (make verify CHIP=file.xorc)"
	@echo "  help     - Show this help message"
