Skip to content

Rust on Rails, With Cart: An experimental programming language with a compiler written in Rust.

License

Notifications You must be signed in to change notification settings

bbayazit16/cart

Repository files navigation

Cart

License: MIT

Cart is a simple rust-like programming language with a compiler written in Rust. The language is compiled into LLVM IR via Inkwell, and then to native machine code. The object file is linked via cc. Unlike Rust or other low-level languages, the language is intended not to be a systems programming language, but rather a high level language (on the level of Java) that compiles to native code. For this reason, Cart is garbage collected and simple-to-use language.

Example Syntax: Nth Fibonacci Number

// Last value of a block automatically corresponds to the return value
func fib(n: int) -> int {
    if n <= 2 {
        1
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

func main() {
    let n: int = 10;
    // Or use auto type inference
    let result = fib(n);
    // Exits program with result: No need for a semicolon if
    // it is the last expression in a block.
    result
}

For the formal grammar, see grammar.

Installation

Cart requires LLVM 18 to be installed on your system. On MacOS, run:

brew install llvm@18

On Linux, run:

wget https://apt.llvm.org/llvm.sh && \
chmod u+x llvm.sh && \
sudo ./llvm.sh 18 && \
sudo apt install -y libpolly-18-dev libz-dev`

Then, run the following command to install Cart:

curl -fsSL https://raw.githubusercontent.com/bbayazit16/cart/main/install.sh | bash

Or, you may choose to directly install the binary from the releases page.

Usage:

Usage: cart <COMMAND>

Commands:
  compile  Compile a file
  run      Compile and run a file
  help     Print this message or the help of the given subcommand(s)

Options:
  -h, --help     Print help
  -V, --version  Print version

Future Plans

TODO:

Parsing and scanning are complete for all the following. The next steps are to generate LLVM IR for the following:

  • Functions and recursion
  • Function calls
  • Blocks with last expression as return value
  • Return statements
  • Variables
  • If-Else
  • Comments
  • Basic Error Reporting
  • Basic operators
  • Structs
  • Strings
  • Extensions (impl blocks)
  • Basic Arrays/Vectors
  • Loops
  • More types
  • Advanced Arrays/Vectors
  • Enums
  • Type Checking
  • Generics
  • Errors
  • Use statements
  • Pattern Matching
  • More advanced error reporting
  • Standard Library

Development

During development, make sure to set CARTLIB_PATH environment variable.

If you are running from the source code, this should typically be set to either target/debug or target/release depending on whether you are running in debug or release mode.

To compile the standard library, run cargo build or cargo build --release.

License

Cart is licensed under the MIT license. See LICENSE for more details.

About

Rust on Rails, With Cart: An experimental programming language with a compiler written in Rust.

Resources

License

Stars

Watchers

Forks

Packages

No packages published