From 19fa2f2d880a693af2dd6346b120bb88e615f4e5 Mon Sep 17 00:00:00 2001 From: Barrett Harber Date: Sun, 11 Jan 2026 19:20:45 -0500 Subject: feat: add zig compiler support --- Makefile | 14 ++++++++++++-- README.md | 18 +++++++++++++++++- src/main.c | 13 +++++++++++-- tests/run_tests.sh | 21 ++++++++++++++++++++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a4dfccd..0b62685 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ - +# Compiler configuration +# Default: gcc +# To build with clang: make CC=clang +# To build with zig: make CC="zig cc" CC = gcc CFLAGS = -Wall -Wextra -g -I./src -I./src/ast -I./src/parser -I./src/codegen -I./plugins -I./src/zen -I./src/utils -I./src/lexer -I./src/analysis -I./src/lsp TARGET = zc @@ -78,4 +81,11 @@ clean: test: $(TARGET) ./tests/run_tests.sh -.PHONY: all clean install uninstall test +# Build with alternative compilers +zig: + $(MAKE) CC="zig cc" + +clang: + $(MAKE) CC=clang + +.PHONY: all clean install uninstall test zig clang diff --git a/README.md b/README.md index ab0bfd9..836bbc7 100644 --- a/README.md +++ b/README.md @@ -443,6 +443,7 @@ Zen C is designed to work with most C11 compilers. Some features rely on GNU C e ```bash zc run app.zc --cc clang +zc run app.zc --cc zig ``` ### Test Suite Status @@ -451,9 +452,22 @@ zc run app.zc --cc clang |:---|:---:|:---|:---| | **GCC** | **100%** | All Features | None. | | **Clang** | **100%** | All Features | None. | +| **Zig** | **100%** | All Features | None. Uses `zig cc` as a drop-in C compiler. | | **TCC** | **~70%** | Basic Syntax, Generics, Traits | No `__auto_type`, No Intel ASM, No Nested Functions. | -> **Recommendation:** Use **GCC** or **Clang** for production builds. TCC is excellent for rapid prototyping due to its compilation speed but misses some advanced C extensions Zen C relies on for full feature support. +> **Recommendation:** Use **GCC**, **Clang**, or **Zig** for production builds. TCC is excellent for rapid prototyping due to its compilation speed but misses some advanced C extensions Zen C relies on for full feature support. + +### Building with Zig + +Zig's `zig cc` command provides a drop-in replacement for GCC/Clang with excellent cross-compilation support. To use Zig: + +```bash +# Compile and run a Zen C program with Zig +zc run app.zc --cc zig + +# Build the Zen C compiler itself with Zig +make zig +``` --- @@ -482,6 +496,8 @@ make test # Run with different compiler ./tests/run_tests.sh --cc clang +./tests/run_tests.sh --cc zig +./tests/run_tests.sh --cc tcc ``` ### Extending the Compiler diff --git a/src/main.c b/src/main.c index d23cd0b..b58ee41 100644 --- a/src/main.c +++ b/src/main.c @@ -32,7 +32,7 @@ void print_usage() printf(" -o Output executable name\n"); printf(" --emit-c Keep generated C file (out.c)\n"); printf(" --freestanding Freestanding mode (no stdlib)\n"); - printf(" --cc C compiler to use (gcc, clang, tcc)\n"); + printf(" --cc C compiler to use (gcc, clang, tcc, zig)\n"); printf(" -O Optimization level\n"); printf(" -g Debug info\n"); printf(" -v, --verbose Verbose output\n"); @@ -131,7 +131,16 @@ int main(int argc, char **argv) { if (i + 1 < argc) { - strcpy(g_config.cc, argv[++i]); + char *cc_arg = argv[++i]; + // Handle "zig" shorthand for "zig cc" + if (strcmp(cc_arg, "zig") == 0) + { + strcpy(g_config.cc, "zig cc"); + } + else + { + strcpy(g_config.cc, cc_arg); + } } } else if (strcmp(arg, "-o") == 0) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 8c3c7f7..8c2fbc7 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -1,5 +1,14 @@ #!/bin/bash +# Zen-C Test Suite Runner +# Usage: ./tests/run_tests.sh [zc options] +# +# Examples: +# ./tests/run_tests.sh # Test with default compiler (gcc) +# ./tests/run_tests.sh --cc clang # Test with clang +# ./tests/run_tests.sh --cc zig # Test with zig cc +# ./tests/run_tests.sh --cc tcc # Test with tcc + # Configuration ZC="./zc" TEST_DIR="tests" @@ -7,7 +16,17 @@ PASSED=0 FAILED=0 FAILED_TESTS="" -echo "** Running Zen C test suite **" +# Display which compiler is being used +CC_NAME="gcc (default)" +for arg in "$@"; do + if [ "$prev_arg" = "--cc" ]; then + CC_NAME="$arg" + break + fi + prev_arg="$arg" +done + +echo "** Running Zen C test suite (compiler: $CC_NAME) **" if [ ! -f "$ZC" ]; then echo "Error: zc binary not found. Please build it first." -- cgit v1.2.3