Skip to content

Commit

Permalink
chore(docs): update README and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Dec 3, 2023
1 parent cb464a5 commit 2130e17
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 129 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ $ make install

After installed, you can execute these commands below:

```
```bash
melang -v //show version
melang -h //help information
melang a.mln b.mln ... //execute melang files
```

**Example**


### Docker Image

If you need to quickly start a Melang runtime environment, you can use this Docker image.

```bash
docker pull melonc/melon
```



### **Example**

```
//example.m
Expand All @@ -59,11 +72,13 @@ Hello World!
```



### Applications

[Meproc](https://github.com/MelonCTech/Meproc): a cross-platform process management and supervision service.



### License

[BSD-3-Clause License](https://github.com/Water-Melon/Melang/blob/master/LICENSE)
Expand Down
12 changes: 6 additions & 6 deletions docs/coroutine.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Coroutine in Melang is time-sharing scheduled in a single thread.

If you use the execution file which is generated by the repository on Github, script files in a same melang command will be executed in one process one thread.
If you use the execution file which is generated by the repository on Github, script files in a same melang command will be executed in one process or even one thread.

e.g.

Expand All @@ -13,15 +13,15 @@ $ ./melang a.mln b.mln ...



Besides this way, there is a function named *eval* to start a new script task in the current thread to execute a piece of code.
Besides this way, there is a function named `Eval` to start a new script task in the current thread to execute a piece of code.

```
Eval(val, data, in_string, alias);
```

If `in_string` is true, `val` is the script code, otherwise `val` is the script file path.
If `in_string` is true, `val` will be the script code, otherwise `val` is the script file path.

`data` will be passed to the new script task. If we want to use `data` in new task, we can use the variable named `EVAL_DATA` which is an internal variable added by `Eval`.
`data` will be passed to the new script task. If we want to use `data` in new task, we can use the variable named `EVAL_DATA` which is a built-in variable added by `Eval`.

`data` not support all data types, it just supports:

Expand Down Expand Up @@ -83,7 +83,7 @@ Here are two comprehensive examples of HTTP server. There are some functions whi



There are two files: *server.mln* and *worker.mln*.
There are two files: `server.mln` and `worker.mln`.

Example 1.

Expand Down Expand Up @@ -116,7 +116,7 @@ net.tcp_close(fd);
sys.print('quit');
```

Now, you can use *ab (apache bench)* to test.
Now, you can use `ab (apache bench)` to test.



Expand Down
29 changes: 25 additions & 4 deletions docs/datatype.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ But every value has its type. Types in Melang are shown below:
- array
- set
- object
- function



Expand Down Expand Up @@ -43,10 +44,30 @@ d = ['age': 18, 'name': 'Mr. M'];
human {
name;
age;
@run () { // this is a method definition.
//...
}
}
//value of o is an object or instance of set human.
o = $human;
//access object properties
o.name;
o.age = 1;
//accessing non-existent object properties
o.gender // nil will be given
o.phone_number = 123; // the value of the new property `phone_number` has been set to 123.
//here is a function definition
@foo()
{
//...
}
// and here is a function call
foo();
```


Expand All @@ -61,9 +82,9 @@ a = [1, 2, 3];
a[0]; //array[index]
```

*a[0]* is getting the first element *1* in array *a*. And *a[2]* is the last element *3*.
`a[0]` retrieves the first element `1` from the array `a`, while `a[2]` retrieves the last element `3`.

If the index is greater than or equal to array length, *nil* will be got.
If the index is greater than or equal to the array length, `nil` will be given.



Expand All @@ -73,10 +94,10 @@ There is another kind of array named *dict*.
d = ['name': 'Tom', 'age': 18];
```

Now if we want to access the element which *key* is *name*, we can do it in this way:
Now if we want to access the element which *key* is `name`, we can do it in this way:

```
d['name']; //dict[key]
```

If *key* is not in this dict, *nil* will be got.
If *key* is not in this dict, `nil` will be given.
78 changes: 38 additions & 40 deletions docs/flowcontrol.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ if (conditions) {
} fi
```

*conditions* is one or more expressions.
`conditions` is one or more expressions.

The boolean value of *conditions* is not only including *true* and *false*, but also including any other values. So there are some values will be treated as *false*:
The boolean value of `conditions` is not only including `true` and `false`, but also including any other values. So there are some values will be treated as `false`:

- nil
- 0
- '' (null string)
- [] (empty array)
- `nil`
- `0`
- `''` (null string)
- `[]` (empty array)

In mode A: if conditions are satisfied (boolean value is *true*), interpreter will execute statements in *if* block.
In mode A: if conditions are satisfied (boolean value is *true*), interpreter will execute statements in `if` block.

In mode B: if conditions are satisfied, interpreter will execute statements in *if* block, otherwise *else* block statements will be executed.
In mode B: if conditions are satisfied, interpreter will execute statements in `if` block, otherwise `else` block statements will be executed.

> if there is only one statement in *if* or *else* block, the curly brackets around statement can be omitted.
>
> In mode B, the last *fi* can be omitted.
> In mode B, there is no *fi* at the end of the statement block.
There is a special case -- if - else if, e.g.
There is a special case -- `if` - `else if`, e.g.

```
if (a == 1)
Expand All @@ -58,7 +58,7 @@ else if (a == 10)
fi
```

Actually it just a combination of mode B and mode A. The last *if* statement is ONE statement, so we can omit curly brackets.
Actually it just a combination of mode B and mode A. The last `if` is a single statement, so we can omit curly brackets.

e.g.

Expand Down Expand Up @@ -124,11 +124,11 @@ for (expr1; expr2; expr3) {

Interpreter will follow these steps to execute:

1. execute *expr1* (expression).
2. execute *expr2* and test its boolean value. if *true*, go to step3, otherwise go to step5.
3. executed statements in *for* block.
4. executed *expr3*, and go to step 2.
5. *for* statement finished.
1. execute `expr1` (expression).
2. execute `expr2` and check its boolean value. if it is *truthy*, go to step3, otherwise go to step5.
3. executed statements in `for` block.
4. executed `expr3`, and go to step 2.
5. `for` statement finished.

> If there is only one statement in *for* block, the curly brackets around statement can be omitted.
Expand All @@ -154,13 +154,13 @@ while (conditions) {
}
```

*conditions* is one or more expressions.
`conditions` is one or more expressions.

Interpreter will follow these steps to execute:

1. test *conditions*, if its boolean value is *true*, go to step 2, otherwise go to step 3.
2. execute statements in *while* block and then go to step 1.
3. *while* statement finished.
1. test `conditions`, if its boolean value is `true`, go to step 2, otherwise go to step 3.
2. execute statements in `while` block and then go to step 1.
3. `while` statement finished.

> If there is only one statement in *while* block, the curly brackets around statement can be omitted.
Expand All @@ -173,7 +173,7 @@ while (i < 10) {
}
```

####


#### switch

Expand All @@ -193,10 +193,10 @@ switch (expr) {

Interpreter will follow these steps:

1. execute *expr* and record its value in interpreter.
2. use *expr*'s value to match every case *value*. If matched (two values are same), go to step 3, otherwise go to step 2 to keep matching. If no case matched, and there is a *default* case in switch, the *default* will be matched, then go to step3. Otherwise go to step 5.
1. execute `expr` and record its value in interpreter.
2. use `expr`'s value to match every case value. If matched (two values are same), go to step 3, otherwise go to step 2 to keep matching. If no case matched, and there is a `default` case in `switch`, the `default` will be matched, then go to step3. Otherwise go to step 5.
3. executed statements those in matched case.
4. keep executing next case statements but NOT to match case *value*. If there is any one case (including *default*) behind current one, go to step 4.
4. keep executing next case statements but NOT to match case value. If there is any one case (including `default`) behind current one, go to step 4.
5. *switch* statement finished.

e.g.
Expand All @@ -215,7 +215,7 @@ switch (i) {
}
```

*break* statement which will be talked soon makes interpreter stop running in *switch*.
`break` statement which will be talked soon makes interpreter stop running in `switch`.



Expand All @@ -227,12 +227,12 @@ Syntax:
goto label;
```

*goto* tells interpreter to go to a specified position marked by *label* and keep executing from the position.
The `goto` statement tells the interpreter to jump to the specified position marked by the `label`, and continue execution from that position.

e.g.

```
sys = Import('sys');
sys = Import('sys');
i = 10;
goto plus;
Expand All @@ -242,9 +242,9 @@ plus:
sys.print(i);
```

*plus* is a label. The output of this code is 12 which means *i--;* is not executed.
*plus* is a label. The output of this code is `12` which means `i--;` is not executed.

> `goto` in Melang is a simplified.
> `goto` in Melang is simplified.
```
@foo()
Expand All @@ -262,7 +262,7 @@ l1:
foo();
```

The interpreter will throw an error that `l1` can not be found, because it is not in the outest statements in a function.
The interpreter will throw an error that `l1` can not be found, because it is not in the outest statements in a function. The next example is working correctly.

```
@foo()
Expand All @@ -280,13 +280,11 @@ li:
foo();
```

But this example is working correctlly.



#### break

*break* is a single statement. It is used to interrupt current loop (including *switch*), and jump out of the loop.
`break` is a single statement. It is used to interrupt current loop (including `switch`), and jump out of the loop.

e.g.

Expand All @@ -301,17 +299,17 @@ for (i = 0; i < 1000; i++) {
sys.print(i);
```

The output is 11, because when *i* greater than 10, *if* condition is matched. Then *break* statement makes *for* loop stop and go to execute next statement (*sys.print(i);*).
The output is `11`, because when `i` greater than `10`, `if` condition is matched. Then `break` statement makes `for` loop stop and go to execute next statement (`sys.print(i);`).



#### continue

*continue* statement is also used in a loop (**except** *switch*).
`continue` statement is also used in a loop (**except** `switch`).

When interpreter encounter *continue* statement, the rest statements those behind *continue*, will not be executed, and go back to the beginnig of loop statements.
When interpreter encounter `continue` statement, the rest statements those behind `continue`, will not be executed, and go back to the beginnig of loop statements.

If *continue* is encountered in a *for* loop, *expr3* and *expr2* will be executed and then back to the beginning of loop statements or finish loop (boolean value of *expr2* is *false*).
If `continue` is encountered in a `for` loop, `expr3` and `expr2` will be executed and then back to the beginning of loop statements or finish loop (boolean value of `expr2` is `false`).

e.g.

Expand All @@ -333,11 +331,11 @@ Syntax:
return value;
```

Usually, return is used in a function. It will stop interpreter executing rest statement and return a value to the caller (outer scope).
Usually, `return` is used in a function. It will stop interpreter executing rest statement and return a value to the caller (outer scope).

*value* can be omitted, then the return value will be *nil*.
`value` can be omitted, then the return value will be `nil`.

If *return* appears in the outest scope, interpreter will stop execution, and this script task will be removed.
If `return` appears in the outest scope, interpreter will stop execution, and this script task will be removed.

e.g.

Expand Down
Loading

0 comments on commit 2130e17

Please sign in to comment.