add documentation for function imports

This commit is contained in:
Moritz Hölting 2024-06-12 18:12:54 +02:00
parent 6cbe6a3c15
commit b05732abe7
6 changed files with 1220 additions and 881 deletions

View File

@ -1,5 +1,5 @@
{
"name": "",
"name": "shulkerscript-docs",
"type": "module",
"version": "0.0.1",
"scripts": {
@ -11,10 +11,10 @@
},
"dependencies": {
"@astrojs/check": "^0.5.10",
"@astrojs/starlight": "^0.21.3",
"astro": "^4.3.5",
"sharp": "^0.32.5",
"@astrojs/starlight": "^0.24.2",
"astro": "^4.10.2",
"sharp": "^0.32.6",
"starlight-links-validator": "^0.7.1",
"typescript": "^5.4.3"
"typescript": "^5.4.5"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -55,6 +55,10 @@ The project structure should look like this:
- pack.png
</FileTree>
:::note
All files in the `src` directory with the `.shu` extension will be processed and neccessary functions generated.
:::
## Writing your first script
After opening the file `src/main.shu` in your favorite text editor, you should see the following content:
```shulkerscript title="src/main.shu"

View File

@ -80,6 +80,37 @@ fn hello() {
}
```
## Imports
Functions from other files can be imported by using the `from`-`import` syntax.
```shulkerscript title="src/main.shu"
namespace "foo";
from "./foo" import bar;
#[load]
fn main() {
bar();
}
```
```shulkerscript title="src/foo.shu"
namespace "foo";
pub fn bar() {
/say Hello, world!
}
```
:::caution[Important]
Notice the `pub` keyword in front of the function `bar`. This is required to make the function accessible from other files.
:::
Multiple functions can be imported by separating them with a comma.
```shulkerscript
from "./foo" import bar, baz;
```
## Conditional Statements
Conditional statements are used to execute code based on a condition.
They start with `if` followed by a condition in parenthesis and a block of code.

View File

@ -14,13 +14,12 @@ hero:
import { Steps } from '@astrojs/starlight/components';
<Steps>
1. **Multi-File Support**\
Allow users to split their code into multiple files and import them into each other.
This will work something like this:
1. **Enhanced Multi-File Support**\
Allow users to import all public functions from other files without specifying each name.
This will work something this:
```shulkerscript
from "path/to/file.shu" import *;
```
And will import all the public (`pub`) functions and variables from the file.
2. **Better Error Reports**\
Improve the error reports to be more helpful and easier to understand.
3. **Tag support**\

View File

@ -18,6 +18,9 @@ export const shulkerscriptGrammar = {
{
include: "#functionAnnotation",
},
{
include: "#importStatement",
}
],
repository: {
// Groupings
@ -53,17 +56,20 @@ export const shulkerscriptGrammar = {
match: "///.*$",
},
namespaceKeyword: {
name: "keyword.control.namespace.shulkerscript",
name: "keyword.other.namespace.shulkerscript",
match: "\\bnamespace\\b",
},
functionDeclaration: {
begin: "^\\s*(fn)\\s+(\\w+)\\(\\s*\\)\\s*{",
begin: "^\\s*(pub\\s)?(fn)\\s+(\\w+)\\(\\s*\\)\\s*{",
end: "}",
captures: {
1: {
name: "keyword.control.function.shulkerscript",
name: "keyword.control.public.shulkerscript"
},
2: {
name: "keyword.control.function.shulkerscript",
},
3: {
name: "entity.name.function.shulkerscript",
},
},
@ -156,5 +162,16 @@ export const shulkerscriptGrammar = {
},
patterns: [{ include: "#functionContents" }],
},
importStatement: {
name: "keyword.import.other.shulkerscript",
begin: "from\\s",
end: "import\\s(\\w+(?:\\s?,\\s?(?:\\w+))*)\\s?,?\\s?",
captures: {
1: {
name: "entity.name.function.shulkerscript"
},
},
patterns: [{ include: "#stringLiteral" }]
}
},
};