CC = gcc
CFLAGS = -Wall -Wextra -O2 -Isrc
LDFLAGS = -lm -lpthread

SRC_DIR = src
TEST_DIR = tests
EXAMPLE_DIR = examples
BUILD_DIR = build
BIN_DIR = bin

TARGET = $(BIN_DIR)/rc

SOURCES = $(SRC_DIR)/main.c \
          $(SRC_DIR)/globals.c \
          $(SRC_DIR)/tokenizer.c \
          $(SRC_DIR)/parser.c \
          $(SRC_DIR)/interpreter.c \
          $(SRC_DIR)/native_functions.c \
          $(SRC_DIR)/string_utils.c

OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)

TESTS = $(TEST_DIR)/feature_test.rc \
        $(TEST_DIR)/endless_loop_test.rc \
        $(TEST_DIR)/math_test.rc \
        $(TEST_DIR)/string_test.rc \
        $(TEST_DIR)/string_manip_test.rc \
        $(TEST_DIR)/increment_decrement_test.rc \
        $(TEST_DIR)/file_io_test.rc \
        $(TEST_DIR)/double_test.rc \
        $(TEST_DIR)/break_continue_test.rc \
        $(TEST_DIR)/async_io_test.rc

EXAMPLES = $(EXAMPLE_DIR)/http_simple.rc \
           $(EXAMPLE_DIR)/http_persistent.rc \
           $(EXAMPLE_DIR)/http_multi.rc \
           $(EXAMPLE_DIR)/async_demo.rc

.PHONY: all clean test run-tests run-examples help

all: dirs $(TARGET)

dirs:
	@mkdir -p $(BUILD_DIR) $(BIN_DIR)

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) -c $< -o $@

$(TARGET): dirs $(OBJECTS)
	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

clean:
	rm -rf $(BUILD_DIR) $(BIN_DIR)

test: $(TARGET)
	@echo "=== Running All Tests ==="
	@echo ""
	@echo "Running feature tests..."
	@$(TARGET) $(TEST_DIR)/feature_test.rc 2>&1 | grep -v "Error at token" || true
	@echo ""
	@echo "Running endless loop test..."
	@$(TARGET) $(TEST_DIR)/endless_loop_test.rc 2>&1 | grep -v "Error at token" || true
	@echo ""
	@echo "Running math tests..."
	@$(TARGET) $(TEST_DIR)/math_test.rc 2>&1 | grep -v "Error at token" || true
	@echo ""
	@echo "Running string concatenation tests..."
	@$(TARGET) $(TEST_DIR)/string_test.rc 2>&1 | grep -v "Error at token" || true
	@echo ""
	@echo "Running string manipulation tests..."
	@$(TARGET) $(TEST_DIR)/string_manip_test.rc 2>&1 | grep -v "Error at token" || true
	@echo ""
	@echo "Running increment decrement tests..."
	@$(TARGET) $(TEST_DIR)/increment_decrement_test.rc 2>&1 | grep -v "Error at token" || true
	@echo ""
	@echo "=== All Tests Completed ==="

run-feature-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/feature_test.rc

run-endless-loop: $(TARGET)
	$(TARGET) $(TEST_DIR)/endless_loop_test.rc

run-math-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/math_test.rc

run-string-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/string_test.rc

run-string-manip: $(TARGET)
	$(TARGET) $(TEST_DIR)/string_manip_test.rc

run-increment-decrement-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/increment_decrement_test.rc

run-file-io-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/file_io_test.rc

run-double-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/double_test.rc

run-break-continue-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/break_continue_test.rc

run-async-io-test: $(TARGET)
	$(TARGET) $(TEST_DIR)/async_io_test.rc

run-http-simple: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/http_simple.rc

run-http-persistent: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/http_persistent.rc

run-http-multi: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/http_multi.rc

run-async-demo: $(TARGET)
	$(TARGET) $(EXAMPLE_DIR)/async_demo.rc

help:
	@echo "RC - Retoor's C Interpreter"
	@echo ""
	@echo "Usage:"
	@echo "  make                  - Build the interpreter"
	@echo "  make test             - Run all feature tests"
	@echo "  make clean            - Remove all build artifacts"
	@echo ""
	@echo "Individual Tests:"
	@echo "  make run-feature-test           - Run feature_test.rc (negative numbers, ==, !=)"
	@echo "  make run-endless-loop           - Run endless_loop_test.rc (while(1) test)"
	@echo "  make run-math-test              - Run math_test.rc (math functions)"
	@echo "  make run-string-test            - Run string_test.rc (string concatenation)"
	@echo "  make run-string-manip           - Run string_manip_test.rc (string manipulation & slicing)"
	@echo "  make run-file-io-test           - Run file_io_test.rc (file I/O operations)"
	@echo "  make run-double-test            - Run double_test.rc (double data type)"
	@echo "  make run-break-continue-test    - Run break_continue_test.rc (break/continue)"
	@echo "  make run-async-io-test          - Run async_io_test.rc (async I/O)"
	@echo ""
	@echo "Examples:"
	@echo "  make run-http-simple            - Run http_simple.rc (single connection HTTP server)"
	@echo "  make run-http-persistent        - Run http_persistent.rc (persistent HTTP server)"
	@echo "  make run-http-multi             - Run http_multi.rc (HTTP server, 100 connections)"
	@echo "  make run-async-demo             - Run async_demo.rc (comprehensive async I/O demo)"
	@echo ""
	@echo "Directory Structure:"
	@echo "  src/       - Source code files"
	@echo "  tests/     - Test programs (.rc files)"
	@echo "  examples/  - Example programs (.rc files)"
	@echo "  build/     - Compiled object files"
	@echo "  bin/       - Final executable (rc)"
