THIS PROGRAMMING LANGUAGE IS A WORK IN PROGRESS. ANYTHING CAN CHANGE AT ANY MOMENT.
Nibble is a programming language based on C. Primarily developed as a learning exercise, this compiler generates x64 machine code on Linux without the use of any libraries other than libc. The current goal is to add the following features to C:
- Order-independent declarations
- Module import system (based on javascript ES6)
- Type inference
- Typed enumerations
- Multiple return values (return anonymous structure object aka tuples)
- Type-safe variadic procedures
- Array slices (See C's Biggest Mistake)
- Variables initialized to zero by default (use
---
value to leave uninitialized) - Defer statement (like go)
- Default procedure arguments
- Named procedure arguments
- Generic procedures (like c++ templates)
Nibble supports the followng operating systems:
- x64 linux
- x64 windows
- x86 linux
- x86 windows
- arm64 macOS
Refer to the language reference document for details. It is very much a work in progress.
The only library required to build the Nibble compiler is the C standard library.
The following terminal command generates an executable called nibble
in the root project directory.
gcc -I./src -O2 -D NIBBLE_ENABLE_UNITY_BUILD -o nibble src/main.c
Here's an example that compiles the "Hello World" example program on linux.
$ ./nibble -o hello_world examples/hello_world/main.nib
[INFO]: Parsing module examples/hello_world/main.nib ...
[INFO]: Resolving/type-checking ...
[INFO]: Generating IR ...
[INFO]: Generating NASM assembly output: hello_world.s ...
[CMD]: nasm -f elf64 hello_world.s -o hello_world.o
[CMD]: ld -o hello_world hello_world.o
$ ./hello_world
Hello, World
To compile a Nibble program, the compiler only needs the file (i.e., module) containing your program's main()
procedure. The compiler can automatically detect any imported or included files. Refer to the language reference to learn more about importing or including other files.
Run ./nibble -h
for available command-line options.
$ ./nibble -h
Usage: ./nibble [OPTIONS] <input.nib>
OPTIONS:
-h Print this help message
-s Silent mode (no output to stdout)
-os [linux | win32 | osx] Target OS
-arch [x64 | x86] Target architecture
-I <module_search_path> Add module (import/include) search path
-L <library_search_path> Add library search path
-o <output_file> Output binary file name. Defaults to `out`
-asm Generate assembly text file
main.nib:
import "std/basic" as std;
proc main(argc : int, argv : ^^char) => int
{
std::print_out("Hello, World!\n");
return 0;
}
$ ./nibble -s main.nib -o hello
$ ./hello
Hello, World!
This example game uses the terminal to display its UI.
$ ./nibble -s examples/snake_game/main.nib -o snake
$ ./snake
Nibble does not yet support all basic C features:
- Integer types
- Floating-point types
- Structure types
- Union types
- Enum types
- Typedefs
- Procedures
- Basic procedures with non-variadic parameters
- Varidic parameters
- Statements
- static_assert
- if/else
- while
- do while
- for loop
- switch
- break
- continue
- return
- Expressions
- Assignments
- Ternary operator
- Binary operators
- Unary operators
Here it is. It is very much a work in progress.