The Fog Programming Language
made by Marcell Varga
This documentation aims to provide a simple and easy way to understand the basics of the language. Most of the documentation is subject to change as it covers the earliest release of the language. The documentation is updated per release and it assumes you are using the latest release of the compiler.
The documentation also hosts examples of the code syntax for understandability.
Translations may be available in the future for multiple languages.
Introduction
Welcome to the Documentation of Fog, the statically typed, simple, flexible language.
What is Fog for
Fog was primarily created as a way to get experience with creating programming languages, and more importantly the inner workings of LLVM. It is also extremely useful to create quick programming ideas. We must also mention that the language is not slow even by modern standards due to its performant backend.
Who is Fog for
The language is mostly implemented for personal use for now, as I am sure the language does not hold much value for enterprises especially in this state.
The use of the documentation
The documentation assumes you are using the latest release of the language. It is advisable that you read the documentation from front to back, so that you are familiar with all the concepts the language brings.
Getting Started
The language can be installed and used fairly easily. You can build the binary for the compiler from the source available here. Additionally, prebuilt installers are available for Windows in the Github repository.
It is best advisable to add the binary to PATH, if compiling the binary manually so that it can be used easily later.
After installing the compiler we finally get to work on our first ever project!
Initializing a project
There are multiple commands available for creating a new project.
You can the commands as following on Windows:
- For Initializing a project in a pre-existing folder:
$ mkdir /%project-name%
$ cd %project-name%
$ fog init
- For creating a new folder specifically for the project:
$ fog new %project-name%
Initializing a project
After installing the compiler we finally get to work on our first ever project!
There are multiple commands available for creating a new project.
You can the commands as following on Windows:
- For Initializing a project in a pre-existing folder:
$ mkdir /%project-name%
$ cd %project-name%
$ fog init
- For creating a new folder specifically for the project:
$ fog new %project-name%
Primitives
Scalar types
Almost all of the basic scalar types are implemented in this language.
Basic types
Bits | Signed | Unsigned | Float |
---|---|---|---|
8-bit | - | uintsmall | - |
16-bit | inthalf | uinthalf | floathalf |
32-bit | int | uint | float |
64-bit | intlong | uintlong | floatlong |
Additional types
bool
: Used for storing a boolean value.
void
: Used for indicating a function with no returned value
string
: A string variable can be used to store text. The language handles strings as a pointer to an array. (This comes into play when interacting with FFI)
Custom Types
Structs can also be created by the user via the struct
keyword. Structs cannot contain themselves.
Definining a struct is similar to how one would do it in other languages.
struct my_struct {
field1: int,
field2: string,
field3: bool,
}
Variables
Creating Variables
Since the language is statically typed, every variable has to have its type defined at compile time. Initializing a variable is not crucial, as variables get a default value if left unintialized by the user.
Here is how one can define a variable with the aforementioned types.
int age = 23;
string name = "marci1175";
bool is_male = true;
Defining struct may seem tricky at first, but they are no different from most languages. Every field has to be manually initialized with their own default value.
struct person {
age: int,
name: string,
is_male: bool,
}
person somebody = person { age: 23, name: "marci", is_male: true, };
Hello World!
Writing the code for the project
Fog source files always end with the .f
extension. A wide range of naming schemes can be used with fog support them all.
Warning⚠️: File names cannot contain any special characters except "_".
Navigate to: %project-name%/src/main.f
And enter the code:
import puts(msg: string): int;
function main(): int {
puts("Hello World!");
return 0;
}
Save the file, run the command fog c
and navigate to ./output
.
Here, you will see your binary's LLVM-IR which can be then parsed by a linker to produce a valid binary. Use your preferred method of linking this file, to create an exe.
My preferred way of linking is via Clang, so I am going to use that.
$ fog c
$ clang %project-name%.ll
$ ./%project-name%.exe
Hello World!
If you can see "Hello World!" in your console, congratulations! You have created your first ever application with Fog.