Skip to content

Commit

Permalink
Update and tweak docs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Feb 12, 2025
1 parent a124c9b commit 3161002
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
10 changes: 7 additions & 3 deletions docs/docs/how-to/web-assembly.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public f<int> 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<int> 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: <br>
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/language/builtin-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions docs/docs/language/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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)`
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/language/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ f<int> 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). <br>
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
$ ./hello-world
Hello World!
```

Congratulations! You just wrote and executed your first Spice program!
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.
15 changes: 10 additions & 5 deletions docs/docs/language/main-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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.

Expand All @@ -39,4 +40,8 @@ f<int> main(int argc, string[] argv) {
}
```

The parameter `argc` holds the number of passed arguments and the `argv` array contains the values of those arguments.
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.

0 comments on commit 3161002

Please sign in to comment.