diff --git a/docs/docs/how-to/web-assembly.md b/docs/docs/how-to/web-assembly.md index 6d696900a..0af825a58 100644 --- a/docs/docs/how-to/web-assembly.md +++ b/docs/docs/how-to/web-assembly.md @@ -22,6 +22,12 @@ public f fibo(int n) { if n <= 1 { return n; } return fibo(n - 1) + fibo(n - 2); } + +// Main function can be deleted, when compiling with --no-entry +f main() { + int fiboBase = 45; + printf("Fibonacci of %d: %d", fiboBase, fibo(fiboBase)); +} ``` You can test, if the code works by compiling and running it, using the following command:
@@ -89,9 +95,7 @@ function fibo(n) { ``` As you can see, you can load the `main.wasm` file in JavaScript using the WebAssembly module. When successful, we can call all -exposed Spice functions using their mangled names. If you don't know the mangled name of your function, you can either use -`wasm-objdump` like described above or add the flag `-asm` to the compile command to dump the assembly. There you will find the -mangled names of all functions. +exposed (`public`) Spice functions using their name. ## Execute diff --git a/docs/docs/language/builtin-types.md b/docs/docs/language/builtin-types.md index cd7c8aa0f..e60740140 100644 --- a/docs/docs/language/builtin-types.md +++ b/docs/docs/language/builtin-types.md @@ -2,7 +2,7 @@ title: Builtin data types --- -In extension to the [primitive data types](primitive-types.md), Spice offers builtin data types. +In extension to the [primitive data types](primitive-types.md), Spice offers several builtin data types. ## The `String` data type In opposite to the [string primitive type](primitive-types.md#the-string-data-type), the `String` builtin type is @@ -11,7 +11,7 @@ mutable and offers several ways to modify the contained value. !!! tip "Tip" Use the `string` primitive type over the `String` builtin type as much as possible, due to its advantages in runtime performance. The usage of the `String` builtin type is only recommended, when you need to modify the value of the - string at runtime. `string` variables are always immutable. + string at runtime. `string` values are always immutable. ### Constructors The `String` builtin type offers the following constructors: diff --git a/docs/docs/language/builtins.md b/docs/docs/language/builtins.md index 3cd37ed6d..3bcaf8cc2 100644 --- a/docs/docs/language/builtins.md +++ b/docs/docs/language/builtins.md @@ -2,7 +2,8 @@ title: Builtin Functions --- -Spice offers five builtin functions out of the box. Those can be used anywhere without having to be imported manually and can be used to establish a minimal setup for testing or the like. +Spice offers six builtin functions out of the box. Those can be used anywhere without having to be imported manually and +can be used to establish a minimal setup for testing or the like. ## The `printf` builtin Printf works the same as the `printf` function in C and is designed for printing a string to the standard text output (cout). @@ -98,7 +99,8 @@ len(stringArray); // 5 ``` ## The `panic` builtin -Panic is used to terminate the program with an error message. +Panic is used to terminate the program with an error message. The error message will be printed to stanard error (stderr) +and the program will terminate with exit code `1`. ### Signature `void panic(const Error& error)` diff --git a/docs/docs/language/hello-world.md b/docs/docs/language/hello-world.md index f9dde25ed..1e6e603b5 100644 --- a/docs/docs/language/hello-world.md +++ b/docs/docs/language/hello-world.md @@ -22,9 +22,7 @@ f main() { ## Run your first program Save the file and open a terminal in the directory, where the `hello-world.spice` file lives. Then run the following command: -`spice build hello-world.spice`. As of now, the current directory should contain an executable file called `hello-world` (Linux) -or `hello-world.exe` (Windows).
-Now run `./hello-world` (Linux) or `.\hello-world.exe` (Windows) in your terminal instance. +`spice run hello-world.spice`. This is what you should see: ```shell @@ -32,4 +30,6 @@ $ ./hello-world Hello World! ``` -Congratulations! You just wrote and executed your first Spice program! \ No newline at end of file +Congratulations! You just wrote and executed your first Spice program! + +You can follow the next pages to see, what else Spice can do for you. Feel free to skip to sections of interest to you. diff --git a/docs/docs/language/main-function.md b/docs/docs/language/main-function.md index 229f792a2..6f18e245e 100644 --- a/docs/docs/language/main-function.md +++ b/docs/docs/language/main-function.md @@ -5,20 +5,21 @@ title: Main Function The `main` function is the entry point to any Spice program. When the operating system runs your compiled executable, the `main` function is the first function, which is called. Therefore, it is mandatory and must have the signature `int main()` or `int main(int argc, string[] argv)` for retrieving command line arguments. The main function has a return value of type `int`, -which represents the return code / exit code / status code of the executable. +which represents the return / exit / status code of the executable. ## Usage The `main` function can be declared like this: ```spice f main() { - ... // Code to do something + // Do something ... } ``` Like any other [function](functions.md), the `main` function automatically declares the variable `result` of the same type as the -function return type. You then have the option to use either the return statement (e.g.: `return 1;`) or assign a value to the -`result` variable. If you choose the second option, the value of this variable gets returned at the end of the function body. +function return type, which is `int` in this case. You then have the option to use either the return statement +(e.g.: `return 1;`) or assign a value to the `result` variable. If you choose the second option, the function continues +to run and return the assigned value at the end of the function body. Unlike normal functions, the `main` function has `0` as the initial value assigned to the `result` variable for reasons of convenience and to not always have to write `return 0;` or `result = 0;` to exit the program with a positive exit code. @@ -39,4 +40,8 @@ f main(int argc, string[] argv) { } ``` -The parameter `argc` holds the number of passed arguments and the `argv` array contains the values of those arguments. \ No newline at end of file +The parameter `argc` holds the number of passed arguments and the `argv` array contains the values of those arguments. + +!!! info "--no-entry" + If the `--no-entry` flag is passed to the Spice compiler, the main function will not be included in the final output + binary. Also, if provided, this option makes the main function non-mandatory.