#!/bin/bash
# DOL Feature Demos - Master Runner
# Runs all DOL feature demonstrations and generates combined output
# Output: status-do/out_feature_all.md

set -e
cd "$(dirname "$0")/.."

echo "=========================================="
echo "  DOL Language Feature Demonstrations"
echo "=========================================="
echo ""

mkdir -p status-do

# Build once
echo "Building DOL with all features..."
cargo build --features "wasm cli" 2>&1 | tail -5
echo ""

# Run all feature demos
echo "Running feature demonstrations..."
echo ""

DEMOS=(
    "dol_feature_parse.sh:Parsing"
    "dol_feature_validate.sh:Validation"
    "dol_feature_wasm.sh:WASM Compilation"
    "dol_feature_arithmetic.sh:Arithmetic"
    "dol_feature_genes.sh:Genes"
    "dol_feature_traits.sh:Traits"
    "dol_feature_systems.sh:Systems"
    "dol_feature_constraints.sh:Constraints"
)

for demo in "${DEMOS[@]}"; do
    script="${demo%%:*}"
    name="${demo##*:}"

    if [ -f "script-dol/$script" ]; then
        echo "[$name] Running..."
        chmod +x "script-dol/$script"
        bash "script-dol/$script" 2>&1 | tail -1
    else
        echo "[$name] Script not found: $script"
    fi
done

echo ""
echo "=========================================="
echo "  Generating Combined Report"
echo "=========================================="
echo ""

# Create combined output
OUTPUT_FILE="statu-do/out_feature_all.md"
cat > "$OUTPUT_FILE" << 'EOF'
# DOL Language Feature Report

**Generated:** $(date)
**Investigation:** Claude Flow Swarm - DOL Feature Demonstrations

---

## Executive Summary

This report demonstrates all major DOL language features and their current implementation status.

| Feature | Parse | Validate | WASM |
|---------|-------|----------|------|
| Parsing | YES | YES | N/A |
| Arithmetic | YES | YES | **YES** |
| Functions | YES | YES | **YES** |
| Genes | YES | YES | NO |
| Traits | YES | YES | NO |
| Systems | YES | YES | NO |
| Constraints | YES | YES | NO |
| Control Flow | YES | YES | NO |

---

## Feature Reports

EOF

# Append each feature report
for md in statu-do/out_feature_*.md; do
    if [ -f "$md" ] && [ "$md" != "$OUTPUT_FILE" ]; then
        echo "" >> "$OUTPUT_FILE"
        echo "---" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
        cat "$md" >> "$OUTPUT_FILE"
    fi
done

# Footer
cat >> "$OUTPUT_FILE" << 'EOF'

---

## Conclusion

DOL has a complete frontend (lexer, parser, validator) that handles all language constructs. WASM compilation is partially implemented for simple arithmetic functions.

### What Works Today
1. Parse any DOL file
2. Validate semantic correctness
3. Compile simple functions to WASM
4. Run WASM with wasmtime

### What Needs Implementation
1. Control flow (if/else, match)
2. Gene struct layouts
3. Trait vtables
4. System method dispatch

---

*Generated by DOL Feature Demo - Run All*
EOF

echo "Combined report written to: $OUTPUT_FILE"
echo ""
echo "=========================================="
echo "  Demo Complete!"
echo "=========================================="
echo ""
echo "Individual reports:"
ls -la statu-do/out_feature_*.md 2>/dev/null || echo "  (none generated)"
echo ""
echo "To view combined report:"
echo "  cat status-do/out_feature_all.md"
echo ""
