Update External Libraries
@@ -10,7 +10,7 @@ Our ground syntax will be `call !Example_GreetUser $username &greet`
|
|||||||
## C code
|
## C code
|
||||||
First, import `groundext.h`. Then, start a new function. For our example, we will call it `greetUser`.
|
First, import `groundext.h`. Then, start a new function. For our example, we will call it `greetUser`.
|
||||||
```c
|
```c
|
||||||
#include "groundext.h"
|
#include <groundext.h>
|
||||||
|
|
||||||
GroundValue greetUser(GroundScope* scope, List args) {
|
GroundValue greetUser(GroundScope* scope, List args) {
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ GroundValue greetUser(GroundScope* scope, List args) {
|
|||||||
To create our function, we need to access our arguments. Use `args.values[idx]` for an argument. `args.values[0]` is the first argument, `args.values[1]` is the second argument, etc.
|
To create our function, we need to access our arguments. Use `args.values[idx]` for an argument. `args.values[0]` is the first argument, `args.values[1]` is the second argument, etc.
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include "groundext.h"
|
#include <groundext.h>
|
||||||
|
|
||||||
GroundValue greetUser(GroundScope* scope, List args) {
|
GroundValue greetUser(GroundScope* scope, List args) {
|
||||||
const char* argument = args.values[0].data.stringVal;
|
const char* argument = args.values[0].data.stringVal;
|
||||||
@@ -30,7 +30,7 @@ The function expects us to return a `GroundValue`. After we create our string, w
|
|||||||
All together, we can write our function like this:
|
All together, we can write our function like this:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include "groundext.h"
|
#include <groundext.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ void ground_init(GroundScope* scope) {
|
|||||||
```
|
```
|
||||||
For our example, it would look something like this:
|
For our example, it would look something like this:
|
||||||
```c
|
```c
|
||||||
#include "groundext.h"
|
#include <groundext.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -89,4 +89,14 @@ println $out # Prints "Hello, DiamondNether90!"
|
|||||||
|
|
||||||
## Best practices
|
## Best practices
|
||||||
- Namespacing is done with an underscore (`libName_FunctionName`).
|
- Namespacing is done with an underscore (`libName_FunctionName`).
|
||||||
- Use camelCase for the library name and PascalCase for the function name.
|
- Use camelCase for the library name and PascalCase for the function name.
|
||||||
|
|
||||||
|
## Throwing Errors
|
||||||
|
|
||||||
|
If your library encounters an error during execution, you can throw an error with the ERROR() macro. The syntax is this:
|
||||||
|
|
||||||
|
```c
|
||||||
|
ERROR("Friendly description of how the error happened", "ErrorCode");
|
||||||
|
```
|
||||||
|
|
||||||
|
The `ErrorCode` is what users of your library will be able to catch errors with. If the error is not caught, the friendly description will be shown to the user.
|
||||||
Reference in New Issue
Block a user