### Install C3C on Gentoo Source: https://github.com/c3lang/c3c/blob/master/README.md Install the C3C compiler on Gentoo using the emerge command after enabling the GURU repository. ```sh sudo emerge -av dev-lang/c3c ``` -------------------------------- ### Basic C3 module and I/O example Source: https://context7.com/c3lang/c3c/llms.txt Demonstrates defining a module, importing standard I/O functions, and printing formatted output. ```c3 module hello_world; import std::io; fn void main() { io::printn("Hello, world!"); io::printfn("The answer is %d", 42); } ``` -------------------------------- ### Install C3 Compiler using Scoop on Windows Source: https://github.com/c3lang/c3c/blob/master/README.md Installs the C3 compiler using the Scoop package manager on Windows. Assumes Scoop is already installed and configured. ```sh scoop install c3 ``` -------------------------------- ### Enable and Sync Gentoo GURU Repository Source: https://github.com/c3lang/c3c/blob/master/README.md Commands to enable the GURU overlay and synchronize its repository for installing C3C on Gentoo. ```sh sudo eselect repository enable guru sudo emaint sync -r guru ``` -------------------------------- ### TCP Client and Server with std::net::tcp Source: https://context7.com/c3lang/c3c/llms.txt Provides examples for establishing a TCP client connection to send a request and receive a response, and setting up a TCP server to listen for and accept connections. ```c3 module demo; import std::io; import std::net::tcp; // TCP client: connect, send, receive fn void! tcp_client() { TcpSocket sock = tcp::connect("example.com", 80)!; defer (void)sock.close(); sock.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")!; char[4096] buf; sz? n = sock.read(buf[:]); if (try n) { io::printfn("Received %d bytes", n); io::printn((String)buf[:n]); } } // TCP server: listen and accept connections fn void! tcp_server() { TcpServerSocket server = tcp::listen("0.0.0.0", 8080, 10, REUSEADDR)!; defer (void)server.close(); io::printn("Listening on :8080"); TcpSocket client = tcp::accept(&server)!; defer (void)client.close(); client.write("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello")!; } fn void main() { if (catch err = tcp_client()) { io::eprintfn("client error: %s", err); } } ``` -------------------------------- ### Install C3 Compiler on Arch Linux using Pacman Source: https://github.com/c3lang/c3c/blob/master/README.md Installs the C3 compiler on Arch Linux using the official 'extra' repository with pacman. This is the recommended method for Arch users. ```sh sudo pacman -S c3c ``` -------------------------------- ### Install C3 on Fedora via COPR Source: https://github.com/c3lang/c3c/blob/master/README.md Enable the community-maintained COPR repository and install C3 using dnf. ```sh sudo dnf copr enable sisyphus1813/c3 sudo dnf install c3 ``` -------------------------------- ### Install Sanitizer Runtime Source: https://github.com/c3lang/c3c/blob/master/CMakeLists.txt Installs the sanitizer runtime directory to the bin/c3c_rt location in the installation prefix. This ensures that runtime libraries are available when the compiler is installed. ```cmake install(DIRECTORY $/c3c_rt/ DESTINATION bin/c3c_rt) ``` -------------------------------- ### C3 Project Configuration (project.json) Source: https://context7.com/c3lang/c3c/llms.txt Example of a project.json file used to configure C3 projects. It specifies language revision, warnings, source directories, build targets, and dependencies. ```json { "langrev": "1", "warnings": ["no-unused"], "sources": ["src/**"], "targets": { "my_app": { "type": "executable", "sources": ["src/**"] }, "my_lib": { "type": "static-lib", "sources": ["lib/**"] } }, "dependencies": ["vendor/raylib"] } ``` -------------------------------- ### Install C3c Compiler Source: https://context7.com/c3lang/c3c/llms.txt Installs the c3c compiler using a shell script. Supports both Linux/macOS (bash) and Windows (PowerShell). ```bash curl -fsSL https://raw.githubusercontent.com/c3lang/c3c/refs/heads/master/install/install.sh | bash ``` ```powershell iwr -useb https://raw.githubusercontent.com/c3lang/c3c/refs/heads/master/install/install.ps1 | iex ``` -------------------------------- ### Filesystem Path Manipulation with std::io::path Source: https://context7.com/c3lang/c3c/llms.txt Shows how to get the current working directory, list directory contents, build new paths, and check path properties using `std::io::path`. ```c3 module demo; import std::io; fn void main() { // Current working directory Path cwd = path::tcwd()!!; io::printfn("cwd: %s", cwd); // List directory contents PathList entries = path::ls(tmem, cwd)!!; foreach (i, p : entries) { bool is_dir = path::is_dir(p); io::printfn("%02d %s%s", i, p.basename(), is_dir ? "/" : ""); } // Build a new path by appending segments Path sub = cwd.append(tmem, "src")!!; io::printfn("sub path: %s", sub); // Check existence and type io::printfn("is dir: %s", path::is_dir(sub)); } ``` -------------------------------- ### Run C3C Compiler Example Source: https://github.com/c3lang/c3c/blob/master/README.md Execute the compiled C3C compiler to process a sample C3 source file. The command varies slightly depending on the operating system and build output directory. ```bash c3c.exe compile ../../resources/examples/hash.c3 ``` ```bash ./build/c3c compile resources/examples/hash.c3 ``` -------------------------------- ### Install C3 Compiler on Arch Linux using AUR helpers Source: https://github.com/c3lang/c3c/blob/master/README.md Installs the C3 compiler on Arch Linux using an AUR helper like paru, yay, or aura. This is an alternative to pacman for packages not in official repositories. ```sh paru -S c3c # or yay -S c3c # or aura -A c3c ``` -------------------------------- ### C3 Generic Stack Usage and C Integration Example Source: https://github.com/c3lang/c3c/blob/master/README.md Demonstrates using the generic stack module with different types (int and double) and importing an external C function (printf). Shows C3's zero initialization and alias creation for parameterized types. ```c3 import stack; // Define our new types, the first will implicitly create // a complete copy of the entire Stack module with "Type" set to "int" alias IntStack = Stack {int}; // The second creates another copy with "Type" set to "double" alias DoubleStack = Stack {double}; // If we had added "alias IntStack2 = Stack {int}" // no additional copy would have been made (since we already // have an parameterization of Stack {int} so it would // be same as declaring IntStack2 an alias of IntStack // Importing an external C function is straightforward // here is an example of importing libc's printf: extern fn int printf(char* format, ...); fn void main() { IntStack stack; // Note that C3 uses zero initialization by default // so the above is equivalent to IntStack stack = {}; stack.push(1); // The above can also be written IntStack.push(&stack, 1); stack.push(2); // Prints pop: 2 printf("pop: %d\n", stack.pop()); // Prints pop: 1 printf("pop: %d\n", stack.pop()); DoubleStack dstack; dstack.push(2.3); dstack.push(3.141); dstack.push(1.1235); // Prints pop: 1.123500 printf("pop: %f\n", dstack.pop()); } ``` -------------------------------- ### Setting up Resizable Panes Source: https://github.com/c3lang/c3c/blob/master/resources/docs.html Initializes resizable functionality for UI elements, likely sidebars or content panes. It abstracts the setup process for resizers, potentially using storage keys to persist user preferences. ```javascript function setupResizers() { const setup = (resizerId, sidebarId, isLeft, storageKey) => { ``` -------------------------------- ### Setup Sidebar Resizer Source: https://github.com/c3lang/c3c/blob/master/resources/docs.html Initializes a resizable sidebar component. It saves and restores the sidebar width using localStorage. Ensure the DOM elements and storage keys are correctly defined. ```javascript const resizer = document.getElementById(resizerId); const sidebar = (typeof sidebarId === 'string') ? document.getElementById(sidebarId) : sidebarId; if (window.innerWidth > 600) { const savedWidth = localStorage.getItem(storageKey); if (savedWidth) { sidebar.style.width = savedWidth + 'px'; } } let startX, startWidth; resizer.addEventListener('mousedown', (e) => { startX = e.clientX; startWidth = sidebar.offsetWidth; resizer.classList.add('dragging'); document.body.style.cursor = 'col-resize'; document.body.style.userSelect = 'none'; const onMouseMove = (e) => { const delta = e.clientX - startX; const newWidth = isLeft ? startWidth + delta : startWidth - delta; sidebar.style.width = `${newWidth}px`; }; const onMouseUp = () => { resizer.classList.remove('dragging'); document.body.style.cursor = 'default'; document.body.style.userSelect = 'auto'; localStorage.setItem(storageKey, sidebar.offsetWidth); document.removeEventListener('mousemove', onMouseMove); document.removeEventListener('mouseup', onMouseUp); }; document.addEventListener('mousemove', onMouseMove); document.addEventListener('mouseup', onMouseUp); }); }; ``` -------------------------------- ### Include and Link Directories for LLVM Source: https://github.com/c3lang/c3c/blob/master/CMakeLists.txt Sets the include and link directories for LLVM libraries. Ensure these paths are correctly set for your LLVM installation. ```cmake include_directories(${LLVM_INCLUDE_DIRS}) link_directories(${LLVM_LIBRARY_DIRS}) ``` -------------------------------- ### HTTP Server using TCP and Threads in C3 Source: https://context7.com/c3lang/c3c/llms.txt A complete example of a basic HTTP file server using `std::io` for TCP networking, thread pools for handling connections, and sending a simple text response. ```c3 module server; import std, libc; const int THREAD_COUNT = 64; fn int main(String[] args) { int port = 8080; TcpServerSocket? server = tcp::listen("0.0.0.0", port, 128, REUSEADDR); if (catch err = server) { io::eprintfn("Failed to listen on port %d: %s", port, err); return 1; } io::printfn("Serving on http://0.0.0.0:%d", port); pool::ThreadPool{THREAD_COUNT} workers; workers.init()!!; defer workers.stop_and_destroy(); while (true) { TcpSocket? conn = tcp::accept(&server); if (catch conn) continue; TcpSocket* conn_ptr = mem::new(TcpSocket); *conn_ptr = conn; workers.push(&handle_thread, conn_ptr); } } fn int handle_thread(void* arg) => @pool_init(mem, 65536) { TcpSocket* conn_ptr = (TcpSocket*)arg; TcpSocket conn = *conn_ptr; mem::free(conn_ptr); // Send a minimal HTTP response conn.write( "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: 5\r\n" "Connection: close\r\n\r\n" "Hello")!!; (void)conn.close(); return 0; } ``` -------------------------------- ### Compile and run C3 Hello World Source: https://context7.com/c3lang/c3c/llms.txt Shows the commands to compile the 'Hello, world!' C3 program and then execute it. ```sh c3c compile main.c3 ./hello_world # => Hello, world! # => The answer is 42 ``` -------------------------------- ### Install Specific C3 Compiler Version on Linux Source: https://github.com/c3lang/c3c/blob/master/README.md Installs a specific version of the C3 compiler on Linux by setting the C3_VERSION environment variable before executing the install script. ```bash curl -fsSL https://raw.githubusercontent.com/c3lang/c3c/refs/heads/master/install/install.sh | C3_VERSION=0.7.11 bash ``` -------------------------------- ### Install Specific C3 Compiler Version on Windows Source: https://github.com/c3lang/c3c/blob/master/README.md Installs a specific version of the C3 compiler on Windows by setting the C3_VERSION environment variable before running the install script. Requires PowerShell run as administrator. ```powershell $env:C3_VERSION='0.7.11'; powershell -ExecutionPolicy Bypass -Command "iwr -useb https://raw.githubusercontent.com/c3lang/c3c/refs/heads/master/install/install.ps1 | iex" ``` -------------------------------- ### Write and Read Files with std::io Source: https://context7.com/c3lang/c3c/llms.txt Demonstrates writing to and reading from a file using `std::io::file`. Ensure files are closed using `defer`. ```c3 module demo; import std::io; import std::io::file; fn void! write_and_read() { // --- Writing a file --- File f = file::open("hello.txt", "w")!; defer (void)f.close(); f.write("Line 1\n")!; f.write("Line 2\n")!; // --- Reading a file --- File r = file::open("hello.txt", "r")!; defer (void)r.close(); while (try line = io::treadline(&r)) { io::printfn(">> %s", line); } // => >> Line 1 // => >> Line 2 // --- File metadata --- bool ok = file::exists("hello.txt"); io::printfn("exists: %s", ok); // => exists: true long? sz = file::get_size("hello.txt"); io::printfn("size: %d bytes", sz!!); // => size: 14 bytes } fn void main() { if (catch err = write_and_read()) { io::eprintfn("I/O error: %s", err); } } ``` -------------------------------- ### Create a new C3 project scaffold Source: https://context7.com/c3lang/c3c/llms.txt Initializes a new C3 project with a standard directory structure and configuration files. ```sh c3c init my_project ``` -------------------------------- ### Install C3 Compiler on Windows using PowerShell Source: https://github.com/c3lang/c3c/blob/master/README.md Installs the latest stable release of the C3 compiler on Windows. The script adds the compiler to your PATH. Ensure PowerShell is run as an administrator. ```powershell iwr -useb https://raw.githubusercontent.com/c3lang/c3c/refs/heads/master/install/install.ps1 | iex ``` -------------------------------- ### Install C3 Git Version on Arch Linux using AUR helper Source: https://github.com/c3lang/c3c/blob/master/README.md Installs the git version of the C3 compiler on Arch Linux using an AUR helper. This provides the latest development version. ```sh paru -S c3c-git # or yay -S c3c-git ``` -------------------------------- ### Install C3 Compiler on Linux using curl Source: https://github.com/c3lang/c3c/blob/master/README.md Installs the latest stable release of the C3 compiler on Linux. The script adds the compiler to your PATH by updating ~/.bashrc. A terminal restart or sourcing the shell may be required. ```bash curl -fsSL https://raw.githubusercontent.com/c3lang/c3c/refs/heads/master/install/install.sh | bash ``` -------------------------------- ### Build and run the current C3 project Source: https://context7.com/c3lang/c3c/llms.txt Compiles and then executes the C3 project in the current directory. ```sh c3c run ``` -------------------------------- ### Get Local Name from UID Source: https://github.com/c3lang/c3c/blob/master/resources/docs.html Extracts the local name from a unique identifier (UID) by removing the module path prefix. ```javascript function getLocalName(uid) { const idx = uid.lastIndexOf('::'); return idx >= 0 ? uid.slice(idx + 2) : uid; } ``` -------------------------------- ### Compile C3C on Windows with Visual Studio 2022 Source: https://github.com/c3lang/c3c/blob/master/README.md Use this command to set up and build the C3C compiler on Windows using the Visual Studio 2022 release preset. Ensure LLVM is fetched during the build. ```bash cmake --preset windows-vs-2022-release -D C3_FETCH_LLVM=ON cmake --build --preset windows-vs-2022-release ``` -------------------------------- ### Compile and immediately run a C3 file Source: https://context7.com/c3lang/c3c/llms.txt This command compiles the specified C3 file and then executes the resulting binary. ```sh c3c compile-run main.c3 ``` -------------------------------- ### Download the Windows SDK Source: https://context7.com/c3lang/c3c/llms.txt Fetches the necessary Windows SDK, typically used when cross-compiling to a Windows target from a non-Windows host. ```sh c3c fetch windows-sdk ``` -------------------------------- ### Inline Enums Example Source: https://github.com/c3lang/c3c/blob/master/releasenotes.md Introduces the `inline` keyword for enums, allowing them to be defined directly within other constructs. This can improve code organization and reduce boilerplate. ```c3 Add `inline` to enums ``` -------------------------------- ### Get Build Environment Info with --build-env Source: https://github.com/c3lang/c3c/blob/master/releasenotes.md The `--build-env` flag displays information about the current build environment. This can be useful for diagnosing build-related problems. ```bash c3c --build-env ``` -------------------------------- ### Spawning Subprocesses and Reading Output in C3 Source: https://context7.com/c3lang/c3c/llms.txt Shows how to spawn external commands using `std::os::process` and stream their standard output. It adapts the command based on the operating system. ```c3 module demo; import std::io; import std::os::process; fn void main() { // Pick a directory-listing command per OS String cmd = env::WIN32 ? "dir" : "ls"; // Spawn the process Process p = process::spawn({ cmd })!!; p.join()!!; // Stream stdout line by line InStream stream = &&p.stdout(); while (try char b = stream.read_byte()) { io::printf("%c", b); } io::printn("...Done"); } ``` -------------------------------- ### Set LLVM/LLD CMake Paths Source: https://github.com/c3lang/c3c/blob/master/CMakeLists.txt Sets CMake variables to point to the fetched LLVM and LLD installation directories. This is crucial for `find_package` to locate the necessary CMake configuration files. ```cmake set(llvm_dir ${llvm_artifact_SOURCE_DIR}) set(CMAKE_PREFIX_PATH ${llvm_dir} ${CMAKE_PREFIX_PATH}) set(LLVM_DIR "${llvm_dir}/lib/cmake/llvm") set(LLD_DIR "${llvm_dir}/lib/cmake/lld") # TEST: For Windows, we might need to add the bin dir to prefix path for find_package to work well if (WIN32) set(CMAKE_SYSTEM_PREFIX_PATH ${llvm_dir} ${CMAKE_SYSTEM_PREFIX_PATH}) endif() find_package(LLVM REQUIRED CONFIG NO_DEFAULT_PATH) find_package(LLD REQUIRED CONFIG NO_DEFAULT_PATH) ``` -------------------------------- ### Run C3C Compiler Version Check on NixOS Source: https://github.com/c3lang/c3c/blob/master/README.md Verify the C3C compiler installation on NixOS by checking its version. This command is executed after building the compiler within the Nix environment. ```bash ./build/c3c -V ``` -------------------------------- ### Compile-Time Array Addition Example Source: https://github.com/c3lang/c3c/blob/master/releasenotes.md Illustrates the use of `+++` for compile-time array addition across various array types. This operator provides a concise way to combine arrays. ```c3 +++ on all types of arrays ```