-
Notifications
You must be signed in to change notification settings - Fork 0
Get Started
Unlike some languages like python and more in line with languages like C, Magma requires the definition of a "main" function. This main function will be the entry point of the program, as in the code present in the main function will be executed before the code of other functions. As such, the main function's role is to orchestrate the program and dispatch the execution flow towards the relevant functions.
As a first program to get an idea of how to use Magma, it is common to write a "Hello world" program. The goal of it is to simply write the text "Hello, World!" to the console.
Here is a hello world program in Magma:
mod main
use "std:io" io
main() void:
io.printLn("Hello, World!")
..
Let's go through the example line-by-line.
Every Magma .mg file must start with a module name declaration.
mod is a keyword, every module name declaration must start with this keyword.
Here main is the name of module. The Magma compiler will look for the "main" function in the "main" module to find the entry point of the program.
A use statement allows you to pull in the code from another module and use it within the current file.
use is the keyword which starts the use statement.
"std:io" is the path to the module, a path starting with "std:" will look for the module within the Magma standard library. Both absolute and relative path to .mg files are accepted.
io is a given alias, as in a name you give to the imported module. You will have to use this name again to call functions from the imported module.
The main function declaration, any code present between the opening ':' and closing '..' will be enclosed within the "function body", in practice this means that the enclosed code will be run only when the function is called. In the case of the main function the code will be run instantly.
main is the name of the function, this same name will have to be used to call the function.
() is the arguments list, in this example it is empty.
void is the return type of the function. Functions can return a value as the result of the execution, the result type denotes the type of data it will return. In the case of the main function, void represents the absence of data.
: opens the body of the function. Any code written in the lines between this opening tag and the '..' closing tag will be considered to be within the body of the function.
This is a call to the function printLn declared within the module imported with the alias io.
Historically the term print refers to literally printing text to a printer, which were the actual predecessors to terminals. While printers didn't stick around as a way to get feedback from your code, the term print did in basically every programming language since then.
Here printLn will write text to the console, then tell the console to write any other following text on a new line.
io refers to the imported module from the line use "std:io" io
.printLn refers to a function named printLn declared within ìo
( is the start of the argument list that will be passed to the function printLn
"Hello, World!" is a string of text, it is the first and only argument that will be passed to the function printLn
) is the end of the argument list.
The result of executing this line will be that the text Hello, World! will appear in the console.
As mentioned previously .. closes the body of the function.