# The Tungsten programming language

the tungsten logo

## introduction *Tungsten* is a statically-typed multi-paradigm compiled programming language made with the LLVM framework and inspired by C++ and Java ## Example Below is a snippet of code which shows some of the basic functionalities of tungsten ```c++ fun fibonacci(num n) -> num { if (n <= 1) ret n; ret fibonacci(n - 1) + fibonacci(n - 2); } fun main() -> num { for (num i = 0; i < 10; ++i) { print("fibonacci(%.0lf) = %.0lf\n", i, fibonacci(i)); } ret CodeSuccess; } ``` > [!WARNING] > the language is still under development, and everything is subject to change # Trying the language if you want to try *Tungsten* before installing the compiler, feel free to do so on the [online tungsten compiler](https://rickisgone.hackclub.app) # VSCode Extension You can install the VSCode extension for *Tungsten* from the [VSCode marketplace](https://marketplace.visualstudio.com/items?itemName=RickIsGone.tungsten) or by searching for `tungsten` in the extensions tab of VSCode. You'll need to have the compiler installed on your system for the extension to work properly. # Building | Platform | Build Status | |----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Ubuntu | [![Ubuntu Build](https://github.com/rickisgone/tungsten/actions/workflows/Ubuntu%20build.yml/badge.svg)](https://github.com/rickisgone/tungsten/actions/workflows/Ubuntu%20build.yml) | | Windows | [![Windows Build](https://github.com/rickisgone/tungsten/actions/workflows/Windows%20Build.yml/badge.svg)](https://github.com/rickisgone/tungsten/actions/workflows/Windows%20Build.yml) | | Mac | [![macOS build](https://github.com/RickIsGone/tungsten/actions/workflows/MacOS%20build.yml/badge.svg)](https://github.com/RickIsGone/tungsten/actions/workflows/MacOS%20build.yml) | ## Prerequisites Global - cmake 3.28 or newer - git Compiler - LLVM Package Manager - Libcurl - libarchive If you are on windows i'd suggest using [vcpkg](https://github.com/microsoft/vcpkg) to install the dependencies > [!IMPORTANT] > this project uses C++ modules, which are currently not supported by all compilers and CMake generators, so make sure > to use both a compiler and a CMake generator that supports modules **1. Cloning the repo:** Start by cloning the repo with `git clone https://github.com/rickisgone/tungsten` after doing this follow the instruction for the targeted OS
WINDOWS

**2. Compiling the project:** Here I'm using vcpkg: ```bash cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE='VCPKG_DIR\scripts\buildsystems\vcpkg.cmake' -DVCPKG_TARGET_TRIPLET=x64-windows-static -DLLVM_DIR='VCPKG_DIR/installed/x64-windows-static/share/llvm/' -Dzstd_DIR='VCPKG_DIR/installed/x64-windows-static/share/zstd' cmake --build build --config Release ``` replace `VCPKG_DIR` with the vcpkg directory

LINUX

**2. Compiling the project:** *the default compiler and CMake generator on linux (gcc and Make) don't support modules, so I'll be using Clang and Ninja in the example below* ```bash cmake -S . -B build -GNinja -DCMAKE_CXX_COMPILER=clang++ cmake --build build --config Release ```

# Syntax ## Variables declaration Tungsten has a similar naming convention to Rust, where base types are written in **camelCase** and structs and classes are written in **PascalCase** | Type | Alignment (Bytes) | |----------|-------------------| | `void` | N/A | | `char` | 2 | | `bool` | 1 | | `String` | N/A | | `num` | 8 | > [!IMPORTANT] > `num` is a `double` so to use functions like `print` and `input` you need to use the `%lf` format > specifier Tungsten has also a variadic type called `ArgPack` which is just like `...` in C ### Stack allocation ```c++ num myVariable = 247; num myVariable{247}; ``` ## Control flow statements ### If If statements are written just like in C++ ```cpp if (condition) { // do stuff } else { // do other stuff } ``` And just like in C++ you can avoid using braces for single instructions ```cpp if (condition) // do stuff else // do other stuff ``` ### While Unlike in C++, while statements require braces to work ```cpp while (condition) { // do stuff } ``` ### Do while Do whiles instead are just like in C++ ```cpp do { // do stuff } while(condition); ``` ### For For statements just like while statements require braces to work ```c++ for (num i = 0; i < 10; ++i) { // do stuff } ``` ## Functions Rather than a C++ aproach, functions are more similar to Rust's: ```c++ fun myFunction() -> num { ret 247; } ``` ### Extern Functions There are two types of extern functions - C functions - tungsten functions You can import C functions with ```cpp extern "C" fun myCFun() -> void; ``` and tungsten functions with ```cpp extern fun myFun() -> void; ``` ### Main Function The `main` function can return either `int` or `int32`. Command line arguments are passed as a `String` array. If no return value is provided, `0` will be returned by default. ```c++ fun main(String[] args) -> num { /* your code */ ret CodeSuccess; } ``` ### integrated Core Functions Tungsten has a reduced number of integrated core functions | Type | Name | Arguments | functionality | |----------|-------------------|-----------------------|----------------------------------------------| | `num` | shell | (String cmd) | same function as `system()` in C | | `void` | print | (String fmt, ArgPack) | same function as `printf()` in C | | `void` | input | (String fmt, ArgPack) | same function as `scanf()` in C | | `String` | __builtinFile | no arguments | returns the name of the file | | `String` | __builtinFunction | no arguments | returns the name of the function | | `num` | __builtinColumn | no arguments | returns the number of the column | | `num` | __builtinLine | no arguments | returns the number of the line | | `String` | nameof | (any variable) | returns the name of the variable | | `String` | typeof | (any variable) | returns the type of the variable | | `num` | sizeof | (any variable) | returns the size of the type of the variable | ## Building a project To build a project, you can either compile via cmd by calling the `tungsten` compiler or you can make a `build.tgs` file and use the integrated buildsystem ### Compiling via cmd > [!IMPORTANT] > Tungsten currently uses *clang* to compile the generated llvm ir to an executable, so you need to have it installed > for the compiler to work ```shell cd projectDirectory tungsten [flags] ``` ### Compiling via buildsystem > [!IMPORTANT] > Not added yet A simple project's build file will look something like this: ```c++ import build; Project myProject{Executable, "myProject"}; fun main() -> num { myProject.addSource("main.tgs"); myProject.build(); } ``` To build the file, you'll then have to call the compiler in the directory where the build file is located ```shell cd projectDirectory tungsten tgs-build [flags] ``` The compiler will then automatically compile the `build.tgs` file. After compiling the file, the compiler will automatically execute the `build` executable which will compile the project following the rules set by the `build.tgs` file. The build output will then be put into the `projectDirectory/build` directory # TPKG > [!IMPORTANT] > Not added yet tpkg is the package manager included with the *Tungsten programming language* > [!NOTE] > tpkg uses a centralized git directory, which is not checked, meaning it's on the users to open issues reporting > malicious packages package files are written in **json**, and a simple package file looks like this ```json { "name": "myPackage", "version": "1.0.0", "description": "my package description", "homepage": "homepage url", "license": "MIT/Apache 2.0/ecc.", "source": "source/release code zip url", "dependencies": [ "your dependencies" ], "build": { "type": "cmd/buildsystem", "args": [ "myargs" ] } } ``` # License This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE.txt) file for details.