#!/bin/bash
cd build

success_count=0
failure_count=0
failed_files=()
successful_files=()

test_folder=../test/e

for test_file in $test_folder/*.go; do
    base_name=$(basename "$test_file" .go)

    echo -e "\n========== 编译 $base_name =========="
    if ! ./LiteGoCompiler $test_folder/$base_name.go; then
        echo "编译错误"
        failure_count=$((failure_count + 1))
        failed_files+=("$base_name")
        continue
    fi
    
    nasm -f elf32 -o "$base_name/out.o" "$base_name/out.asm"
    nasm -f elf32 -o "$test_folder/print.o" $test_folder/print.asm
    

    if ! ld -m elf_i386 -o "$base_name/out.bin" "$base_name/out.o" "$test_folder/print.o"; then
        echo "链接错误"
        failure_count=$((failure_count + 1))
        failed_files+=("$base_name")
        continue
    fi

    echo -e "\n========== 运行 $base_name =========="
    if ! $base_name/out.bin; then
        echo "运行错误"
        failure_count=$((failure_count + 1))
        failed_files+=("$base_name")
        continue
    fi

    success_count=$((success_count + 1))
    successful_files+=("$base_name")
done

# 输出统计结果
echo -e "\n========== 编译结果统计 =========="
echo "成功的文件数量: $success_count"
echo "成功的文件名: ${successful_files[*]}"
echo "失败的文件数量: $failure_count"
echo "失败的文件名: ${failed_files[*]}"