Skip to content

Commit ddc0db4

Browse files
committed
Polish basic syntax docs
1 parent 8a2af0c commit ddc0db4

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

docs/guide/language/basic.mdx

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Badge } from 'rspress/theme';
22

33
# Basic Syntax
44

5-
AIScript syntax is inspired by Python, JavaScript and Rust. If you familiar one of three languages, you can grasp the basic language syntax in 10 minutes.
5+
AIScript syntax is inspired by Python, JavaScript and Rust. If you're familiar with any of these languages, you can grasp the basic language syntax in about 10 minutes.
66

77
## Comments
88

9-
AIScript use `//` as the comment symbol.
9+
AIScript uses `//` for comments.
1010

1111
```js
1212
// This is a comment
@@ -17,7 +17,7 @@ print("Hello World");
1717

1818
## Semicolon
1919

20-
AIScript is semnicolon end language, that mean the `;` is required at the end of statements.
20+
AIScript is a semicolon-terminated language, which means `;` is required at the end of statements.
2121

2222
```js
2323
let a = 1;
@@ -28,7 +28,7 @@ print(1 + 2);
2828

2929
## Variables
3030

31-
AIScript use `let` to define a variable, however type annotation is not support yet.
31+
AIScript uses `let` to define variables. Type annotations are not supported yet.
3232

3333
```js
3434
let name = "AIScript";
@@ -37,7 +37,7 @@ let age = 18;
3737
// let flag: bool = false;
3838
```
3939

40-
There are two kinds of variable: `global variable` and `local variable`. Any variable defined in the root scope is global variable, otherwise it is local scope variable.
40+
There are two kinds of variables: `global variables` and `local variables`. Any variable defined in the root scope is a global variable; otherwise, it's a local scope variable.
4141

4242
```js
4343
let global_variable = "abc";
@@ -47,14 +47,15 @@ if len(global_variable) > 1 {
4747
print("I can access global variable:", global_variable); // print: I can access global variable: abc
4848
}
4949

50-
// local variable is inaccessible outer of local scope,
51-
// this print statement will raise error: Undefined variable 'local_variable'.
50+
// Local variables are inaccessible outside of their local scope.
51+
// This print statement will raise error: Undefined variable 'local_variable'.
5252
// print(local_variable);
5353
```
5454

5555
## Constants
5656

57-
Constants are declared with the `const` keyword, and they are immutable. Constants can only be declared once and must be initialized with a value, they cannot be reassigned.
57+
Constants are declared with the `const` keyword and are immutable. Constants can only be declared once and must be initialized with a value; they cannot be reassigned.
58+
5859

5960
```js
6061
const PI = 3.14;
@@ -94,10 +95,10 @@ let numbers = [1, 2, 3, 4, 5];
9495
let address_key = "address";
9596
let person = {
9697
"name": "AIScript",
97-
age: 18, // double quotes is optional
98+
age: 18, // double quotes are optional
9899
"is_male": true,
99100
"hobbies": ["reading", "coding", "gaming"],
100-
[address_key]: { // key name is computable
101+
[address_key]: { // key name are computable
101102
"city": "Beijing",
102103
"street": "No. 100, Xihuan Road",
103104
"zipcode": "100000",
@@ -154,7 +155,7 @@ let b = 1 not in [1, 2, 3];
154155
```
155156

156157
:::danger
157-
AIScript doesn't support `i++`, `++i`, `i--` or `--i`!
158+
AIScript doesn't support increment/decrement operators like `i++`, `++i`, `i--` or `--i`.
158159
:::
159160

160161
## String
@@ -191,7 +192,7 @@ if age > 60 {
191192

192193
## Inline if
193194

194-
AIScript doesn't support `?` ternary operator like other languages, however you can use inline if syntax.
195+
AIScript doesn't support the `?:` ternary operator like other languages, but you can use `inline if` syntax.
195196

196197
```js
197198
let score = 92;
@@ -201,11 +202,11 @@ print(result); // "A"
201202

202203
## Match
203204

204-
Use `match` to evaluate the expression whether meet the arms.
205+
Use `match` to evaluate expressions against different patterns:
205206

206-
- `match` also support evaluate enum, learn more in [Enum chapter](/guide/language/enum-match)
207+
- `match` also support evaluate enums, learn more in [Enum chapter](/guide/language/enum-match)
207208
- Use `|` to combine multipe case in one arm
208-
- `match` arm support `if guard`
209+
- `match` arm support `if` guards
209210

210211
```js
211212
let language = "AIScript";
@@ -221,9 +222,9 @@ match language {
221222

222223
## Functions
223224

224-
- Use `fn` keyword to define function, you can use `ai fn` to define [AI function](/guide/ai/function).
225-
- Argument and return type annotation is recommend but can be optional.
226-
- Use `|` to append error type after return type, see [Error handling](/guide/language/error-handling).
225+
- Use `fn` keyword to define function; you can use `ai fn` to define [AI function](/guide/ai/function).
226+
- Argument and return type annotations are recommend but optional.
227+
- Use `|` to append error types after return type, see [Error handling](/guide/language/error-handling).
227228

228229
```js
229230
fn add(a: int, b: int) -> int {
@@ -235,22 +236,22 @@ print(add(1, 2)); // 3
235236

236237
## Closures
237238

238-
Function is the first-class citizen in AIScript, hence you can return a function as closure. The biggest different closure compare to function is `closure will capture variable`.
239+
Functions are first-class citizens in AIScript, so you can return a function as a closure. The key difference between closures and regular functions is that `closures capture variables` from their surrounding scope.
239240

240241
```js
241-
fn add(a: int) -> fn {
242-
fn _add(b: int) -> int {
242+
fn add(a: int) {
243+
fn add_inner(b: int) -> int {
243244
return a + b;
244245
}
245246

246-
return _add;
247+
return add_inner;
247248
}
248249

249250
let add5 = add(5);
250251
print(add5(10)); // 15
251252
```
252253

253-
Closure `_add` captured outer variable `a`.
254+
Closure `add_inner` captures the outer variable `a`.
254255

255256
## Slice <Badge text="Not supported yet" type="warning" />
256257

@@ -272,18 +273,31 @@ let d = a[:2]; // "AI"
272273

273274
## Loops
274275

275-
AIScript only has two keyword for loops, `for` and `while`.
276+
AIScript only has two keyword for loops: `for` and `while`.
276277

277278
### Regular for iteration
278279

279280
```js
280281
for let i = 0; i < 10; i = i + 1 {
281282
print(i);
282283
}
284+
```
285+
286+
### While loop
287+
288+
:::info
289+
AIScript doesn't support `do...while`.
290+
:::
283291

284-
while (let i = 0; i < 10; i++>) {
292+
```js
293+
let i = 0;
294+
while i < 10 {
285295
print(i);
286296
}
297+
298+
while true {
299+
// infinite loop
300+
}
287301
```
288302

289303
### Iterate over a list <Badge text="Not supported yet" type="warning" />

0 commit comments

Comments
 (0)