C3 is a C-like language trying to be "an incremental improvement over C" rather than a whole new language. C3 owes a lot to the ideas of the C2 language: to iterate on top of C without trying to be a whole new language.
C3 tries to be an alternative in the C/C++ niche: fast and close to the metal.
- Procedural "get things done"-type of language.
- Try to stay close to C - only change what's really necessary.
- C ABI compatibility and excellent C integration.
- Learning C3 should be easy for a C programmer.
- Data is inert.
- Avoid "big ideas" & the "more is better" fallacy.
- Introduce some higher level conveniences where the value is great.
Create a main.c3
file with:
module hello_world;
import std::io;
func void main()
{
io::printf("Hello, world!\n");
}
Make sure you have the standard libraries at either ../lib/std/
or /lib/std/
.
Then run
c3c compile main.c3
The generated binary will be called a.out
.
- No mandatory header files
- New semantic macro system
- Module based name spacing
- Subarrays (slices) and dynamic arrays built in
- Compile time reflection
- Enhanced compile time execution
- Generics based on generic modules
- "Result"-based zero overhead error handling
- Defer
- Value methods
- Associated enum data
- Built in strings
- No preprocessor
- Undefined behaviour trapped on debug by default
- Optional pre and post conditions
It's possible to try out the current C3 compiler in the browser: https://ide.judge0.com/ – this is courtesy of the developer of Judge0.
Design work is still being done in the design draft here: https://c3lang.github.io/c3docs/. If you have any suggestions, send a mail to christoffer@aegik.com, file an issue or discuss C3 on its dedicated Discord: https://discord.gg/qN76R87
The compiler should compile on Linux and MacOS, but needs some development love to work on Windows. Also, parts of the code is still rough and needs to be made solid.
- For/while/do
-
if
/ternary - Structs
- Union
- Enums
- Value methods
- Compound literals
- Designated initalizers
- Slicing syntax
- Arrays and subarrays
- Modules
-
$unreachable
- Compile time assert with
$assert
- Compiler guiding
assert
- C code calling by declaring methods
extern
- Compile time variables
- Basic macros
- 4cc, 8cc, 2cc
- Enum type inference in switch/assignment
- Integer type inference
- Error type
- Failable error handling
-
try
for conditional execution -
catch
for error handling - Implicit unwrap after
catch
-
sizeof
-
typeof
- 2s complement wrapping operators
- Labelled break / continue
-
nextcase
statement - Expression blocks
- Do-without-while
- Foreach statement
- Templates
- Distinct types
- Built-in linking
- CT only macros evaluating to constants
- range initializers e.g.
{ [1..2] = 2 }
- Trailing body macros e.g.
@foo(1, 100; int a) { bar(a); };
- Anonymous structs
- Complete C ABI conformance in progress
- Debug info in progress
- Virtual type in progress
- Enum associated data support
- Windows support
- All attributes
- Associative array literals
- CT type constants
- Reflection methods
- LTO/ThinLTO setup
-
global
/shared
for globals - Complex macros
- Escape macros
- Implicit capturing macros
- Subarray initializers
- Bitstructs
-
asm
section -
$switch
-
$for
- Pre-post conditions
- Stdlib inclusion
- String functions
- Simd vector types
- If you wish to contribute with ideas, please file issues on the c3docs: https://github.com/c3lang/c3docs instead of the compiler.
- Discuss the language on discord to help iron out syntax.
- Interested in contributing to the stdlib? Please get in touch on Discord.
- Are you a Windows dev? Please help make the compiler work on Windows!
- Install instructions for other Linux and Unix variants are appreciated.
- Make sure you have a C compiler that handles C11 and a C++ compiler, such as GCC or Clang. Git also needs to be installed.
- Install CMake:
sudo apt install cmake
- Install LLVM 11:
sudo apt-get install clang-11 zlib1g zlib1g-dev libllvm11 llvm-11 llvm-11-dev llvm-11-runtime liblld-11-dev liblld-11
- Clone the C3C github repository:
git clone https://github.com/c3lang/c3c.git
- Enter the C3C directory
cd c3c
. - Create a build directory
mkdir build
- Change directory to the build directory
cd build
- Set up CMake build for debug:
cmake -DLLVM_DIR=/usr/lib/llvm-11/cmake -DCMAKE_BUILD_TYPE=Debug ..
- Build:
cmake --build .
You should now have a c3c
executable.
You can try it out by running some sample code: ./c3c compile ../resources/examples/hash.c3
You can build c3c
using either an Ubuntu 18.04 or 20.04 container:
./build-with-docker.sh 18
Replace 18
with 20
to build through Ubuntu 20.04.
For a release build specify:
./build-with-docker.sh 20 Release
A c3c
executable will be found under bin/
.
- Install CMake:
brew install cmake
- Install LLVM 11:
brew install llvm
- Clone the C3C github repository:
git clone https://github.com/c3lang/c3c.git
- Enter the C3C directory
cd c3c
. - Create a build directory
mkdir build
- Change directory to the build directory
cd build
- Set up CMake build for debug:
cmake ..
- Build:
cmake --build .