Skip to content

adrianlizarraga/nibble

Repository files navigation

THIS PROGRAMMING LANGUAGE IS A WORK IN PROGRESS. ANYTHING CAN CHANGE AT ANY MOMENT.

Nibble

Unit Tests

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.

Quickstart

1. Build the Nibble compiler

The only library required to build the Nibble compiler is the C standard library.

Linux

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

2. Compile a nibble program

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.

Command-line options

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

Code examples

Hello, World

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!

Snake game

This example game uses the terminal to display its UI.

$ ./nibble -s examples/snake_game/main.nib -o snake
$ ./snake

Status of the project

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

Language reference

Here it is. It is very much a work in progress.

About

Programming language (C-like, minimal dependencies)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages