📝 update documents
This commit is contained in:
parent
2f58a7c223
commit
c946e9debd
317
README.md
317
README.md
|
@ -25,7 +25,7 @@
|
|||
|
||||
__Contact us if having great ideas to share!__
|
||||
|
||||
* __E-mail__: __lhk101lhk101@qq.com__
|
||||
* __E-mail__: __lhk101lhk101@qq.com__(ValKmjolnir) __1467329765@qq.com__(Sidi762)
|
||||
|
||||
## __Introduction__
|
||||
|
||||
|
@ -97,8 +97,9 @@ You could choose which compiler you want to use:
|
|||
If your system is `Windows` and you want to output unicode, you could write this in nasal code:
|
||||
|
||||
```javascript
|
||||
if(os.platform()=="windows")
|
||||
if (os.platform()=="windows") {
|
||||
system("chcp 65001");
|
||||
}
|
||||
```
|
||||
|
||||
## __Tutorial__
|
||||
|
@ -183,15 +184,16 @@ var f=func(x,y,z,deft=1){
|
|||
}
|
||||
var f = func(args...) {
|
||||
var sum = 0;
|
||||
foreach(var i;args)
|
||||
foreach(var i; args) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
|
||||
__`upval`__ is used to store upvalues, used in __`vm`__ to make sure closure runs correctly.
|
||||
|
||||
__`obj`__ is used to store other complex `C/C++` data types.
|
||||
__`ghost`__ is used to store other complex `C/C++` data types.
|
||||
This type is created by native-function of nasal. If want to define a new data type, see how to add native-functions by editing code.
|
||||
|
||||
</details>
|
||||
|
@ -323,10 +325,12 @@ if(1){
|
|||
While loop and for loop is simalar to C/C++.
|
||||
|
||||
```javascript
|
||||
while(condition)
|
||||
while(condition) {
|
||||
continue;
|
||||
for(var i=0;i<10;i+=1)
|
||||
}
|
||||
for(var i = 0; i<10; i += 1) {
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
Nasal has another two kinds of loops that iterates through a vector:
|
||||
|
@ -334,15 +338,17 @@ Nasal has another two kinds of loops that iterates through a vector:
|
|||
`forindex` will get the index of a vector. Index will be `0` to `size(elem)-1`.
|
||||
|
||||
```javascript
|
||||
forindex(var i;elem)
|
||||
forindex(var i; elem) {
|
||||
print(elem[i]);
|
||||
}
|
||||
```
|
||||
|
||||
`foreach` will get the element of a vector. Element will be `elem[0]` to `elem[size(elem)-1]`.
|
||||
|
||||
```javascript
|
||||
foreach(var i;elem)
|
||||
foreach(var i; elem) {
|
||||
print(i);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -532,31 +538,17 @@ Definition:
|
|||
|
||||
```C++
|
||||
// you could also use a macro to define one.
|
||||
nas_native(builtin_print);
|
||||
var builtin_print(context*, gc*);
|
||||
```
|
||||
|
||||
Then complete this function using C++:
|
||||
|
||||
```C++
|
||||
var builtin_print(var* local,gc& ngc)
|
||||
{
|
||||
var builtin_print(context* ctx, gc* ngc) {
|
||||
// find value with index begin from 1
|
||||
// because local[0] is reserved for value 'me'
|
||||
var vec=local[1];
|
||||
// main process
|
||||
// also check number of arguments and type here
|
||||
// if get an error,use nas_err
|
||||
for(auto& i:vec.vec().elems)
|
||||
switch(i.type)
|
||||
{
|
||||
case vm_none: std::cout<<"undefined"; break;
|
||||
case vm_nil: std::cout<<"nil"; break;
|
||||
case vm_num: std::cout<<i.num(); break;
|
||||
case vm_str: std::cout<<i.str(); break;
|
||||
case vm_vec: std::cout<<i.vec(); break;
|
||||
case vm_hash: std::cout<<i.hash(); break;
|
||||
case vm_func: std::cout<<"func(..){..}";break;
|
||||
case vm_obj: std::cout<<"<object>"; break;
|
||||
for(auto& i : ctx->localr[1].vec().elems) {
|
||||
std::cout << i;
|
||||
}
|
||||
std::cout << std::flush;
|
||||
// generate return value,
|
||||
|
@ -572,17 +564,24 @@ The value got before will be collected, but stil in use in this builtin function
|
|||
So use `gc::temp` in builtin functions to temprorarily store the gc-managed value that you want to return later. Like this:
|
||||
|
||||
```C++
|
||||
var builtin_keys(var* local,gc& ngc)
|
||||
{
|
||||
var hash=local[1];
|
||||
if(hash.type!=vm_hash)
|
||||
var builtin_keys(context* ctx, gc* ngc) {
|
||||
auto hash = ctx->localr[1];
|
||||
if (hash.type!=vm_hash && hash.type!=vm_map) {
|
||||
return nas_err("keys", "\"hash\" must be hash");
|
||||
}
|
||||
// use gc.temp to store the gc-managed-value, to avoid being sweeped
|
||||
var res=ngc.temp=ngc.alloc(vm_vec);
|
||||
auto res = ngc->temp = ngc->alloc(vm_vec);
|
||||
auto& vec = res.vec().elems;
|
||||
for(auto& iter:hash.hash().elems)
|
||||
vec.push_back(ngc.newstr(iter.first));
|
||||
ngc.temp=nil;
|
||||
if (hash.type==vm_hash) {
|
||||
for(const auto& iter : hash.hash().elems) {
|
||||
vec.push_back(ngc->newstr(iter.first));
|
||||
}
|
||||
} else {
|
||||
for(const auto& iter : hash.map().mapper) {
|
||||
vec.push_back(ngc->newstr(iter.first));
|
||||
}
|
||||
}
|
||||
ngc->temp = nil;
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
@ -590,12 +589,7 @@ var builtin_keys(var* local,gc& ngc)
|
|||
After that, register the built-in function's name(in nasal) and the function's pointer in this table:
|
||||
|
||||
```C++
|
||||
struct func
|
||||
{
|
||||
const char* name;
|
||||
var (*func)(var*,gc&);
|
||||
} builtin[]=
|
||||
{
|
||||
nasal_builtin_table builtin[] = {
|
||||
{"__print", builtin_print},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
@ -812,19 +806,31 @@ void ghost_for_test_destructor(void* ptr) {
|
|||
var create_new_ghost(var* args, usize size, gc* ngc) {
|
||||
var res = ngc->alloc(vm_obj);
|
||||
// create ghost type
|
||||
res.obj().set(ghost_for_test, ghost_for_test_destructor, new u32);
|
||||
res.ghost().set(ghost_for_test, ghost_for_test_destructor, new u32);
|
||||
return res;
|
||||
}
|
||||
|
||||
var set_new_ghost(var* args, usize size, gc* ngc) {
|
||||
var res = args[0];
|
||||
if (!res.object_check(ghost_for_test)) {
|
||||
std::cout << "set_new_ghost: not ghost for test type.\n";
|
||||
return nil;
|
||||
}
|
||||
f64 num = args[1].num();
|
||||
*(reinterpret_cast<u32*>(res.ghost().pointer)) = static_cast<u32>(num);
|
||||
std::cout << "set_new_ghost: successfully set ghost = " << num << "\n";
|
||||
return nil;
|
||||
}
|
||||
|
||||
var print_new_ghost(var* args, usize size, gc* ngc) {
|
||||
var res = args[0];
|
||||
// check ghost type by the type name
|
||||
if (!res.objchk(ghost_for_test)) {
|
||||
if (!res.object_check(ghost_for_test)) {
|
||||
std::cout << "print_new_ghost: not ghost for test type.\n";
|
||||
return nil;
|
||||
}
|
||||
std::cout << "print_new_ghost: " << res.obj() << " result = "
|
||||
<< *((u32*)res.obj().ptr) << "\n";
|
||||
std::cout << "print_new_ghost: " << res.ghost() << " result = "
|
||||
<< *((u32*)res.ghost().pointer) << "\n";
|
||||
return nil;
|
||||
}
|
||||
```
|
||||
|
@ -841,7 +847,7 @@ We use this function to create a new ghost type:
|
|||
|
||||
And we use this function to check if value is the correct ghost type:
|
||||
|
||||
`bool var::objchk(const std::string&);`
|
||||
`bool var::object_check(const std::string&);`
|
||||
|
||||
The parameter is the name of the ghost type.
|
||||
|
||||
|
@ -901,8 +907,7 @@ it will print trace back information:
|
|||
Function `die` is used to throw error and crash immediately.
|
||||
|
||||
```javascript
|
||||
func()
|
||||
{
|
||||
func() {
|
||||
println("hello");
|
||||
die("error occurred this line");
|
||||
return;
|
||||
|
@ -912,17 +917,27 @@ func()
|
|||
```javascript
|
||||
hello
|
||||
[vm] error: error occurred this line
|
||||
[vm] native function error.
|
||||
trace back:
|
||||
0x000000ac 40 00 00 00 25 callb 0x25 <__die@0x41afc0> (lib.nas:131)
|
||||
0x000004f6 3e 00 00 00 01 callfv 0x1 (a.nas:4)
|
||||
0x000004fa 3e 00 00 00 00 callfv 0x0 (a.nas:6)
|
||||
vm stack (0x7fffcd21bc68 <sp+80>, limit 10, total 12):
|
||||
0x0000005b | null |
|
||||
...
|
||||
0x00000057 | str | <0x138ff60> error occurred t...
|
||||
...
|
||||
0x00000052 | nil |
|
||||
[vm] error: error occurred in native function
|
||||
|
||||
call trace (main)
|
||||
call func@0x557513935710() {entry: 0x850}
|
||||
|
||||
trace back (main)
|
||||
0x000547 4c 00 00 16 callb 0x16 <__die@0x557512441780>(std/lib.nas:150)
|
||||
0x000856 4a 00 00 01 callfv 0x1(a.nas:3)
|
||||
0x00085a 4a 00 00 00 callfv 0x0(a.nas:5)
|
||||
|
||||
stack (0x5575138e8c40, limit 10, total 14)
|
||||
0x00000d | null |
|
||||
0x00000c | pc | 0x856
|
||||
0x00000b | addr | 0x5575138e8c50
|
||||
0x00000a | nil |
|
||||
0x000009 | nil |
|
||||
0x000008 | str | <0x5575138d9190> error occurred t...
|
||||
0x000007 | nil |
|
||||
0x000006 | func | <0x5575139356f0> entry:0x850
|
||||
0x000005 | pc | 0x85a
|
||||
0x000004 | addr | 0x0
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -942,18 +957,31 @@ func(f){
|
|||
```
|
||||
|
||||
```javascript
|
||||
[vm] stack overflow
|
||||
trace back:
|
||||
0x000004fb 3e 00 00 00 01 callfv 0x1 (a.nas:5)
|
||||
0x000004fb 1349 same call(s)
|
||||
0x000004f3 3e 00 00 00 01 callfv 0x1 (a.nas:2)
|
||||
0x000004ff 3e 00 00 00 01 callfv 0x1 (a.nas:3)
|
||||
vm stack (0x7fffd3781d58 <sp+80>, limit 10, total 8108):
|
||||
0x00001ffb | func | <0x15f8d90> entry:0x4f9
|
||||
0x00001ffa | func | <0x15f8d90> entry:0x4f9
|
||||
0x00001ff9 | pc | 0x4fb
|
||||
...
|
||||
0x00001ff2 | addr | 0x7fffd37a16e8
|
||||
[vm] error: stack overflow
|
||||
|
||||
call trace (main)
|
||||
call func@0x564106058620(f) {entry: 0x859}
|
||||
--> 583 same call(s)
|
||||
call func@0x5641060586c0(f) {entry: 0x851}
|
||||
|
||||
trace back (main)
|
||||
0x000859 45 00 00 01 calll 0x1(a.nas:5)
|
||||
0x00085b 4a 00 00 01 callfv 0x1(a.nas:5)
|
||||
0x00085b 582 same call(s)
|
||||
0x000853 4a 00 00 01 callfv 0x1(a.nas:2)
|
||||
0x00085f 4a 00 00 01 callfv 0x1(a.nas:3)
|
||||
|
||||
stack (0x56410600be00, limit 10, total 4096)
|
||||
0x000fff | func | <0x564106058600> entry:0x859
|
||||
0x000ffe | pc | 0x85b
|
||||
0x000ffd | addr | 0x56410601bd20
|
||||
0x000ffc | nil |
|
||||
0x000ffb | nil |
|
||||
0x000ffa | func | <0x564106058600> entry:0x859
|
||||
0x000ff9 | nil |
|
||||
0x000ff8 | func | <0x564106058600> entry:0x859
|
||||
0x000ff7 | pc | 0x85b
|
||||
0x000ff6 | addr | 0x56410601bcb0
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -969,11 +997,13 @@ func(){
|
|||
```
|
||||
|
||||
```javascript
|
||||
[vm] callv: must call a vector/hash/string
|
||||
trace back:
|
||||
0x000004f4 3b 00 00 00 00 callv 0x0 (a.nas:3)
|
||||
vm stack (0x7fffff539c28 <sp+80>, limit 10, total 1):
|
||||
0x00000050 | num | 0
|
||||
[vm] error: must call a vector/hash/string but get number
|
||||
|
||||
trace back (main)
|
||||
0x000854 47 00 00 00 callv 0x0(a.nas:3)
|
||||
|
||||
stack (0x564993f462b0, limit 10, total 1)
|
||||
0x000000 | num | 0
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -985,35 +1015,48 @@ Use command __`-d`__ or __`--detail`__ the trace back info will show more detail
|
|||
```javascript
|
||||
hello
|
||||
[vm] error: error occurred this line
|
||||
[vm] error: native function error
|
||||
[vm] error: error occurred in native function
|
||||
|
||||
call trace (main)
|
||||
call func@0x55dcb5b8fbf0() {entry: 0x850}
|
||||
|
||||
trace back (main)
|
||||
0x000000b0 40 00 00 00 2b callb 0x2b <__die@0x41c380> (lib.nas:131)
|
||||
0x00000553 3e 00 00 00 01 callfv 0x1 (test.nas:4)
|
||||
0x00000557 3e 00 00 00 00 callfv 0x0 (test.nas:6)
|
||||
vm stack (0x7fffe0ffed90 <sp+63>, limit 10, total 12)
|
||||
0x0000004a | null |
|
||||
0x00000049 | pc | 0x553
|
||||
0x00000048 | addr | 0x7fffe0ffeda0
|
||||
...
|
||||
0x00000041 | nil |
|
||||
0x000547 4c 00 00 16 callb 0x16 <__die@0x55dcb3c41780>(std/lib.nas:150)
|
||||
0x000856 4a 00 00 01 callfv 0x1(a.nas:3)
|
||||
0x00085a 4a 00 00 00 callfv 0x0(a.nas:5)
|
||||
|
||||
stack (0x55dcb5b43120, limit 10, total 14)
|
||||
0x00000d | null |
|
||||
0x00000c | pc | 0x856
|
||||
0x00000b | addr | 0x55dcb5b43130
|
||||
0x00000a | nil |
|
||||
0x000009 | nil |
|
||||
0x000008 | str | <0x55dcb5b33670> error occurred t...
|
||||
0x000007 | nil |
|
||||
0x000006 | func | <0x55dcb5b8fbd0> entry:0x850
|
||||
0x000005 | pc | 0x85a
|
||||
0x000004 | addr | 0x0
|
||||
|
||||
registers (main)
|
||||
[ pc ] | pc | 0xb0
|
||||
[ global ] | addr | 0x7fffe0ffe9a0
|
||||
[ localr ] | addr | 0x7fffe0ffedf0
|
||||
[pc ] | pc | 0x547
|
||||
[global] | addr | 0x55dcb5b53130
|
||||
[local ] | addr | 0x55dcb5b43190
|
||||
[memr ] | addr | 0x0
|
||||
[ canary ] | addr | 0x7fffe1002990
|
||||
[ top ] | addr | 0x7fffe0ffee40
|
||||
[ funcr ] | func | <0x677cd0> entry:0xb0
|
||||
[ upvalr ] | nil |
|
||||
global (0x7fffe0ffe9a0 <sp+0>)
|
||||
0x00000000 | func | <0x65fb00> entry:0x5
|
||||
0x00000001 | func | <0x65fb20> entry:0xd
|
||||
[canary] | addr | 0x55dcb5b53110
|
||||
[top ] | addr | 0x55dcb5b431f0
|
||||
[funcr ] | func | <0x55dcb5b65620> entry:0x547
|
||||
[upval ] | nil |
|
||||
|
||||
global (0x55dcb5b53130)
|
||||
0x000000 | nmspc| <0x55dcb5b33780> namespace [95 val]
|
||||
0x000001 | vec | <0x55dcb5b64c20> [0 val]
|
||||
...
|
||||
0x0000003d | func | <0x66bf00> entry:0x51f
|
||||
0x0000003e | hash | <0x65ffa0> {5 val}
|
||||
local (0x7fffe0ffedf0 <sp+45>)
|
||||
0x00000000 | nil |
|
||||
0x00000001 | str | <0x6cb630> error occurred t...
|
||||
0x00005e | func | <0x55dcb5b8fc70> entry:0x846
|
||||
|
||||
local (0x55dcb5b43190 <+7>)
|
||||
0x000000 | nil |
|
||||
0x000001 | str | <0x55dcb5b33670> error occurred t...
|
||||
0x000002 | nil |
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -1038,16 +1081,18 @@ source code:
|
|||
for(var i=0;i<31;i+=1)
|
||||
print(fib(i),'\n');
|
||||
|
||||
|
||||
next bytecode:
|
||||
--> 0x00000000 01 00 00 00 41 intg 0x41 (test/fib.nas:0)
|
||||
0x00000001 0b 00 00 00 05 newf 0x5 (lib.nas:6)
|
||||
0x00000002 02 00 00 00 02 intl 0x2 (lib.nas:6)
|
||||
0x00000003 0f 00 00 00 00 dyn 0x0 ("elems") (lib.nas:6)
|
||||
0x00000004 32 00 00 00 07 jmp 0x7 (lib.nas:6)
|
||||
0x00000005 40 00 00 00 00 callb 0x0 <__print@0x419c80> (lib.nas:7)
|
||||
0x00000006 4a 00 00 00 00 ret 0x0 (lib.nas:7)
|
||||
0x00000007 03 00 00 00 00 loadg 0x0 (lib.nas:6)
|
||||
vm stack (0x7fffd0259138 <sp+65>, limit 10, total 0)
|
||||
0x000848 4a 00 00 01 callfv 0x1(std/lib.nas:427)
|
||||
0x000849 3d 00 00 00 pop 0x0(std/lib.nas:427)
|
||||
0x00084a 07 00 00 00 pnil 0x0(std/lib.nas:423)
|
||||
0x00084b 56 00 00 00 ret 0x0(std/lib.nas:423)
|
||||
0x00084c 03 00 00 5e loadg 0x5e(std/lib.nas:423)
|
||||
--> 0x00084d 0b 00 08 51 newf 0x851(test/fib.nas:1)
|
||||
0x00084e 02 00 00 03 intl 0x3(test/fib.nas:1)
|
||||
0x00084f 0d 00 00 08 para 0x8 (x)(test/fib.nas:1)
|
||||
|
||||
stack (0x55ccd0a1b9d0, limit 10, total 0)
|
||||
>>
|
||||
```
|
||||
|
||||
|
@ -1070,23 +1115,26 @@ source code:
|
|||
for(var i=0;i<31;i+=1)
|
||||
print(fib(i),'\n');
|
||||
|
||||
|
||||
next bytecode:
|
||||
0x00000548 0c 00 00 00 aa happ 0xaa ("running") (lib.nas:503)
|
||||
0x00000549 03 00 00 00 3e loadg 0x3e (lib.nas:498)
|
||||
0x0000054a 0b 00 00 05 4e newf 0x54e (test/fib.nas:1)
|
||||
0x0000054b 02 00 00 00 02 intl 0x2 (test/fib.nas:1)
|
||||
0x0000054c 0d 00 00 00 1b para 0x1b ("x") (test/fib.nas:1)
|
||||
0x0000054d 32 00 00 05 5d jmp 0x55d (test/fib.nas:1)
|
||||
--> 0x0000054e 39 00 00 00 01 calll 0x1 (test/fib.nas:3)
|
||||
0x0000054f 2d 00 00 00 03 lessc 0x3 (2) (test/fib.nas:3)
|
||||
vm stack (0x7fffd0259138 <sp+65>, limit 10, total 7)
|
||||
0x00000047 | pc | 0x566
|
||||
0x00000046 | addr | 0x0
|
||||
0x00000045 | nil |
|
||||
0x00000044 | num | 0
|
||||
0x00000043 | nil |
|
||||
0x00000042 | nil |
|
||||
0x00000041 | func | <0x88d2f0> entry:0x5
|
||||
0x000850 3e 00 08 60 jmp 0x860(test/fib.nas:1)
|
||||
--> 0x000851 45 00 00 01 calll 0x1(test/fib.nas:3)
|
||||
0x000852 39 00 00 07 lessc 0x7 (2)(test/fib.nas:3)
|
||||
0x000853 40 00 08 56 jf 0x856(test/fib.nas:3)
|
||||
0x000854 45 00 00 01 calll 0x1(test/fib.nas:3)
|
||||
0x000855 56 00 00 00 ret 0x0(test/fib.nas:3)
|
||||
0x000856 44 00 00 5f callg 0x5f(test/fib.nas:4)
|
||||
0x000857 45 00 00 01 calll 0x1(test/fib.nas:4)
|
||||
|
||||
stack (0x55ccd0a1b9d0, limit 10, total 8)
|
||||
0x000007 | pc | 0x869
|
||||
0x000006 | addr | 0x0
|
||||
0x000005 | nil |
|
||||
0x000004 | nil |
|
||||
0x000003 | num | 0
|
||||
0x000002 | nil |
|
||||
0x000001 | nil |
|
||||
0x000000 | func | <0x55ccd0a58fa0> entry:0x487
|
||||
>>
|
||||
```
|
||||
|
||||
|
@ -1114,3 +1162,22 @@ Nasal REPL interpreter version 11.0 (Oct 7 2023 17:28:31)
|
|||
|
||||
>>>
|
||||
```
|
||||
|
||||
Try import `std/json.nas`~
|
||||
|
||||
```bash
|
||||
[nasal-repl] Initializating enviroment...
|
||||
[nasal-repl] Initialization complete.
|
||||
|
||||
Nasal REPL interpreter version 11.1 (Nov 1 2023 23:37:30)
|
||||
.h, .help | show help
|
||||
.e, .exit | quit the REPL
|
||||
.q, .quit | quit the REPL
|
||||
.c, .clear | clear the screen
|
||||
.s, .source | show source code
|
||||
|
||||
>>> use std.json;
|
||||
{stringify:func(..) {..},parse:func(..) {..}}
|
||||
|
||||
>>>
|
||||
```
|
||||
|
|
326
doc/README_zh.md
326
doc/README_zh.md
|
@ -25,7 +25,7 @@
|
|||
|
||||
__如果有好的意见或建议,欢迎联系我们!__
|
||||
|
||||
* __E-mail__: __lhk101lhk101@qq.com__
|
||||
* __E-mail__: __lhk101lhk101@qq.com__(ValKmjolnir) __1467329765@qq.com__(Sidi762)
|
||||
|
||||
## __简介__
|
||||
|
||||
|
@ -88,8 +88,9 @@ __注意__: 如果你想直接下载发行版提供的zip/tar.gz压缩包来构
|
|||
如果你是 `Windows` 用户且想正常输出unicode,在nasal代码里写这个来开启unicode代码页:
|
||||
|
||||
```javascript
|
||||
if(os.platform()=="windows")
|
||||
if (os.platform()=="windows") {
|
||||
system("chcp 65001");
|
||||
}
|
||||
```
|
||||
|
||||
## __教程__
|
||||
|
@ -171,15 +172,16 @@ var f=func(x,y,z,deft=1){
|
|||
}
|
||||
var f = func(args...) {
|
||||
var sum = 0;
|
||||
foreach(var i;args)
|
||||
foreach(var i; args) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
|
||||
__`upval`__ 是存储闭包数据的特殊类型, 在 __`vm`__ 中使用,以确保闭包功能正常。
|
||||
|
||||
__`obj`__ 是用来存储`C/C++`的一些复杂数据结构。这种类型的数据由内置函数生成。如果想为nasal添加新的数据结构, 可以看下文如何通过修改本项目来添加内置函数。
|
||||
__`ghost`__ 是用来存储`C/C++`的一些复杂数据结构。这种类型的数据由内置函数生成。如果想为nasal添加新的数据结构, 可以看下文如何通过修改本项目来添加内置函数。
|
||||
|
||||
</details>
|
||||
|
||||
|
@ -308,10 +310,12 @@ if(1){
|
|||
while循环和for循环大体上与C/C++是一致的。
|
||||
|
||||
```javascript
|
||||
while(condition)
|
||||
while(condition) {
|
||||
continue;
|
||||
for(var i=0;i<10;i+=1)
|
||||
}
|
||||
for(var i = 0; i<10; i += 1) {
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
同时,nasal还有另外两种直接遍历列表的循环方式:
|
||||
|
@ -319,15 +323,17 @@ for(var i=0;i<10;i+=1)
|
|||
`forindex` 会获取列表的下标,依次递增. 下标会从`0`递增到`size(elem)-1`结束。
|
||||
|
||||
```javascript
|
||||
forindex(var i;elem)
|
||||
forindex(var i; elem) {
|
||||
print(elem[i]);
|
||||
}
|
||||
```
|
||||
|
||||
`foreach`会依次直接获取列表中的数据. 这些数据会从`elem[0]`依次获取到`elem[size(elem)-1]`.
|
||||
|
||||
```javascript
|
||||
foreach(var i;elem)
|
||||
foreach(var i; elem) {
|
||||
print(i);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -506,42 +512,27 @@ println(d());
|
|||
|
||||
__警告:__ 如果你 __不想__ 通过直接修改解释器源码来添加你自定义的函数,那么你应该看下一个节 __`模块`__ 的内容。
|
||||
|
||||
如果你确实是想修改源码来搞一个自己私人订制的解释器,那么你可以说:“我他妈就是想自己私人订制,你们他妈的管得着吗”,
|
||||
然后看看源码中关于内置函数的部分,以及`lib.nas`中是如何包装这些函数的,还有下面的样例:
|
||||
如果你确实是想修改源码来搞一个自己私人订制的解释器 ———— “我他妈就是想自己私人订制,你们他妈的管得着吗?”,
|
||||
参考源码中关于内置函数的部分,以及`lib.nas`中是如何包装这些函数的,下面是其中一个样例:
|
||||
|
||||
定义新的内置函数:
|
||||
|
||||
```C++
|
||||
// 你可以使用这个宏来直接定义一个新的内置函数
|
||||
nas_native(builtin_print);
|
||||
var builtin_print(context*, gc*);
|
||||
```
|
||||
|
||||
然后用C++完成这个函数的函数体:
|
||||
|
||||
```C++
|
||||
var builtin_print(var* local,gc& ngc)
|
||||
{
|
||||
var builtin_print(context* ctx, gc* ngc) {
|
||||
// 局部变量的下标其实是从 1 开始的
|
||||
// 因为 local[0] 是保留给 'me' 的空间
|
||||
var vec=local[1];
|
||||
// 主要部分
|
||||
// 一些必要的类型检查和输入合法性检测也要在这里写出
|
||||
// 如果检测到问题,用builtin_err函数来返回vm_null
|
||||
// 并且狠狠地骂那些不好好写代码的混蛋(玩笑)
|
||||
for(auto& i:vec.vec().elems)
|
||||
switch(i.type)
|
||||
{
|
||||
case vm_none: std::cout<<"undefined"; break;
|
||||
case vm_nil: std::cout<<"nil"; break;
|
||||
case vm_num: std::cout<<i.num(); break;
|
||||
case vm_str: std::cout<<i.str(); break;
|
||||
case vm_vec: std::cout<<i.vec(); break;
|
||||
case vm_hash: std::cout<<i.hash(); break;
|
||||
case vm_func: std::cout<<"func(..){..}";break;
|
||||
case vm_obj: std::cout<<"<object>"; break;
|
||||
for(auto& i : ctx->localr[1].vec().elems) {
|
||||
std::cout << i;
|
||||
}
|
||||
std::cout << std::flush;
|
||||
// 最后一定要记得生成返回值,返回值必须是一个内置的类型,
|
||||
// 最后生成返回值,返回值必须是一个内置的类型,
|
||||
// 可以使用ngc::alloc(type)来申请一个需要内存管理的复杂数据结构
|
||||
// 或者用我们已经定义好的nil/one/zero,这些可以直接使用
|
||||
return nil;
|
||||
|
@ -554,17 +545,24 @@ var builtin_print(var* local,gc& ngc)
|
|||
可以使用`gc::temp`来暂时存储一个会被返回的需要gc管理的变量,这样可以防止内部所有的申请错误触发垃圾回收。如下所示:
|
||||
|
||||
```C++
|
||||
var builtin_keys(var* local,gc& ngc)
|
||||
{
|
||||
var hash=local[1];
|
||||
if(hash.type!=vm_hash)
|
||||
var builtin_keys(context* ctx, gc* ngc) {
|
||||
auto hash = ctx->localr[1];
|
||||
if (hash.type!=vm_hash && hash.type!=vm_map) {
|
||||
return nas_err("keys", "\"hash\" must be hash");
|
||||
}
|
||||
// 使用gc.temp来存储gc管理的变量,防止错误的回收
|
||||
var res=ngc.temp=ngc.alloc(vm_vec);
|
||||
auto res = ngc->temp = ngc->alloc(vm_vec);
|
||||
auto& vec = res.vec().elems;
|
||||
for(auto& iter:hash.hash().elems)
|
||||
vec.push_back(ngc.newstr(iter.first));
|
||||
ngc.temp=nil;
|
||||
if (hash.type==vm_hash) {
|
||||
for(const auto& iter : hash.hash().elems) {
|
||||
vec.push_back(ngc->newstr(iter.first));
|
||||
}
|
||||
} else {
|
||||
for(const auto& iter : hash.map().mapper) {
|
||||
vec.push_back(ngc->newstr(iter.first));
|
||||
}
|
||||
}
|
||||
ngc->temp = nil;
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
@ -572,12 +570,7 @@ var builtin_keys(var* local,gc& ngc)
|
|||
这些工作都完成之后,在内置函数注册表中填写它在nasal中的别名,并且在表中填对这个函数的函数指针:
|
||||
|
||||
```C++
|
||||
struct func
|
||||
{
|
||||
const char* name;
|
||||
var (*func)(var*,gc&);
|
||||
} builtin[]=
|
||||
{
|
||||
nasal_builtin_table builtin[] = {
|
||||
{"__print", builtin_print},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
@ -767,7 +760,7 @@ dylib.dlclose(dlhandle.lib);
|
|||
|
||||
<details><summary> 自定义类型(开发者教程) </summary>
|
||||
|
||||
创建一个自定义类型现在不是很困难。下面是使用示例:
|
||||
创建一个自定义类型很容易。下面是使用示例:
|
||||
|
||||
```c++
|
||||
const auto ghost_for_test = "ghost_for_test";
|
||||
|
@ -785,19 +778,31 @@ void ghost_for_test_destructor(void* ptr) {
|
|||
var create_new_ghost(var* args, usize size, gc* ngc) {
|
||||
var res = ngc->alloc(vm_obj);
|
||||
// 创建自定义类型
|
||||
res.obj().set(ghost_for_test, ghost_for_test_destructor, new u32);
|
||||
res.ghost().set(ghost_for_test, ghost_for_test_destructor, new u32);
|
||||
return res;
|
||||
}
|
||||
|
||||
var set_new_ghost(var* args, usize size, gc* ngc) {
|
||||
var res = args[0];
|
||||
if (!res.object_check(ghost_for_test)) {
|
||||
std::cout << "set_new_ghost: not ghost for test type.\n";
|
||||
return nil;
|
||||
}
|
||||
f64 num = args[1].num();
|
||||
*(reinterpret_cast<u32*>(res.ghost().pointer)) = static_cast<u32>(num);
|
||||
std::cout << "set_new_ghost: successfully set ghost = " << num << "\n";
|
||||
return nil;
|
||||
}
|
||||
|
||||
var print_new_ghost(var* args, usize size, gc* ngc) {
|
||||
var res = args[0];
|
||||
// 用自定义类型的名字来检查是否是正确的自定义类型
|
||||
if (!res.objchk(ghost_for_test)) {
|
||||
if (!res.object_check(ghost_for_test)) {
|
||||
std::cout << "print_new_ghost: not ghost for test type.\n";
|
||||
return nil;
|
||||
}
|
||||
std::cout << "print_new_ghost: " << res.obj() << " result = "
|
||||
<< *((u32*)res.obj().ptr) << "\n";
|
||||
std::cout << "print_new_ghost: " << res.ghost() << " result = "
|
||||
<< *((u32*)res.ghost().pointer) << "\n";
|
||||
return nil;
|
||||
}
|
||||
```
|
||||
|
@ -814,7 +819,7 @@ var print_new_ghost(var* args, usize size, gc* ngc) {
|
|||
|
||||
我们使用下面的这个函数检测是否是正确的自定义类型:
|
||||
|
||||
`bool var::objchk(const std::string&);`
|
||||
`bool var::object_check(const std::string&);`
|
||||
|
||||
参数是自定义类型的类型名。
|
||||
|
||||
|
@ -866,8 +871,7 @@ code: undefined symbol "i"
|
|||
`die`函数用于直接抛出错误并终止执行。
|
||||
|
||||
```javascript
|
||||
func()
|
||||
{
|
||||
func() {
|
||||
println("hello");
|
||||
die("error occurred this line");
|
||||
return;
|
||||
|
@ -877,17 +881,27 @@ func()
|
|||
```javascript
|
||||
hello
|
||||
[vm] error: error occurred this line
|
||||
[vm] native function error.
|
||||
trace back:
|
||||
0x000000ac 40 00 00 00 25 callb 0x25 <__die@0x41afc0> (lib.nas:131)
|
||||
0x000004f6 3e 00 00 00 01 callfv 0x1 (a.nas:4)
|
||||
0x000004fa 3e 00 00 00 00 callfv 0x0 (a.nas:6)
|
||||
vm stack (0x7fffcd21bc68 <sp+80>, limit 10, total 12):
|
||||
0x0000005b | null |
|
||||
...
|
||||
0x00000057 | str | <0x138ff60> error occurred t...
|
||||
...
|
||||
0x00000052 | nil |
|
||||
[vm] error: error occurred in native function
|
||||
|
||||
call trace (main)
|
||||
call func@0x557513935710() {entry: 0x850}
|
||||
|
||||
trace back (main)
|
||||
0x000547 4c 00 00 16 callb 0x16 <__die@0x557512441780>(std/lib.nas:150)
|
||||
0x000856 4a 00 00 01 callfv 0x1(a.nas:3)
|
||||
0x00085a 4a 00 00 00 callfv 0x0(a.nas:5)
|
||||
|
||||
stack (0x5575138e8c40, limit 10, total 14)
|
||||
0x00000d | null |
|
||||
0x00000c | pc | 0x856
|
||||
0x00000b | addr | 0x5575138e8c50
|
||||
0x00000a | nil |
|
||||
0x000009 | nil |
|
||||
0x000008 | str | <0x5575138d9190> error occurred t...
|
||||
0x000007 | nil |
|
||||
0x000006 | func | <0x5575139356f0> entry:0x850
|
||||
0x000005 | pc | 0x85a
|
||||
0x000004 | addr | 0x0
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -907,18 +921,31 @@ func(f){
|
|||
```
|
||||
|
||||
```javascript
|
||||
[vm] stack overflow
|
||||
trace back:
|
||||
0x000004fb 3e 00 00 00 01 callfv 0x1 (a.nas:5)
|
||||
0x000004fb 1349 same call(s)
|
||||
0x000004f3 3e 00 00 00 01 callfv 0x1 (a.nas:2)
|
||||
0x000004ff 3e 00 00 00 01 callfv 0x1 (a.nas:3)
|
||||
vm stack (0x7fffd3781d58 <sp+80>, limit 10, total 8108):
|
||||
0x00001ffb | func | <0x15f8d90> entry:0x4f9
|
||||
0x00001ffa | func | <0x15f8d90> entry:0x4f9
|
||||
0x00001ff9 | pc | 0x4fb
|
||||
...
|
||||
0x00001ff2 | addr | 0x7fffd37a16e8
|
||||
[vm] error: stack overflow
|
||||
|
||||
call trace (main)
|
||||
call func@0x564106058620(f) {entry: 0x859}
|
||||
--> 583 same call(s)
|
||||
call func@0x5641060586c0(f) {entry: 0x851}
|
||||
|
||||
trace back (main)
|
||||
0x000859 45 00 00 01 calll 0x1(a.nas:5)
|
||||
0x00085b 4a 00 00 01 callfv 0x1(a.nas:5)
|
||||
0x00085b 582 same call(s)
|
||||
0x000853 4a 00 00 01 callfv 0x1(a.nas:2)
|
||||
0x00085f 4a 00 00 01 callfv 0x1(a.nas:3)
|
||||
|
||||
stack (0x56410600be00, limit 10, total 4096)
|
||||
0x000fff | func | <0x564106058600> entry:0x859
|
||||
0x000ffe | pc | 0x85b
|
||||
0x000ffd | addr | 0x56410601bd20
|
||||
0x000ffc | nil |
|
||||
0x000ffb | nil |
|
||||
0x000ffa | func | <0x564106058600> entry:0x859
|
||||
0x000ff9 | nil |
|
||||
0x000ff8 | func | <0x564106058600> entry:0x859
|
||||
0x000ff7 | pc | 0x85b
|
||||
0x000ff6 | addr | 0x56410601bcb0
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -934,11 +961,13 @@ func(){
|
|||
```
|
||||
|
||||
```javascript
|
||||
[vm] callv: must call a vector/hash/string
|
||||
trace back:
|
||||
0x000004f4 3b 00 00 00 00 callv 0x0 (a.nas:3)
|
||||
vm stack (0x7fffff539c28 <sp+80>, limit 10, total 1):
|
||||
0x00000050 | num | 0
|
||||
[vm] error: must call a vector/hash/string but get number
|
||||
|
||||
trace back (main)
|
||||
0x000854 47 00 00 00 callv 0x0(a.nas:3)
|
||||
|
||||
stack (0x564993f462b0, limit 10, total 1)
|
||||
0x000000 | num | 0
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -950,35 +979,48 @@ vm stack (0x7fffff539c28 <sp+80>, limit 10, total 1):
|
|||
```javascript
|
||||
hello
|
||||
[vm] error: error occurred this line
|
||||
[vm] error: native function error
|
||||
[vm] error: error occurred in native function
|
||||
|
||||
call trace (main)
|
||||
call func@0x55dcb5b8fbf0() {entry: 0x850}
|
||||
|
||||
trace back (main)
|
||||
0x000000b0 40 00 00 00 2b callb 0x2b <__die@0x41c380> (lib.nas:131)
|
||||
0x00000553 3e 00 00 00 01 callfv 0x1 (test.nas:4)
|
||||
0x00000557 3e 00 00 00 00 callfv 0x0 (test.nas:6)
|
||||
vm stack (0x7fffe0ffed90 <sp+63>, limit 10, total 12)
|
||||
0x0000004a | null |
|
||||
0x00000049 | pc | 0x553
|
||||
0x00000048 | addr | 0x7fffe0ffeda0
|
||||
...
|
||||
0x00000041 | nil |
|
||||
0x000547 4c 00 00 16 callb 0x16 <__die@0x55dcb3c41780>(std/lib.nas:150)
|
||||
0x000856 4a 00 00 01 callfv 0x1(a.nas:3)
|
||||
0x00085a 4a 00 00 00 callfv 0x0(a.nas:5)
|
||||
|
||||
stack (0x55dcb5b43120, limit 10, total 14)
|
||||
0x00000d | null |
|
||||
0x00000c | pc | 0x856
|
||||
0x00000b | addr | 0x55dcb5b43130
|
||||
0x00000a | nil |
|
||||
0x000009 | nil |
|
||||
0x000008 | str | <0x55dcb5b33670> error occurred t...
|
||||
0x000007 | nil |
|
||||
0x000006 | func | <0x55dcb5b8fbd0> entry:0x850
|
||||
0x000005 | pc | 0x85a
|
||||
0x000004 | addr | 0x0
|
||||
|
||||
registers (main)
|
||||
[ pc ] | pc | 0xb0
|
||||
[ global ] | addr | 0x7fffe0ffe9a0
|
||||
[ localr ] | addr | 0x7fffe0ffedf0
|
||||
[pc ] | pc | 0x547
|
||||
[global] | addr | 0x55dcb5b53130
|
||||
[local ] | addr | 0x55dcb5b43190
|
||||
[memr ] | addr | 0x0
|
||||
[ canary ] | addr | 0x7fffe1002990
|
||||
[ top ] | addr | 0x7fffe0ffee40
|
||||
[ funcr ] | func | <0x677cd0> entry:0xb0
|
||||
[ upvalr ] | nil |
|
||||
global (0x7fffe0ffe9a0 <sp+0>)
|
||||
0x00000000 | func | <0x65fb00> entry:0x5
|
||||
0x00000001 | func | <0x65fb20> entry:0xd
|
||||
[canary] | addr | 0x55dcb5b53110
|
||||
[top ] | addr | 0x55dcb5b431f0
|
||||
[funcr ] | func | <0x55dcb5b65620> entry:0x547
|
||||
[upval ] | nil |
|
||||
|
||||
global (0x55dcb5b53130)
|
||||
0x000000 | nmspc| <0x55dcb5b33780> namespace [95 val]
|
||||
0x000001 | vec | <0x55dcb5b64c20> [0 val]
|
||||
...
|
||||
0x0000003d | func | <0x66bf00> entry:0x51f
|
||||
0x0000003e | hash | <0x65ffa0> {5 val}
|
||||
local (0x7fffe0ffedf0 <sp+45>)
|
||||
0x00000000 | nil |
|
||||
0x00000001 | str | <0x6cb630> error occurred t...
|
||||
0x00005e | func | <0x55dcb5b8fc70> entry:0x846
|
||||
|
||||
local (0x55dcb5b43190 <+7>)
|
||||
0x000000 | nil |
|
||||
0x000001 | str | <0x55dcb5b33670> error occurred t...
|
||||
0x000002 | nil |
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -1002,16 +1044,18 @@ source code:
|
|||
for(var i=0;i<31;i+=1)
|
||||
print(fib(i),'\n');
|
||||
|
||||
|
||||
next bytecode:
|
||||
--> 0x00000000 01 00 00 00 41 intg 0x41 (test/fib.nas:0)
|
||||
0x00000001 0b 00 00 00 05 newf 0x5 (lib.nas:6)
|
||||
0x00000002 02 00 00 00 02 intl 0x2 (lib.nas:6)
|
||||
0x00000003 0f 00 00 00 00 dyn 0x0 ("elems") (lib.nas:6)
|
||||
0x00000004 32 00 00 00 07 jmp 0x7 (lib.nas:6)
|
||||
0x00000005 40 00 00 00 00 callb 0x0 <__print@0x419c80> (lib.nas:7)
|
||||
0x00000006 4a 00 00 00 00 ret 0x0 (lib.nas:7)
|
||||
0x00000007 03 00 00 00 00 loadg 0x0 (lib.nas:6)
|
||||
vm stack (0x7fffd0259138 <sp+65>, limit 10, total 0)
|
||||
0x000848 4a 00 00 01 callfv 0x1(std/lib.nas:427)
|
||||
0x000849 3d 00 00 00 pop 0x0(std/lib.nas:427)
|
||||
0x00084a 07 00 00 00 pnil 0x0(std/lib.nas:423)
|
||||
0x00084b 56 00 00 00 ret 0x0(std/lib.nas:423)
|
||||
0x00084c 03 00 00 5e loadg 0x5e(std/lib.nas:423)
|
||||
--> 0x00084d 0b 00 08 51 newf 0x851(test/fib.nas:1)
|
||||
0x00084e 02 00 00 03 intl 0x3(test/fib.nas:1)
|
||||
0x00084f 0d 00 00 08 para 0x8 (x)(test/fib.nas:1)
|
||||
|
||||
stack (0x55ccd0a1b9d0, limit 10, total 0)
|
||||
>>
|
||||
```
|
||||
|
||||
|
@ -1034,23 +1078,26 @@ source code:
|
|||
for(var i=0;i<31;i+=1)
|
||||
print(fib(i),'\n');
|
||||
|
||||
|
||||
next bytecode:
|
||||
0x00000548 0c 00 00 00 aa happ 0xaa ("running") (lib.nas:503)
|
||||
0x00000549 03 00 00 00 3e loadg 0x3e (lib.nas:498)
|
||||
0x0000054a 0b 00 00 05 4e newf 0x54e (test/fib.nas:1)
|
||||
0x0000054b 02 00 00 00 02 intl 0x2 (test/fib.nas:1)
|
||||
0x0000054c 0d 00 00 00 1b para 0x1b ("x") (test/fib.nas:1)
|
||||
0x0000054d 32 00 00 05 5d jmp 0x55d (test/fib.nas:1)
|
||||
--> 0x0000054e 39 00 00 00 01 calll 0x1 (test/fib.nas:3)
|
||||
0x0000054f 2d 00 00 00 03 lessc 0x3 (2) (test/fib.nas:3)
|
||||
vm stack (0x7fffd0259138 <sp+65>, limit 10, total 7)
|
||||
0x00000047 | pc | 0x566
|
||||
0x00000046 | addr | 0x0
|
||||
0x00000045 | nil |
|
||||
0x00000044 | num | 0
|
||||
0x00000043 | nil |
|
||||
0x00000042 | nil |
|
||||
0x00000041 | func | <0x88d2f0> entry:0x5
|
||||
0x000850 3e 00 08 60 jmp 0x860(test/fib.nas:1)
|
||||
--> 0x000851 45 00 00 01 calll 0x1(test/fib.nas:3)
|
||||
0x000852 39 00 00 07 lessc 0x7 (2)(test/fib.nas:3)
|
||||
0x000853 40 00 08 56 jf 0x856(test/fib.nas:3)
|
||||
0x000854 45 00 00 01 calll 0x1(test/fib.nas:3)
|
||||
0x000855 56 00 00 00 ret 0x0(test/fib.nas:3)
|
||||
0x000856 44 00 00 5f callg 0x5f(test/fib.nas:4)
|
||||
0x000857 45 00 00 01 calll 0x1(test/fib.nas:4)
|
||||
|
||||
stack (0x55ccd0a1b9d0, limit 10, total 8)
|
||||
0x000007 | pc | 0x869
|
||||
0x000006 | addr | 0x0
|
||||
0x000005 | nil |
|
||||
0x000004 | nil |
|
||||
0x000003 | num | 0
|
||||
0x000002 | nil |
|
||||
0x000001 | nil |
|
||||
0x000000 | func | <0x55ccd0a58fa0> entry:0x487
|
||||
>>
|
||||
```
|
||||
|
||||
|
@ -1077,3 +1124,22 @@ Nasal REPL interpreter version 11.0 (Oct 7 2023 17:28:31)
|
|||
|
||||
>>>
|
||||
```
|
||||
|
||||
试试引入 `std/json.nas` 模块 ~
|
||||
|
||||
```bash
|
||||
[nasal-repl] Initializating enviroment...
|
||||
[nasal-repl] Initialization complete.
|
||||
|
||||
Nasal REPL interpreter version 11.1 (Nov 1 2023 23:37:30)
|
||||
.h, .help | show help
|
||||
.e, .exit | quit the REPL
|
||||
.q, .quit | quit the REPL
|
||||
.c, .clear | clear the screen
|
||||
.s, .source | show source code
|
||||
|
||||
>>> use std.json;
|
||||
{stringify:func(..) {..},parse:func(..) {..}}
|
||||
|
||||
>>>
|
||||
```
|
||||
|
|
|
@ -62,3 +62,27 @@ var example_module = func {
|
|||
};
|
||||
}();
|
||||
```
|
||||
|
||||
## Import a module
|
||||
|
||||
Here is a module named `std/example_module.nas`:
|
||||
|
||||
```nasal
|
||||
var a = 1;
|
||||
```
|
||||
|
||||
Then there's a script file named `test.nas`, import module in this file using this way:
|
||||
|
||||
```nasal
|
||||
use std.example_module;
|
||||
|
||||
println(example_module.a); # 1
|
||||
```
|
||||
|
||||
Or this way:
|
||||
|
||||
```nasal
|
||||
import("std/example_module.nas");
|
||||
|
||||
println(example_module.a); # 1
|
||||
```
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1> Nasal | Not another scripting language!</h1>
|
||||
<img src="/doc/pic/social.png" width="900" height="400" style="margin-left: 15px;"><br /></img>
|
||||
<div class="badges">
|
||||
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/github/languages/code-size/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github"></img></a>
|
||||
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github"></img></a>
|
||||
|
|
|
@ -64,7 +64,7 @@ var set_new_ghost(var* args, usize size, gc* ngc) {
|
|||
return nil;
|
||||
}
|
||||
f64 num = args[1].num();
|
||||
*((u32*)res.ghost().pointer) = static_cast<u32>(num);
|
||||
*(reinterpret_cast<u32*>(res.ghost().pointer)) = static_cast<u32>(num);
|
||||
std::cout << "set_new_ghost: successfully set ghost = " << num << "\n";
|
||||
return nil;
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ var nas_connect(var* args, usize size, gc* ngc) {
|
|||
memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
|
||||
return var::num(static_cast<double>(connect(
|
||||
args[0].num(),
|
||||
(sockaddr*)&addr,
|
||||
reinterpret_cast<sockaddr*>(&addr),
|
||||
sizeof(sockaddr_in)
|
||||
)));
|
||||
}
|
||||
|
|
|
@ -320,6 +320,7 @@ while(1){
|
|||
elsif(path=="/license")
|
||||
http.send(client,respond.ok(io.readfile("./LICENSE")));
|
||||
elsif(path=="/doc/pic/nasal.png" or
|
||||
path=="/doc/pic/social.png" or
|
||||
path=="/doc/pic/benchmark.png" or
|
||||
path=="/doc/pic/mandelbrot.png" or
|
||||
path=="/doc/pic/feigenbaum.png" or
|
||||
|
|
Loading…
Reference in New Issue