Skip to content

Compile Command

The compile command transpiles FastC source code to C11.

Usage

fastc compile [OPTIONS] <INPUT>

Arguments

Argument Description
<INPUT> Input FastC source file (.fc)

Options

Option Description
-o, --output <FILE> Output file (default: stdout)
--emit-header Also generate a C header file
-h, --help Print help

Examples

Compile to Stdout

fastc compile src/main.fc

Output is printed to the terminal.

Compile to File

fastc compile src/main.fc -o build/main.c

Generate Header

fastc compile src/main.fc -o build/main.c --emit-header

This creates both build/main.c and build/main.h.

Output Format

The generated C code includes:

  1. Header comment
  2. Runtime include
  3. Standard library includes
  4. Forward declarations
  5. Type definitions (structs, enums)
  6. Function implementations

Example Output

Input (hello.fc):

fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

fn main() -> i32 {
    return add(1, 2);
}

Output:

/* Generated by fastc - do not edit */

#include "fastc_runtime.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

int32_t add(int32_t a, int32_t b);
int32_t main(void);

int32_t add(int32_t a, int32_t b) {
    int32_t __tmp0;
    if (__builtin_add_overflow(a, b, (&__tmp0))) {
        fc_trap();
    }
    return __tmp0;
}

int32_t main(void) {
    return add(1, 2);
}

Header Generation

With --emit-header, a header file is generated with:

  • Include guards
  • Type definitions
  • Function declarations (public only)

Example Header

#ifndef MAIN_H
#define MAIN_H

#include <stdint.h>
#include <stdbool.h>

int32_t add(int32_t a, int32_t b);
int32_t main(void);

#endif /* MAIN_H */

Check Command

To type-check without generating code:

fastc check src/main.fc

This runs all compiler phases except code generation:

  • Lexing
  • Parsing
  • Name resolution
  • Type checking

Success Output

No errors found.

Error Output

error: undefined name 'undefined_var'
  --> src/main.fc:5:12
   |
 5 |     return undefined_var;
   |            ^^^^^^^^^^^^^

Format Command

Format FastC source code:

# Format in place
fastc fmt src/main.fc

# Output to stdout
fastc fmt src/main.fc -o -

# Check formatting (exit 1 if not formatted)
fastc fmt src/main.fc --check

Compiling Generated C

After generating C code, compile with your C compiler:

# Generate C
fastc compile src/main.fc -o main.c

# Compile with gcc
gcc -I/path/to/runtime main.c -o main

# Compile with clang
clang -I/path/to/runtime main.c -o main

Or use fastc build --cc for automatic compilation.

See Also