forked from xxq250/Nasal-Interpreter
Compare commits
77 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c9a10d710 | ||
|
|
fd57e9a47c | ||
|
|
ab99d2d1ed | ||
|
|
ae0dae5956 | ||
|
|
00c6e3b4fd | ||
|
|
cdf7b92a8e | ||
|
|
0e979a6e7b | ||
|
|
dd144305da | ||
|
|
4f3ddf803a | ||
|
|
de305d26ad | ||
|
|
9f30f45774 | ||
|
|
1ae47807eb | ||
|
|
3deea632f8 | ||
|
|
9f2c31149a | ||
|
|
b25a1bc3f4 | ||
|
|
fd7677f94f | ||
|
|
2e31a70406 | ||
|
|
8e29a3ec5b | ||
|
|
a68bf85f04 | ||
|
|
aae9395d66 | ||
|
|
a463af53b7 | ||
|
|
6adb991c04 | ||
|
|
c5f4736984 | ||
|
|
1a233fbe15 | ||
|
|
a7d6518bff | ||
|
|
0700ce14f7 | ||
|
|
c88620920b | ||
|
|
125fc8a9fe | ||
|
|
b06e1bb5dd | ||
|
|
c7316e9780 | ||
|
|
be1bcdfe2c | ||
|
|
144e6f45da | ||
|
|
569d5c6c6a | ||
|
|
7087c67d79 | ||
|
|
c4e6a89959 | ||
|
|
f60f674845 | ||
|
|
c21d40c466 | ||
|
|
19b590f3bb | ||
|
|
a421470715 | ||
|
|
79dc13f419 | ||
|
|
2e8208a752 | ||
|
|
1c40cca673 | ||
|
|
b1a5a5f6c0 | ||
|
|
64961877de | ||
|
|
9c9bb52818 | ||
|
|
02148f4766 | ||
|
|
767711c93a | ||
|
|
78ba0641a6 | ||
|
|
80683c381f | ||
|
|
6aac46adaf | ||
|
|
388ef66308 | ||
|
|
8faa4ef2db | ||
|
|
441c02d0fb | ||
|
|
953ad80482 | ||
|
|
e2ee9cff4c | ||
|
|
996ac59c79 | ||
|
|
944f713ee9 | ||
|
|
7329c70492 | ||
|
|
b5514fd269 | ||
|
|
3be50116fa | ||
|
|
125d6d3a7d | ||
|
|
bb746dfbfb | ||
|
|
8069a1b659 | ||
|
|
f0cb8b6ef3 | ||
|
|
9474ac9ef0 | ||
|
|
b862aa91eb | ||
|
|
bc64d530be | ||
|
|
ea5116e963 | ||
|
|
cc4e697246 | ||
|
|
515bef3f5d | ||
|
|
05800fe518 | ||
|
|
33d37771ce | ||
|
|
89540475cf | ||
|
|
e03da2f737 | ||
|
|
4617eb8f17 | ||
|
|
fed1e20085 | ||
|
|
9f30440286 |
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 ValKmjolnir
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
677
README.md
677
README.md
@@ -1,60 +1,105 @@
|
||||
# Nasal Interpreter
|
||||
# Nasal Script Language
|
||||
|
||||
[](http://wiki.flightgear.org/File:Nasallogo3.png)
|
||||
|
||||
# Nasal script language
|
||||
## Introduction
|
||||
|
||||
[Nasal](http://wiki.flightgear.org/Nasal_scripting_language) is a script language that used in [FlightGear](https://www.flightgear.org/).
|
||||
|
||||
There is a Nasal console in FlightGear but sometimes it is not so easy for every developer to use.
|
||||
The interpreter is totally rewritten by ValKmjolnir using C++(standard c++11) without reusing the code in Andy Ross's nasal interpreter(<https://github.com/andyross/nasal>). But we really appreciate that Andy created this amazing programming language and his interpreter project.
|
||||
|
||||
So this is an interpreter for Nasal written by C++.
|
||||
The interpreter is still in development(now it works well --2021/2/15). We really need your support!
|
||||
|
||||
The interpreter is still in development.We really need your support!
|
||||
Also,i am a member of [FGPRC](https://www.fgprc.org/), welcome to join us!
|
||||
|
||||
# Why Writing Nasal Interpreter
|
||||
(2021/5/4) Now this project uses MIT license.Edit it if you want, use this project to learn or create more interesting things(But don't forget me XD).
|
||||
|
||||
Nasal is a script language first used in Flightgear.
|
||||
## Why Writing Nasal Interpreter
|
||||
|
||||
But in last summer holiday, members in FGPRC told me that it is hard to debug with nasal-console in Flightgear, especially when checking syntax error.
|
||||
Nasal is a script language first used in Flightgear, created by Andy Ross(<https://github.com/andyross>).
|
||||
|
||||
But in last summer holiday, members in FGPRC told me that it is hard to debug with nasal-console in Flightgear, especially when checking syntax errors.
|
||||
|
||||
So i tried to write a new interpreter to help them checking syntax error and even, runtime error.
|
||||
|
||||
I wrote the lexer, parser and runtime(nasal virtual machine/ast-runtime virtual machine) to help checking errors.
|
||||
I wrote the lexer, parser and runtimebytecode virtual machine(there was an ast-interpreter,but i deleted it after version4.0) to help checking errors.
|
||||
|
||||
They found it easier for them to check errors before copying nasal-codes in nasal-console in Flightgear to test.
|
||||
They found it much easier to check syntax and runtime errors before copying nasal-codes in nasal-console in Flightgear to test.
|
||||
|
||||
# How to Compile
|
||||
Also, you could use this language to write some interesting programs and run them without the lib of Flightgear.
|
||||
|
||||
> g++ -std=c++11 main.cpp -o main.exe
|
||||
|
||||
# Lexical Analysis
|
||||
You could add your own built-in functions to change this interpreter to a useful tool in your own projects(such as a script in your own game).
|
||||
|
||||
The flow chart of lexer is here:
|
||||
## How to Compile
|
||||
|
||||
[](https://github.com/ValKmjolnir/Nasal-Interpreter/blob/master/pic/nasal_lexer.png)
|
||||
Better choose the latest update of the interpreter.
|
||||
|
||||
This picture seems ugly. I will re-draw it later(maybe 1000 years later).
|
||||
MUST USE -O2/-O3 if want to optimize the interpreter! pragma gcc optimize(2) seems useless when using g++
|
||||
|
||||
# Parser
|
||||
> g++ -std=c++11 -O2 main.cpp -o nasal.exe
|
||||
|
||||
## Version 3.0
|
||||
Or use this in linux/macOS/Unix
|
||||
|
||||
I refactored parser and make it easier to maintain.
|
||||
> g++ -std=c++11 -O2 main.cpp -o nasal
|
||||
|
||||
The EBNF is also refactored.
|
||||
## How to Use?
|
||||
|
||||
# Abstract Syntax Tree
|
||||
Input this command to use interactive interpreter mode:
|
||||
|
||||
## Version 1.2
|
||||
> ./nasal
|
||||
|
||||
Input this command to run scripts directly:
|
||||
|
||||
> ./nasal filename
|
||||
|
||||
Use these commands to get version of interpreter:
|
||||
|
||||
> ./nasal -v | -version
|
||||
|
||||
Use these commands to get help:
|
||||
|
||||
> ./nasal -h | -help
|
||||
|
||||
If your system is Windows and you want to output unicode,please use this command before running nasal interpreter:
|
||||
|
||||
> chcp 65001
|
||||
|
||||
The interpreter's interactive mode will do this automatically,so you don't need to run this command if you use the interactive interpreter.
|
||||
|
||||
## Parser
|
||||
|
||||
LL(k) parser.
|
||||
|
||||
```javascript
|
||||
(var a,b,c)=[{b:nil},[1,2],func{return 0;}];
|
||||
(a.b,b[0],c)=(1,2,3);
|
||||
```
|
||||
|
||||
These two expressions have the same first set,so LL(1) is useless for this language.
|
||||
|
||||
Maybe in the future i can refactor it to LL(1) with special checks.
|
||||
|
||||
### version 1.0(last update 2019/10/14)
|
||||
|
||||
First fully functional version of nasal_parser.
|
||||
|
||||
Before version 1.0,i tried many times to create a correct parser.
|
||||
|
||||
Finally i learned LL(1) and LL(k) and wrote a parser for math formulas in version 0.16(last update 2019/9/14).
|
||||
|
||||
In version 0.17(2019/9/15) 0.18(2019/9/18) 0.19(2019/10/1)i was playing the parser happily and after that i wrote version 1.0.
|
||||
|
||||
This project began at 2019/8/31.
|
||||
|
||||
## Abstract Syntax Tree
|
||||
|
||||
### Version 1.2(last update 2019/10/31)
|
||||
|
||||
The ast has been completed in this version.
|
||||
|
||||
## Version 2.0
|
||||
### Version 2.0(last update 2020/8/31)
|
||||
|
||||
A completed ast-interpreter with unfinished lib functions.
|
||||
|
||||
## Version 3.0
|
||||
### Version 3.0(last update 2020/10/23)
|
||||
|
||||
The ast is refactored and is now easier to read and maintain.
|
||||
|
||||
@@ -64,9 +109,15 @@ Now you can add your own functions as builtin-functions in this interpreter!
|
||||
|
||||
I decide to save the ast interpreter after releasing v4.0. Because it took me a long time to think and write...
|
||||
|
||||
# Byte Code Interpreter
|
||||
### Version 5.0(last update 2021/3/7)
|
||||
|
||||
## Version 4.0
|
||||
I change my mind.AST interpreter leaves me too much things to do.
|
||||
|
||||
If i continue saving this interpreter,it will be harder for me to make the bytecode vm become more efficient.
|
||||
|
||||
## Byte Code Interpreter
|
||||
|
||||
### Version 4.0(last update 2020/12/17)
|
||||
|
||||
I have just finished the first version of byte-code-interpreter.
|
||||
|
||||
@@ -77,21 +128,563 @@ Now i am trying to search hidden bugs in this interpreter.Hope you could help me
|
||||
There's an example of byte code below:
|
||||
|
||||
```javascript
|
||||
var (a,b,c)=(1,2,3);
|
||||
for(var i=0;i<4000000;i+=1);
|
||||
```
|
||||
|
||||
```asm
|
||||
.number 0
|
||||
.number 4e+006
|
||||
.number 1
|
||||
.symbol i
|
||||
0x00000000: pzero 0x00000000
|
||||
0x00000001: loadg 0x00000000 (i)
|
||||
0x00000002: callg 0x00000000 (i)
|
||||
0x00000003: pnum 0x00000001 (4e+006)
|
||||
0x00000004: less 0x00000000
|
||||
0x00000005: jf 0x0000000b
|
||||
0x00000006: pone 0x00000000
|
||||
0x00000007: mcallg 0x00000000 (i)
|
||||
0x00000008: addeq 0x00000000
|
||||
0x00000009: pop 0x00000000
|
||||
0x0000000a: jmp 0x00000002
|
||||
0x0000000b: nop 0x00000000
|
||||
```
|
||||
|
||||
### Version 5.0(last update 2021/3/7)
|
||||
|
||||
I decide to optimize bytecode vm in this version.
|
||||
|
||||
Because it takes more than 1.5s to count i from 0 to 4000000-1.This is not efficient at all!
|
||||
|
||||
2021/1/23 update: Now it can count from 0 to 4000000-1 in 1.5s.
|
||||
|
||||
### Version 6.0(last update 2021/6/1)
|
||||
|
||||
Use loadg loadl callg calll mcallg mcalll to avoid branches.
|
||||
|
||||
Delete type vm_scop.
|
||||
|
||||
Use const vm_num to avoid frequently new & delete.
|
||||
|
||||
Change garbage collector from reference-counting to mark-sweep.
|
||||
|
||||
Vapp and newf operand use .num to reduce the size of exec_code.
|
||||
|
||||
2021/4/3 update: Now it can count from 0 to 4000000-1 in 0.8s.
|
||||
|
||||
2021/4/19 update: Now it can count from 0 to 4e6-1 in 0.4s.
|
||||
|
||||
In this update i changed global and local scope from unordered_map to vector.
|
||||
|
||||
So the bytecode generator changed a lot.
|
||||
|
||||
```javascript
|
||||
for(var i=0;i<4000000;i+=1);
|
||||
```
|
||||
|
||||
```asm
|
||||
.number 4e+006
|
||||
0x00000000: intg 0x00000001
|
||||
0x00000001: pzero 0x00000000
|
||||
0x00000002: loadg 0x00000000
|
||||
0x00000003: callg 0x00000000
|
||||
0x00000004: pnum 0x00000000 (4e+006)
|
||||
0x00000005: less 0x00000000
|
||||
0x00000006: jf 0x0000000c
|
||||
0x00000007: pone 0x00000000
|
||||
0x00000008: mcallg 0x00000000
|
||||
0x00000009: addeq 0x00000000
|
||||
0x0000000a: pop 0x00000000
|
||||
0x0000000b: jmp 0x00000003
|
||||
0x0000000c: nop 0x00000000
|
||||
```
|
||||
|
||||
### Version 6.5(latest)
|
||||
|
||||
2021/5/31 update: Now gc can collect garbage correctly without re-collecting,which will cause fatal error.
|
||||
|
||||
Add builtin_alloc to avoid mark-sweep when running a built-in function,which will mark useful items as useless garbage to collect.
|
||||
|
||||
Better use setsize and assignment to get a big array,append is very slow in this situation.
|
||||
|
||||
2021/6/3 update: Fixed a bug that gc still re-collects garbage,this time i use three mark states to make sure garbage is ready to be collected.
|
||||
|
||||
Change callf to callfv and callfh.And callfv fetches arguments from val_stack directly instead of using vm_vec,a not very efficient way.
|
||||
|
||||
Better use callfv instead of callfh,callfh will fetch a vm_hash from stack and parse it,making this process slow.
|
||||
|
||||
```javascript
|
||||
var f=func(x,y){return x+y;}
|
||||
f(1024,2048);
|
||||
```
|
||||
|
||||
```asm
|
||||
.number 1024
|
||||
.number 2048
|
||||
.symbol x
|
||||
.symbol y
|
||||
0x00000000: intg 0x00000001
|
||||
0x00000001: newf 0x00000007
|
||||
0x00000002: intl 0x00000003
|
||||
0x00000003: offset 0x00000001
|
||||
0x00000004: para 0x00000000 (x)
|
||||
0x00000005: para 0x00000001 (y)
|
||||
0x00000006: jmp 0x0000000b
|
||||
0x00000007: calll 0x00000001
|
||||
0x00000008: calll 0x00000002
|
||||
0x00000009: add 0x00000000
|
||||
0x0000000a: ret 0x00000000
|
||||
0x0000000b: loadg 0x00000000
|
||||
0x0000000c: callg 0x00000000
|
||||
0x0000000d: pnum 0x00000000 (1024)
|
||||
0x0000000e: pnum 0x00000001 (2048)
|
||||
0x0000000f: callfv 0x00000002
|
||||
0x00000010: pop 0x00000000
|
||||
0x00000011: nop 0x00000000
|
||||
```
|
||||
|
||||
2021/6/21 update: Now gc will not collect nullptr.And the function of assignment is complete,now these kinds of assignment is allowed:
|
||||
|
||||
```javascript
|
||||
var f=func()
|
||||
{
|
||||
var _=[{_:0},{_:1}];
|
||||
return func(x)
|
||||
{
|
||||
return _[x];
|
||||
}
|
||||
}
|
||||
var m=f();
|
||||
m(0)._=m(1)._=10;
|
||||
|
||||
[0,1,2][1:2][0]=0;
|
||||
```
|
||||
|
||||
In the old version,parser will check this left-value and tells that these kinds of left-value are not allowed(bad lvalue).
|
||||
|
||||
But now it can work.And you could see its use by reading the code above.To make sure this assignment works correctly,codegen will generate byte code by nasal_codegen::call_gen() instead of nasal_codegen::mcall_gen(),and the last child of the ast will be generated by nasal_codegen::mcall_gen().So the bytecode is totally different now:
|
||||
|
||||
```asm
|
||||
.number 10
|
||||
.number 2
|
||||
.number 3
|
||||
.symbol a
|
||||
.symbol b
|
||||
.symbol c
|
||||
0x00000000: pone 0x00000000
|
||||
0x00000001: load 0x00000000 (a)
|
||||
0x00000002: pnum 0x00000001 (2)
|
||||
0x00000003: load 0x00000001 (b)
|
||||
0x00000004: pnum 0x00000002 (3)
|
||||
0x00000005: load 0x00000002 (c)
|
||||
0x00000006: nop 0x00000000
|
||||
```
|
||||
.symbol _
|
||||
.symbol x
|
||||
0x00000000: intg 0x00000002
|
||||
0x00000001: newf 0x00000005
|
||||
0x00000002: intl 0x00000002
|
||||
0x00000003: offset 0x00000001
|
||||
0x00000004: jmp 0x00000017
|
||||
0x00000005: newh 0x00000000
|
||||
0x00000006: pzero 0x00000000
|
||||
0x00000007: happ 0x00000000 (_)
|
||||
0x00000008: newh 0x00000000
|
||||
0x00000009: pone 0x00000000
|
||||
0x0000000a: happ 0x00000000 (_)
|
||||
0x0000000b: newv 0x00000002
|
||||
0x0000000c: loadl 0x00000001
|
||||
0x0000000d: newf 0x00000012
|
||||
0x0000000e: intl 0x00000003
|
||||
0x0000000f: offset 0x00000002
|
||||
0x00000010: para 0x00000001 (x)
|
||||
0x00000011: jmp 0x00000016
|
||||
0x00000012: calll 0x00000001
|
||||
0x00000013: calll 0x00000002
|
||||
0x00000014: callv 0x00000000
|
||||
0x00000015: ret 0x00000000
|
||||
0x00000016: ret 0x00000000
|
||||
0x00000017: loadg 0x00000000
|
||||
0x00000018: callg 0x00000000
|
||||
0x00000019: callfv 0x00000000
|
||||
0x0000001a: loadg 0x00000001
|
||||
0x0000001b: pnum 0x00000000 (10.000000)
|
||||
0x0000001c: callg 0x00000001
|
||||
0x0000001d: pone 0x00000000
|
||||
0x0000001e: callfv 0x00000001
|
||||
0x0000001f: mcallh 0x00000000 (_)
|
||||
0x00000020: meq 0x00000000
|
||||
0x00000021: callg 0x00000001
|
||||
0x00000022: pzero 0x00000000
|
||||
0x00000023: callfv 0x00000001
|
||||
0x00000024: mcallh 0x00000000 (_)
|
||||
0x00000025: meq 0x00000000
|
||||
0x00000026: pop 0x00000000
|
||||
0x00000027: pzero 0x00000000
|
||||
0x00000028: pzero 0x00000000
|
||||
0x00000029: pone 0x00000000
|
||||
0x0000002a: pnum 0x00000001 (2.000000)
|
||||
0x0000002b: newv 0x00000003
|
||||
0x0000002c: slcbeg 0x00000000
|
||||
0x0000002d: pone 0x00000000
|
||||
0x0000002e: pnum 0x00000001 (2.000000)
|
||||
0x0000002f: slc2 0x00000000
|
||||
0x00000030: slcend 0x00000000
|
||||
0x00000031: pzero 0x00000000
|
||||
0x00000032: mcallv 0x00000000
|
||||
0x00000033: meq 0x00000000
|
||||
0x00000034: pop 0x00000000
|
||||
0x00000035: nop 0x00000000
|
||||
```
|
||||
|
||||
As you could see from the bytecode above,mcall/mcallv/mcallh operands' using frequency will reduce,call/callv/callh/callfv/callfh at the opposite.
|
||||
|
||||
And because of the new structure of mcall, addr_stack, a stack used to store the memory address, is deleted from nasal_vm, and now nasal_vm use nasal_val** mem_addr to store the memory address. This will not cause fatal errors because the memory address is used __immediately__ after getting it.
|
||||
|
||||
## Test data
|
||||
|
||||
### version 6.5(i5-8250U windows10 2021/6/19)
|
||||
|
||||
running time and gc time:
|
||||
|
||||
|file|call gc|total time|gc time|
|
||||
|:----|:----|:----|:----|
|
||||
|pi.nas|12000049|0.593s|0.222s|
|
||||
|fib.nas|10573747|2.838s|0.187s|
|
||||
|bp.nas|4419829|1.99s|0.18s|
|
||||
|bigloop.nas|4000000|0.419s|0.039s|
|
||||
|mandelbrot.nas|1044630|0.433s|0.041s|
|
||||
|life.nas|817112|8.557s|0.199s|
|
||||
|ascii-art.nas|45612|0.48s|0.027s|
|
||||
|calc.nas|8089|0.068s|0.006s|
|
||||
|quick_sort.nas|2768|0.107s|0s|
|
||||
|bfs.nas|2471|1.763s|0.003s|
|
||||
|
||||
operands calling frequency:
|
||||
|
||||
|file|1st|2nd|3rd|4th|5th|
|
||||
|:----|:----|:----|:----|:----|:----|
|
||||
|pi.nas|callg|pop|mcallg|pnum|pone|
|
||||
|fib.nas|calll|pnum|callg|less|jf|
|
||||
|bp.nas|calll|callg|pop|callv|addeq|
|
||||
|bigloop.nas|pnum|less|jf|callg|pone|
|
||||
|mandelbrot.nas|callg|mult|loadg|pnum|pop|
|
||||
|life.nas|calll|callv|pnum|jf|callg|
|
||||
|ascii-art.nas|calll|pop|mcalll|callg|callb|
|
||||
|calc.nas|calll|pop|pstr|mcalll|jmp|
|
||||
|quick_sort.nas|calll|pop|jt|jf|less|
|
||||
|bfs.nas|calll|pop|callv|mcalll|jf|
|
||||
|
||||
operands calling total times:
|
||||
|
||||
|file|1st|2nd|3rd|4th|5th|
|
||||
|:----|:----|:----|:----|:----|:----|
|
||||
|pi.nas|6000004|6000003|6000000|4000005|4000002|
|
||||
|fib.nas|17622792|10573704|7049218|7049155|7049155|
|
||||
|bp.nas|7081480|4227268|2764676|2617112|2065441|
|
||||
|bigloop.nas|4000001|4000001|4000001|4000001|4000000|
|
||||
|mandelbrot.nas|1519632|563856|290641|286795|284844|
|
||||
|life.nas|2114371|974244|536413|534794|489743|
|
||||
|ascii-art.nas|37906|22736|22402|18315|18292|
|
||||
|calc.nas|191|124|109|99|87|
|
||||
|quick_sort.nas|16226|5561|4144|3524|2833|
|
||||
|bfs.nas|24707|16297|14606|14269|8672|
|
||||
|
||||
## How to Use Nasal to Program
|
||||
|
||||
### basic value type
|
||||
|
||||
Nasal has 6 value types.Number,string,vector,hash,function,nil.
|
||||
|
||||
Number has 3 formats.Dec,hex and oct;
|
||||
|
||||
String has 3 formats.But the third one is often used to declare a character.
|
||||
|
||||
Vector has unlimited length and can store all types of values.
|
||||
|
||||
Hash is a hashmap that stores values with strings/identifiers as the key.
|
||||
|
||||
Function is also a value type in nasal.
|
||||
|
||||
```javascript
|
||||
var spc=nil;
|
||||
|
||||
var a=1;
|
||||
var a=2.71828;
|
||||
var a=2.147e16;
|
||||
var a=1e-10;
|
||||
var a=0x7fffffff;
|
||||
var a=0xAA55;
|
||||
var a=0o170001;
|
||||
|
||||
var b='str';
|
||||
var b="another string";
|
||||
var b=`c`;
|
||||
|
||||
var c=[];
|
||||
var c=[0,nil,{},[],func(){return 0;}];
|
||||
append(c,0,1,2);
|
||||
|
||||
var d={
|
||||
member1:nil,
|
||||
member2:'str',
|
||||
'member3':'member\'s name can also be a string constant',
|
||||
"member4":"also this",
|
||||
function:func()
|
||||
{
|
||||
var a=me.member2~me.member3;
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
var f=func(x,y,z)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
var f=func
|
||||
{
|
||||
return 1024;
|
||||
}
|
||||
var f=func(x,y,z,default_para1=1,default_para2=2)
|
||||
{
|
||||
return x+y+z+default_para1+default_para2;
|
||||
}
|
||||
var f=func(x,y,z,dynamic_para...)
|
||||
{
|
||||
var sum=0;
|
||||
foreach(var i;dynamic_para)
|
||||
sum+=i;
|
||||
return sum+x+y+z;
|
||||
}
|
||||
```
|
||||
|
||||
### operators
|
||||
|
||||
```javascript
|
||||
1+2-1*2/1;
|
||||
'str1'~'str2';
|
||||
(1+2)*(3+4)
|
||||
|
||||
1+1 and 0;
|
||||
1<0 or 1>0;
|
||||
1<=0 and 1>=0;
|
||||
1==0 or 1!=0;
|
||||
|
||||
-1;
|
||||
!0;
|
||||
|
||||
a=b=c=d=1;
|
||||
a+=1;
|
||||
a-=1;
|
||||
a*=1;
|
||||
a/=1;
|
||||
a~='string';
|
||||
```
|
||||
|
||||
### definition
|
||||
|
||||
```javascript
|
||||
var a=1;
|
||||
var (a,b,c)=[0,1,2];
|
||||
var (a,b,c)=(0,1,2);
|
||||
(var a,b,c)=[0,1,2];
|
||||
(var a,b,c)=(0,1,2);
|
||||
```
|
||||
|
||||
### multi-assignment
|
||||
|
||||
```javascript
|
||||
(a,b[0],c.d)=[0,1,2];
|
||||
(a,b[1],c.e)=(0,1,2);
|
||||
```
|
||||
|
||||
### conditional expression
|
||||
|
||||
```javascript
|
||||
if(1)
|
||||
{
|
||||
;
|
||||
}
|
||||
elsif(2)
|
||||
{
|
||||
;
|
||||
}
|
||||
else if(3)
|
||||
{
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
;
|
||||
}
|
||||
```
|
||||
|
||||
### loop
|
||||
|
||||
```javascript
|
||||
while(condition)
|
||||
continue;
|
||||
|
||||
for(var i=0;i<10;i+=1)
|
||||
break;
|
||||
|
||||
forindex(var i;elem)
|
||||
print(elem[i]);
|
||||
|
||||
foreach(var i;elem)
|
||||
print(i);
|
||||
```
|
||||
|
||||
### subvec
|
||||
|
||||
Use index to search one element in the string will get the ascii number of this character.If you want to get the character,use built-in function chr().
|
||||
|
||||
```javascript
|
||||
a[-1,1,0:2,0:,:3,:,nil:8,3:nil,nil:nil];
|
||||
"hello world"[0];
|
||||
```
|
||||
|
||||
### special function call
|
||||
|
||||
This is of great use but is not very efficient(because hashmap use string as the key to compare).
|
||||
|
||||
```javascript
|
||||
a(x:0,y:1,z:2);
|
||||
```
|
||||
|
||||
### lambda
|
||||
|
||||
Also functions have this kind of use:
|
||||
|
||||
```javascript
|
||||
func(x,y){return x+y}(0,1);
|
||||
func(x){return 1/(1+math.exp(-x));}(0.5);
|
||||
```
|
||||
|
||||
### closure
|
||||
|
||||
Use closure to OOP.
|
||||
|
||||
```javascript
|
||||
var f=func()
|
||||
{
|
||||
var a=1;
|
||||
return func(){return a;};
|
||||
}
|
||||
print(f()());
|
||||
|
||||
var student=func(name,age)
|
||||
{
|
||||
var val={
|
||||
name:name,
|
||||
age:age
|
||||
};
|
||||
return {
|
||||
print_info:func(){println(val.name,' ',val.age);},
|
||||
set_age: func(age){val.age=age;},
|
||||
get_age: func(){return val.age;},
|
||||
set_name: func(name){val.name=name;},
|
||||
get_name: func(){return val.name;}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### built-in functions
|
||||
|
||||
Must import lib.nas or has these functions' definitions inside your code.
|
||||
|
||||
Also you could add builtin functions of your own(written in C/C++) to help you calculate things more quickly.(Advanced usage)
|
||||
|
||||
Check built-in functions in lib.nas!
|
||||
|
||||
If you want to add your own built-in functions,define the function in nasal_builtin.h.
|
||||
|
||||
Definition:
|
||||
|
||||
```C++
|
||||
nasal_val* builtin_chr(std::vector<nasal_val*>&,nasal_gc&);
|
||||
```
|
||||
|
||||
Then complete this function using C++:
|
||||
|
||||
```C++
|
||||
nasal_val* builtin_print(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
|
||||
{
|
||||
// get arguments by using builtin_find
|
||||
// find value with index begin from 1
|
||||
// because local_scope[0] is reserved for value 'me'
|
||||
nasal_val* vector_value=local_scope[1];
|
||||
// main process
|
||||
// also check number of arguments and type here
|
||||
// if get a type error,use builtin_err and return nullptr
|
||||
for(auto i:vec_addr->ptr.vec->elems)
|
||||
switch(i->type)
|
||||
{
|
||||
case vm_nil: std::cout<<"nil"; break;
|
||||
case vm_num: std::cout<<i->ptr.num; break;
|
||||
case vm_str: std::cout<<*i->ptr.str; break;
|
||||
case vm_vec: i->ptr.vec->print(); break;
|
||||
case vm_hash: i->ptr.hash->print(); break;
|
||||
case vm_func: std::cout<<"func(...){...}"; break;
|
||||
}
|
||||
// if a nasal value is not in use,use gc::del_reference to delete it
|
||||
// generate return value,use gc::gc_alloc(type) to make a new value
|
||||
// or use reserved reference gc.nil_addr/gc.one_addr/gc.zero_addr
|
||||
return gc.nil_addr;
|
||||
}
|
||||
```
|
||||
|
||||
After that, write the built-in function's name(in nasal) and the function's pointer in this table:
|
||||
|
||||
```C++
|
||||
struct FUNC_TABLE
|
||||
{
|
||||
const char* name;
|
||||
nasal_val* (*func)(std::vector<nasal_val*>&,nasal_gc&);
|
||||
} builtin_func[]=
|
||||
{
|
||||
{"__builtin_print",builtin_print},
|
||||
{nullptr, nullptr }
|
||||
};
|
||||
```
|
||||
|
||||
At last,warp the '__builtin_print' in a nasal file:
|
||||
|
||||
```javascript
|
||||
var print=func(elems...)
|
||||
{
|
||||
return __builtin_print(elems);
|
||||
};
|
||||
```
|
||||
|
||||
In fact the arguments that '__builtin_print' uses is not necessary,So writting it like this is also right:
|
||||
|
||||
```javascript
|
||||
var print=func(elems...)
|
||||
{
|
||||
return __builtin_print;
|
||||
};
|
||||
```
|
||||
|
||||
In version 5.0,if you don't warp built-in function in a normal nasal function,this built-in function may cause a fault when searching arguments,which will cause SIGSEGV segmentation error(maybe).
|
||||
|
||||
Use import("") to get the nasal file including your built-in functions,then you could use it.
|
||||
|
||||
version 6.5 update:
|
||||
|
||||
Use nasal_gc::builtin_alloc in builtin function if this function uses alloc more then one time.
|
||||
|
||||
When running a builtin function,alloc will run more than one time,this may cause mark-sweep in gc_alloc.
|
||||
|
||||
The value got before will be collected,but stil in use in this builtin function,this is a fatal error.
|
||||
|
||||
So use builtin_alloc in builtin functions like this:
|
||||
|
||||
```C++
|
||||
nasal_val* builtin_keys(std::vector<nasal_val*>& local_scope,nasal_gc& gc)
|
||||
{
|
||||
nasal_val* hash_addr=local_scope[1];
|
||||
if(hash_addr->type!=vm_hash)
|
||||
{
|
||||
builtin_err("keys","\"hash\" must be hash");
|
||||
return nullptr;
|
||||
}
|
||||
nasal_val* ret_addr=gc.builtin_alloc(vm_vec);
|
||||
std::vector<nasal_val*>& ref_vec=ret_addr->ptr.vec->elems;
|
||||
for(auto iter:hash_addr->ptr.hash->elems)
|
||||
{
|
||||
nasal_val* str_addr=gc.builtin_alloc(vm_str);
|
||||
*str_addr->ptr.str=iter.first;
|
||||
ref_vec.push_back(str_addr);
|
||||
}
|
||||
return ret_addr;
|
||||
}
|
||||
```
|
||||
|
||||
175
lib.nas
175
lib.nas
@@ -1,167 +1,144 @@
|
||||
var import=func(filename)
|
||||
{
|
||||
nasal_call_import(filename);
|
||||
return nil;
|
||||
return __builtin_import(filename);
|
||||
}
|
||||
var print=func(elements...)
|
||||
var print=func(elems...)
|
||||
{
|
||||
nasal_call_builtin_std_cout(elements);
|
||||
return nil;
|
||||
return __builtin_print(elems);
|
||||
};
|
||||
var append=func(vector,elements...)
|
||||
var println=func(elems...)
|
||||
{
|
||||
nasal_call_builtin_push_back(vector,elements);
|
||||
return nil;
|
||||
__builtin_print(elems);
|
||||
elems=['\n'];
|
||||
return __builtin_print(elems);
|
||||
}
|
||||
var setsize=func(vector,size)
|
||||
var append=func(vec,elems...)
|
||||
{
|
||||
nasal_call_builtin_set_size(vector,size);
|
||||
return nil;
|
||||
return __builtin_append(vec,elems);
|
||||
}
|
||||
var setsize=func(vec,size)
|
||||
{
|
||||
return __builtin_setsize(vec,size);
|
||||
}
|
||||
var system=func(str)
|
||||
{
|
||||
nasal_call_builtin_system(str);
|
||||
return;
|
||||
return __builtin_system(str);
|
||||
}
|
||||
var input=func()
|
||||
{
|
||||
return nasal_call_builtin_input();
|
||||
return __builtin_input();
|
||||
}
|
||||
var sleep=func(duration)
|
||||
{
|
||||
nasal_call_builtin_sleep(duration);
|
||||
return;
|
||||
return __builtin_sleep(duration);
|
||||
}
|
||||
var split=func(delimeter,string)
|
||||
var split=func(deli,str)
|
||||
{
|
||||
return nasal_call_builtin_split(delimeter,string);
|
||||
return __builtin_split(deli,str);
|
||||
}
|
||||
var rand=func(seed=nil)
|
||||
{
|
||||
return nasal_call_builtin_rand(seed);
|
||||
return __builtin_rand(seed);
|
||||
}
|
||||
var id=func(thing)
|
||||
var id=func(object)
|
||||
{
|
||||
return nasal_call_builtin_get_id(thing);
|
||||
return __builtin_id(object);
|
||||
}
|
||||
var int=func(value)
|
||||
var int=func(val)
|
||||
{
|
||||
return nasal_call_builtin_trans_int(value);
|
||||
return __builtin_int(val);
|
||||
}
|
||||
var num=func(value)
|
||||
var num=func(val)
|
||||
{
|
||||
return nasal_call_builtin_trans_num(value);
|
||||
return __builtin_num(val);
|
||||
}
|
||||
var pop=func(vector)
|
||||
var pop=func(vec)
|
||||
{
|
||||
return nasal_call_builtin_pop_back(vector);
|
||||
return __builtin_pop(vec);
|
||||
}
|
||||
var str=func(number)
|
||||
var str=func(num)
|
||||
{
|
||||
return nasal_call_builtin_trans_str(number);
|
||||
return __builtin_str(num);
|
||||
}
|
||||
var size=func(object)
|
||||
{
|
||||
return nasal_call_builtin_size(object);
|
||||
return __builtin_size(object);
|
||||
}
|
||||
var contains=func(hash,key)
|
||||
{
|
||||
return nasal_call_builtin_contains(hash,key);
|
||||
return __builtin_contains(hash,key);
|
||||
}
|
||||
var delete=func(hash,key)
|
||||
{
|
||||
nasal_call_builtin_delete(hash,key);
|
||||
return;
|
||||
return __builtin_delete(hash,key);
|
||||
}
|
||||
var keys=func(hash)
|
||||
{
|
||||
return nasal_call_builtin_get_keys(hash);
|
||||
return __builtin_keys(hash);
|
||||
}
|
||||
var time=func(begin_time)
|
||||
{
|
||||
return nasal_call_builtin_time(begin_time);
|
||||
return __builtin_time(begin_time);
|
||||
}
|
||||
var die=func(str)
|
||||
{
|
||||
nasal_call_builtin_die(str);
|
||||
return nil;
|
||||
return __builtin_die(str);
|
||||
}
|
||||
var typeof=func(object)
|
||||
{
|
||||
return nasal_call_builtin_type(object);
|
||||
return __builtin_type(object);
|
||||
}
|
||||
var substr=func(str,begin,length)
|
||||
var substr=func(str,begin,len)
|
||||
{
|
||||
return nasal_call_builtin_substr(str,begin,length);
|
||||
return __builtin_substr(str,begin,len);
|
||||
}
|
||||
var streq=func(a,b)
|
||||
{
|
||||
return __builtin_streq(a,b);
|
||||
}
|
||||
var left=func(str,len)
|
||||
{
|
||||
return __builtin_left(str,len);
|
||||
}
|
||||
var right=func(str,len)
|
||||
{
|
||||
return __builtin_right(str,len);
|
||||
}
|
||||
var cmp=func(a,b)
|
||||
{
|
||||
return __builtin_cmp(a,b);
|
||||
}
|
||||
var chr=func(code)
|
||||
{
|
||||
return __builtin_chr(code);
|
||||
}
|
||||
|
||||
var io=
|
||||
{
|
||||
fin:func(filename)
|
||||
{
|
||||
return nasal_call_builtin_finput(filename);
|
||||
},
|
||||
fout:func(filename,str)
|
||||
{
|
||||
nasal_call_builtin_foutput(filename,str);
|
||||
return;
|
||||
}
|
||||
fin: func(filename){return __builtin_fin(filename);},
|
||||
fout:func(filename,str){return __builtin_fout(filename,str);}
|
||||
};
|
||||
|
||||
var bits=
|
||||
{
|
||||
bitxor:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_xor(a,b);
|
||||
},
|
||||
bitand:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_and(a,b);
|
||||
},
|
||||
bitor:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_or(a,b);
|
||||
},
|
||||
bitnand:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_nand(a,b);
|
||||
},
|
||||
bitnot:func(a)
|
||||
{
|
||||
return nasal_call_builtin_not(a);
|
||||
}
|
||||
bitxor: func(a,b){return __builtin_xor(a,b); },
|
||||
bitand: func(a,b){return __builtin_and(a,b); },
|
||||
bitor: func(a,b){return __builtin_or(a,b); },
|
||||
bitnand: func(a,b){return __builtin_nand(a,b);},
|
||||
bitnot: func(a) {return __builtin_not(a); }
|
||||
};
|
||||
|
||||
var math=
|
||||
{
|
||||
e:2.7182818284590452354,
|
||||
pi:3.14159265358979323846264338327950288,
|
||||
sin:func(x)
|
||||
{
|
||||
return nasal_call_builtin_sin(x);
|
||||
},
|
||||
cos:func(x)
|
||||
{
|
||||
return nasal_call_builtin_cos(x);
|
||||
},
|
||||
tan:func(x)
|
||||
{
|
||||
return nasal_call_builtin_tan(x);
|
||||
},
|
||||
exp:func(x)
|
||||
{
|
||||
return nasal_call_builtin_exp(x);
|
||||
},
|
||||
ln:func(x)
|
||||
{
|
||||
return nasal_call_builtin_cpp_math_ln(x);
|
||||
},
|
||||
sqrt:func(x)
|
||||
{
|
||||
return nasal_call_builtin_cpp_math_sqrt(x);
|
||||
},
|
||||
atan2:func(x,y)
|
||||
{
|
||||
return nasal_call_builtin_cpp_atan2(x,y);
|
||||
},
|
||||
e: 2.7182818284590452354,
|
||||
pi: 3.14159265358979323846264338327950288,
|
||||
sin: func(x) {return __builtin_sin(x); },
|
||||
cos: func(x) {return __builtin_cos(x); },
|
||||
tan: func(x) {return __builtin_tan(x); },
|
||||
exp: func(x) {return __builtin_exp(x); },
|
||||
ln: func(x) {return __builtin_ln(x); },
|
||||
sqrt: func(x) {return __builtin_sqrt(x); },
|
||||
atan2: func(x,y){return __builtin_atan2(x,y);}
|
||||
};
|
||||
|
||||
var D2R=math.pi/180;
|
||||
256
main.cpp
256
main.cpp
@@ -1,221 +1,173 @@
|
||||
#include "nasal.h"
|
||||
|
||||
nasal_lexer lexer;
|
||||
nasal_parse parse;
|
||||
nasal_import import;
|
||||
std::string inputfile="null";
|
||||
nasal_runtime runtime;
|
||||
nasal_codegen code_generator;
|
||||
nasal_bytecode_vm bytevm;
|
||||
|
||||
void help()
|
||||
void help_interact()
|
||||
{
|
||||
std::cout<<">> [\"file\"] input a file name.\n";
|
||||
std::cout<<">> [help ] show help.\n";
|
||||
std::cout<<">> [clear ] clear the screen.\n";
|
||||
std::cout<<">> [del ] clear the input filename.\n";
|
||||
std::cout<<">> [lex ] use lexer to turn code into tokens.\n";
|
||||
std::cout<<">> [ast ] do parsing and check the abstract syntax tree.\n";
|
||||
std::cout<<">> [run ] run abstract syntax tree.\n";
|
||||
std::cout<<">> [code ] show byte code.\n";
|
||||
std::cout<<">> [exec ] execute program on bytecode vm.\n";
|
||||
std::cout<<">> [logo ] print logo of nasal .\n";
|
||||
std::cout<<">> [exit ] quit nasal interpreter.\n";
|
||||
std::cout
|
||||
<<">> [ ] input a file name to execute. \n"
|
||||
<<">> [help ] show help. \n"
|
||||
<<">> [clear] clear the screen. \n"
|
||||
<<">> [lex ] view tokens. \n"
|
||||
<<">> [ast ] view abstract syntax tree. \n"
|
||||
<<">> [code ] view byte code. \n"
|
||||
<<">> [exec ] execute program on bytecode vm.\n"
|
||||
<<">> [logo ] print logo of nasal . \n"
|
||||
<<">> [exit ] quit nasal interpreter. \n";
|
||||
return;
|
||||
}
|
||||
void help_cmd()
|
||||
{
|
||||
std::cout
|
||||
#ifdef _WIN32
|
||||
<<"use command \'chcp 65001\' if want to use unicode.\n"
|
||||
#endif
|
||||
<<"nasal | use interactive interpreter.\n"
|
||||
<<"nasal -h -help | get help.\n"
|
||||
<<"nasal -v -version | get version of nasal interpreter.\n"
|
||||
<<"nasal filename | execute script file.\n";
|
||||
return;
|
||||
}
|
||||
void info()
|
||||
{
|
||||
std::cout
|
||||
<<">> Nasal interpreter ver 6.5.\n"
|
||||
<<">> Thanks to https://github.com/andyross/nasal\n"
|
||||
<<">> Code: https://github.com/ValKmjolnir/Nasal-Interpreter\n"
|
||||
<<">> Code: https://gitee.com/valkmjolnir/Nasal-Interpreter\n"
|
||||
<<">> Info: http://wiki.flightgear.org/Nasal_scripting_language\n"
|
||||
<<">> Input \"help\" to get help .\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void logo()
|
||||
{
|
||||
std::cout<<" __ _ \n";
|
||||
std::cout<<" /\\ \\ \\__ _ ___ __ _| | \n";
|
||||
std::cout<<" / \\/ / _` / __|/ _` | | \n";
|
||||
std::cout<<" / /\\ / (_| \\__ \\ (_| | | \n";
|
||||
std::cout<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n";
|
||||
std::cout
|
||||
<<" __ _ \n"
|
||||
<<" /\\ \\ \\__ _ ___ __ _| | \n"
|
||||
<<" / \\/ / _` / __|/ _` | | \n"
|
||||
<<" / /\\ / (_| \\__ \\ (_| | | \n"
|
||||
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void del_func()
|
||||
{
|
||||
lexer.clear();
|
||||
parse.clear();
|
||||
inputfile="null";
|
||||
std::cout<<">> [Delete] complete.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void die(std::string stage,std::string filename)
|
||||
void die(const char* stage,std::string& filename)
|
||||
{
|
||||
std::cout<<">> ["<<stage<<"] in <\""<<filename<<"\">: error(s) occurred,stop.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
void lex_func()
|
||||
void execute(std::string& file,std::string& command)
|
||||
{
|
||||
lexer.openfile(inputfile);
|
||||
nasal_lexer lexer;
|
||||
nasal_parse parse;
|
||||
nasal_import import;
|
||||
nasal_codegen codegen;
|
||||
nasal_vm vm;
|
||||
lexer.openfile(file);
|
||||
lexer.scanner();
|
||||
if(lexer.get_error())
|
||||
{
|
||||
die("lexer",inputfile);
|
||||
die("lexer",file);
|
||||
return;
|
||||
}
|
||||
lexer.print_token();
|
||||
return;
|
||||
}
|
||||
|
||||
void ast_print()
|
||||
{
|
||||
lexer.openfile(inputfile);
|
||||
lexer.scanner();
|
||||
if(lexer.get_error())
|
||||
if(command=="lex")
|
||||
{
|
||||
die("lexer",inputfile);
|
||||
lexer.print_token();
|
||||
return;
|
||||
}
|
||||
parse.set_toklist(lexer.get_token_list());
|
||||
lexer.get_token_list().clear();
|
||||
parse.main_process();
|
||||
if(parse.get_error())
|
||||
{
|
||||
die("parse",inputfile);
|
||||
die("parse",file);
|
||||
return;
|
||||
}
|
||||
parse.get_root().print_ast(0);
|
||||
return;
|
||||
}
|
||||
|
||||
void runtime_start()
|
||||
{
|
||||
lexer.openfile(inputfile);
|
||||
lexer.scanner();
|
||||
if(lexer.get_error())
|
||||
if(command=="ast")
|
||||
{
|
||||
die("lexer",inputfile);
|
||||
return;
|
||||
}
|
||||
parse.set_toklist(lexer.get_token_list());
|
||||
parse.main_process();
|
||||
if(parse.get_error())
|
||||
{
|
||||
die("parse",inputfile);
|
||||
parse.get_root().print_ast(0);
|
||||
return;
|
||||
}
|
||||
import.link(parse.get_root());
|
||||
parse.get_root().clear();
|
||||
if(import.get_error())
|
||||
{
|
||||
die("import",inputfile);
|
||||
die("import",file);
|
||||
return;
|
||||
}
|
||||
runtime.set_root(import.get_root());
|
||||
runtime.run();
|
||||
return;
|
||||
}
|
||||
|
||||
void show_bytecode()
|
||||
{
|
||||
lexer.openfile(inputfile);
|
||||
lexer.scanner();
|
||||
if(lexer.get_error())
|
||||
codegen.main_progress(import.get_root());
|
||||
if(codegen.get_error())
|
||||
{
|
||||
die("lexer",inputfile);
|
||||
die("codegen",file);
|
||||
return;
|
||||
}
|
||||
parse.set_toklist(lexer.get_token_list());
|
||||
parse.main_process();
|
||||
if(parse.get_error())
|
||||
if(command=="code")
|
||||
{
|
||||
die("parse",inputfile);
|
||||
codegen.print_byte_code();
|
||||
return;
|
||||
}
|
||||
import.link(parse.get_root());
|
||||
if(import.get_error())
|
||||
{
|
||||
die("import",inputfile);
|
||||
return;
|
||||
}
|
||||
code_generator.main_progress(import.get_root());
|
||||
code_generator.print_byte_code();
|
||||
return;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
lexer.openfile(inputfile);
|
||||
lexer.scanner();
|
||||
if(lexer.get_error())
|
||||
{
|
||||
die("lexer",inputfile);
|
||||
return;
|
||||
}
|
||||
parse.set_toklist(lexer.get_token_list());
|
||||
parse.main_process();
|
||||
if(parse.get_error())
|
||||
{
|
||||
die("parse",inputfile);
|
||||
return;
|
||||
}
|
||||
import.link(parse.get_root());
|
||||
if(import.get_error())
|
||||
{
|
||||
die("import",inputfile);
|
||||
return;
|
||||
}
|
||||
code_generator.main_progress(import.get_root());
|
||||
bytevm.run(
|
||||
code_generator.get_string_table(),
|
||||
code_generator.get_number_table(),
|
||||
code_generator.get_exec_code()
|
||||
vm.init(
|
||||
codegen.get_str_table(),
|
||||
codegen.get_num_table(),
|
||||
codegen.get_exec_code()
|
||||
);
|
||||
vm.run();
|
||||
vm.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
void interact()
|
||||
{
|
||||
std::string command;
|
||||
#ifdef _WIN32
|
||||
// use chcp 65001 to use unicode io
|
||||
system("chcp 65001");
|
||||
system("cls");
|
||||
#endif
|
||||
std::string command,file="null";
|
||||
logo();
|
||||
std::cout<<">> Nasal interpreter ver 3.0 .\n";
|
||||
std::cout<<">> Code: https://github.com/ValKmjolnir/Nasal-Interpreter\n";
|
||||
std::cout<<">> Info: http://wiki.flightgear.org/Nasal_scripting_language\n";
|
||||
std::cout<<">> Input \"help\" to get help .\n";
|
||||
info();
|
||||
while(1)
|
||||
{
|
||||
std::cout<<">> ";
|
||||
std::cin>>command;
|
||||
if(command=="help")
|
||||
help();
|
||||
help_interact();
|
||||
else if(command=="clear")
|
||||
{
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
#endif
|
||||
#ifdef _linux_
|
||||
system("clear");
|
||||
#endif
|
||||
#ifdef TARGET_OS_MAC
|
||||
system("clear");
|
||||
#else
|
||||
int rs=system("clear");
|
||||
#endif
|
||||
}
|
||||
else if(command=="del")
|
||||
del_func();
|
||||
else if(command=="lex")
|
||||
lex_func();
|
||||
else if(command=="ast")
|
||||
ast_print();
|
||||
else if(command=="run")
|
||||
runtime_start();
|
||||
else if(command=="code")
|
||||
show_bytecode();
|
||||
else if(command=="exec")
|
||||
execute();
|
||||
else if(command=="logo")
|
||||
logo();
|
||||
else if(command=="exit")
|
||||
break;
|
||||
return;
|
||||
else if(command=="lex" || command=="ast" || command=="code" || command=="exec")
|
||||
execute(file,command);
|
||||
else
|
||||
inputfile=command;
|
||||
file=command;
|
||||
}
|
||||
}
|
||||
int main(int argc,const char* argv[])
|
||||
{
|
||||
std::string command,file="null";
|
||||
if(argc==1)
|
||||
interact();
|
||||
else if(argc==2 && (!strcmp(argv[1],"-v") || !strcmp(argv[1],"-version")))
|
||||
{
|
||||
logo();
|
||||
std::cout<<"Nasal interpreter ver 6.5\n";
|
||||
}
|
||||
else if(argc==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"-help")))
|
||||
help_cmd();
|
||||
else if(argc==2 && argv[1][0]!='-')
|
||||
{
|
||||
file=argv[1];
|
||||
command="exec";
|
||||
execute(file,command);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout
|
||||
<<"invalid command.\n"
|
||||
<<"use nasal -h to get help.\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -68,6 +68,7 @@ scalar::=
|
||||
|hash {call_scalar}
|
||||
|number
|
||||
|string
|
||||
|nil
|
||||
|'(' calculation ')' {call_scalar}
|
||||
;
|
||||
call_scalar::=
|
||||
|
||||
120
nasal.h
120
nasal.h
@@ -3,6 +3,7 @@
|
||||
|
||||
#pragma GCC optimize(2)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@@ -19,113 +20,101 @@
|
||||
#include <stack>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
/*
|
||||
check if a string can be converted to a number
|
||||
if this string cannot be converted to a number,it will return nan
|
||||
*/
|
||||
inline double hex_to_double(std::string str)
|
||||
inline double hex_to_double(const char* str)
|
||||
{
|
||||
double ret=0;
|
||||
for(int i=2;str[i];++i)
|
||||
for(;*str;++str)
|
||||
{
|
||||
ret*=16;
|
||||
if('0'<=str[i] && str[i]<='9')
|
||||
ret+=(str[i]-'0');
|
||||
else if('a'<=str[i] && str[i]<='f')
|
||||
ret+=(str[i]-'a'+10);
|
||||
else if('A'<=str[i] && str[i]<='F')
|
||||
ret+=(str[i]-'A'+10);
|
||||
if('0'<=*str && *str<='9')
|
||||
ret+=(*str-'0');
|
||||
else if('a'<=*str && *str<='f')
|
||||
ret+=(*str-'a'+10);
|
||||
else if('A'<=*str && *str<='F')
|
||||
ret+=(*str-'A'+10);
|
||||
else
|
||||
return std::nan("");
|
||||
return nan("");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
inline double oct_to_double(std::string str)
|
||||
inline double oct_to_double(const char* str)
|
||||
{
|
||||
double ret=0;
|
||||
for(int i=2;str[i];++i)
|
||||
for(;*str;++str)
|
||||
{
|
||||
ret*=8;
|
||||
if('0'<=str[i] && str[i]<='8')
|
||||
ret+=(str[i]-'0');
|
||||
if('0'<=*str && *str<='8')
|
||||
ret+=(*str-'0');
|
||||
else
|
||||
return std::nan("");
|
||||
return nan("");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
inline double dec_to_double(std::string str,int len)
|
||||
inline double dec_to_double(const char* str)
|
||||
{
|
||||
double ret=0;
|
||||
int i=0;
|
||||
while('0'<=str[i] && str[i]<='9' && i<len)
|
||||
ret=ret*10+(str[i++]-'0');
|
||||
if(i==len) return ret;
|
||||
if(str[i]!='.' && str[i]!='e' && str[i]!='E')
|
||||
return std::nan("");
|
||||
if(str[i]=='.')
|
||||
double ret=0,negative=1,num_pow=0;
|
||||
while('0'<=*str && *str<='9')
|
||||
ret=ret*10+(*str++-'0');
|
||||
if(!*str) return ret;
|
||||
if(*str=='.')
|
||||
{
|
||||
++i;
|
||||
if(i==len) return std::nan("");
|
||||
double num_pow=0.1;
|
||||
while('0'<=str[i] && str[i]<='9' && i<len)
|
||||
if(!*++str) return nan("");
|
||||
num_pow=0.1;
|
||||
while('0'<=*str && *str<='9')
|
||||
{
|
||||
ret+=num_pow*(str[i++]-'0');
|
||||
ret+=num_pow*(*str++-'0');
|
||||
num_pow*=0.1;
|
||||
}
|
||||
if(!*str) return ret;
|
||||
}
|
||||
if(i==len) return ret;
|
||||
if(str[i]!='e' && str[i]!='E')
|
||||
return std::nan("");
|
||||
++i;
|
||||
if(i==len) return std::nan("");
|
||||
double negative=(str[i]=='-'? -1:1);
|
||||
if(str[i]=='-' || str[i]=='+')
|
||||
++i;
|
||||
if(i==len) return std::nan("");
|
||||
double num_pow=0;
|
||||
for(;i<len;++i)
|
||||
if(*str!='e' && *str!='E')
|
||||
return nan("");
|
||||
if(!*++str) return nan("");
|
||||
if(*str=='-' || *str=='+')
|
||||
negative=(*str++=='-'? -1:1);
|
||||
if(!*str) return nan("");
|
||||
num_pow=0;
|
||||
for(;*str;++str)
|
||||
{
|
||||
if('0'<=str[i] && str[i]<='9')
|
||||
num_pow=num_pow*10+(str[i]-'0');
|
||||
if('0'<=*str && *str<='9')
|
||||
num_pow=num_pow*10+(*str-'0');
|
||||
else
|
||||
return std::nan("");
|
||||
return nan("");
|
||||
}
|
||||
return ret*std::pow(10,negative*num_pow);
|
||||
}
|
||||
double trans_string_to_number(std::string str)
|
||||
double str2num(const char* str)
|
||||
{
|
||||
double is_negative=1;
|
||||
int len=str.length();
|
||||
bool is_negative=false;
|
||||
double ret_num=0;
|
||||
if(!len) return std::nan("");
|
||||
if(str[0]=='-' || str[0]=='+')
|
||||
{
|
||||
if(len==1) return std::nan("");
|
||||
is_negative=(str[0]=='-'?-1:1);
|
||||
str=str.substr(1,len--);
|
||||
}
|
||||
if(len>1 && str[0]=='0' && str[1]=='x')
|
||||
ret_num=hex_to_double(str);
|
||||
else if(len>1 && str[0]=='0' && str[1]=='o')
|
||||
ret_num=oct_to_double(str);
|
||||
if(*str=='-' || *str=='+')
|
||||
is_negative=(*str++=='-');
|
||||
if(!*str)
|
||||
return nan("");
|
||||
if(str[0]=='0' && str[1]=='x')
|
||||
ret_num=hex_to_double(str+2);
|
||||
else if(str[0]=='0' && str[1]=='o')
|
||||
ret_num=oct_to_double(str+2);
|
||||
else
|
||||
ret_num=dec_to_double(str,len);
|
||||
return is_negative*ret_num;
|
||||
ret_num=dec_to_double(str);
|
||||
return is_negative?-ret_num:ret_num;
|
||||
}
|
||||
|
||||
/*
|
||||
trans_number_to_string:
|
||||
convert number to string
|
||||
*/
|
||||
std::string trans_number_to_string(double number)
|
||||
std::string num2str(double number)
|
||||
{
|
||||
std::string res;
|
||||
std::stringstream ss;
|
||||
std::ostringstream ss;
|
||||
ss<<number;
|
||||
ss>>res;
|
||||
return res;
|
||||
return ss.str();
|
||||
}
|
||||
#include "nasal_lexer.h"
|
||||
#include "nasal_ast.h"
|
||||
@@ -133,8 +122,7 @@ std::string trans_number_to_string(double number)
|
||||
#include "nasal_import.h"
|
||||
#include "nasal_gc.h"
|
||||
#include "nasal_builtin.h"
|
||||
#include "nasal_runtime.h"
|
||||
#include "nasal_codegen.h"
|
||||
#include "nasal_bytecode_vm.h"
|
||||
#include "nasal_vm.h"
|
||||
|
||||
#endif
|
||||
|
||||
275
nasal_ast.h
275
nasal_ast.h
@@ -3,230 +3,139 @@
|
||||
|
||||
enum ast_node
|
||||
{
|
||||
ast_null=0,ast_root,ast_block,
|
||||
ast_nil,ast_number,ast_string,ast_identifier,ast_function,ast_hash,ast_vector,
|
||||
ast_hashmember,ast_call,ast_call_hash,ast_call_vec,ast_call_func,ast_subvec,
|
||||
ast_null=0,
|
||||
ast_root,ast_block,
|
||||
ast_nil,ast_num,ast_str,ast_id,ast_func,ast_hash,ast_vec,
|
||||
ast_hashmember,ast_call,ast_callh,ast_callv,ast_callf,ast_subvec,
|
||||
ast_args,ast_default_arg,ast_dynamic_id,
|
||||
ast_and,ast_or,
|
||||
ast_equal,ast_add_equal,ast_sub_equal,ast_mult_equal,ast_div_equal,ast_link_equal,
|
||||
ast_cmp_equal,ast_cmp_not_equal,
|
||||
ast_less_than,ast_less_equal,
|
||||
ast_greater_than,ast_greater_equal,
|
||||
ast_equal,ast_addeq,ast_subeq,ast_multeq,ast_diveq,ast_lnkeq,
|
||||
ast_cmpeq,ast_neq,
|
||||
ast_less,ast_leq,
|
||||
ast_grt,ast_geq,
|
||||
ast_add,ast_sub,ast_mult,ast_div,ast_link,
|
||||
ast_unary_sub,ast_unary_not,
|
||||
ast_trinocular,
|
||||
ast_neg,ast_not,
|
||||
ast_trino,
|
||||
ast_for,ast_forindex,ast_foreach,ast_while,ast_new_iter,
|
||||
ast_conditional,ast_if,ast_elsif,ast_else,
|
||||
ast_multi_id,ast_multi_scalar,
|
||||
ast_definition,ast_multi_assign,
|
||||
ast_continue,ast_break,ast_return
|
||||
ast_def,ast_multi_assign,
|
||||
ast_continue,ast_break,ast_ret
|
||||
};
|
||||
|
||||
std::string ast_str(int type)
|
||||
const char* ast_name[]=
|
||||
{
|
||||
std::string str;
|
||||
switch(type)
|
||||
{
|
||||
case ast_null: str="null";break;
|
||||
case ast_root: str="root";break;
|
||||
case ast_block: str="block";break;
|
||||
case ast_nil: str="nil";break;
|
||||
case ast_number: str="number";break;
|
||||
case ast_string: str="string";break;
|
||||
case ast_identifier: str="id";break;
|
||||
case ast_function: str="function";break;
|
||||
case ast_hash: str="hash";break;
|
||||
case ast_vector: str="vector";break;
|
||||
case ast_hashmember: str="hashmember";break;
|
||||
case ast_call: str="call";break;
|
||||
case ast_call_hash: str="call_hash";break;
|
||||
case ast_call_vec: str="call_vector";break;
|
||||
case ast_call_func: str="call_func";break;
|
||||
case ast_subvec: str="subvec";break;
|
||||
case ast_args: str="arguments";break;
|
||||
case ast_default_arg: str="default_arg";break;
|
||||
case ast_dynamic_id: str="dynamic_id";break;
|
||||
case ast_and: str="and";break;
|
||||
case ast_or: str="or";break;
|
||||
case ast_equal: str="=";break;
|
||||
case ast_add_equal: str="+=";break;
|
||||
case ast_sub_equal: str="-=";break;
|
||||
case ast_mult_equal: str="*=";break;
|
||||
case ast_div_equal: str="/=";break;
|
||||
case ast_link_equal: str="~=";break;
|
||||
case ast_cmp_equal: str="==";break;
|
||||
case ast_cmp_not_equal:str="!=";break;
|
||||
case ast_less_than: str="<";break;
|
||||
case ast_less_equal: str="<=";break;
|
||||
case ast_greater_than: str=">";break;
|
||||
case ast_greater_equal:str=">=";break;
|
||||
case ast_add: str="+";break;
|
||||
case ast_sub: str="-";break;
|
||||
case ast_mult: str="*";break;
|
||||
case ast_div: str="/";break;
|
||||
case ast_link: str="~";break;
|
||||
case ast_unary_sub: str="unary-";break;
|
||||
case ast_unary_not: str="unary!";break;
|
||||
case ast_trinocular: str="trinocular";break;
|
||||
case ast_for: str="for";break;
|
||||
case ast_forindex: str="forindex";break;
|
||||
case ast_foreach: str="foreach";break;
|
||||
case ast_while: str="while";break;
|
||||
case ast_new_iter: str="new_iterator";break;
|
||||
case ast_conditional: str="conditional";break;
|
||||
case ast_if: str="if";break;
|
||||
case ast_elsif: str="elsif";break;
|
||||
case ast_else: str="else";break;
|
||||
case ast_multi_id: str="multi_id";break;
|
||||
case ast_multi_scalar: str="multi_scalar";break;
|
||||
case ast_definition: str="definition";break;
|
||||
case ast_multi_assign: str="multi_assignment";break;
|
||||
case ast_continue: str="continue";break;
|
||||
case ast_break: str="break";break;
|
||||
case ast_return: str="return";break;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
"null",
|
||||
"root","block",
|
||||
"nil","num","str","id","func","hash","vec",
|
||||
"hashmember","call","callh","callv","callf","subvec",
|
||||
"args","deflt_arg","dyn_id",
|
||||
"and","or",
|
||||
"=","+=","-=","*=","/=","~=",
|
||||
"==","!=",
|
||||
"<","<=",
|
||||
">",">=",
|
||||
"+","-","*","/","~",
|
||||
"unary-","unary!",
|
||||
"trino",
|
||||
"for","forindex","foreach","while","iter",
|
||||
"conditional","if","elsif","else",
|
||||
"multi_id","multi_scalar",
|
||||
"def","multi_assign",
|
||||
"continue","break","return"
|
||||
};
|
||||
|
||||
class nasal_ast
|
||||
{
|
||||
private:
|
||||
int line;
|
||||
int type;
|
||||
int line;
|
||||
int type;
|
||||
double num;
|
||||
std::string str;
|
||||
double num;
|
||||
std::vector<nasal_ast> children;
|
||||
public:
|
||||
nasal_ast(int,int);
|
||||
nasal_ast(){line=0;type=ast_null;}
|
||||
nasal_ast(int l,int t){line=l;type=t;}
|
||||
nasal_ast(const nasal_ast&);
|
||||
~nasal_ast();
|
||||
nasal_ast(nasal_ast&&);
|
||||
nasal_ast& operator=(const nasal_ast&);
|
||||
void clear();
|
||||
void set_line(int);
|
||||
void set_type(int);
|
||||
void set_str(std::string&);
|
||||
void set_num(double);
|
||||
void add_child(nasal_ast);
|
||||
int get_line();
|
||||
int get_type();
|
||||
std::string get_str();
|
||||
double get_num();
|
||||
std::vector<nasal_ast>& get_children();
|
||||
void print_ast(int);
|
||||
nasal_ast& operator=(nasal_ast&&);
|
||||
void print_ast(int);
|
||||
void clear();
|
||||
void add_child(nasal_ast&& ast){children.push_back(std::move(ast));}
|
||||
void set_line(int l){line=l;}
|
||||
void set_type(int t){type=t;}
|
||||
void set_str(std::string& s){str=s;}
|
||||
void set_num(double n){num=n;}
|
||||
int get_line(){return line;}
|
||||
int get_type(){return type;}
|
||||
double get_num(){return num;}
|
||||
std::string& get_str(){return str;}
|
||||
std::vector<nasal_ast>& get_children(){return children;}
|
||||
};
|
||||
|
||||
nasal_ast::nasal_ast(int init_line=0,int init_type=ast_null)
|
||||
{
|
||||
this->line=init_line;
|
||||
this->type=init_type;
|
||||
return;
|
||||
}
|
||||
|
||||
nasal_ast::nasal_ast(const nasal_ast& tmp)
|
||||
{
|
||||
this->line=tmp.line;
|
||||
this->type=tmp.type;
|
||||
this->str=tmp.str;
|
||||
this->num=tmp.num;
|
||||
this->children=tmp.children;
|
||||
line=tmp.line;
|
||||
type=tmp.type;
|
||||
str =tmp.str;
|
||||
num =tmp.num;
|
||||
children=tmp.children;
|
||||
return;
|
||||
}
|
||||
|
||||
nasal_ast::~nasal_ast()
|
||||
nasal_ast::nasal_ast(nasal_ast&& tmp)
|
||||
{
|
||||
this->children.clear();
|
||||
line=tmp.line;
|
||||
type=tmp.type;
|
||||
str.swap(tmp.str);
|
||||
num =tmp.num;
|
||||
children.swap(tmp.children);
|
||||
return;
|
||||
}
|
||||
|
||||
nasal_ast& nasal_ast::operator=(const nasal_ast& tmp)
|
||||
{
|
||||
this->line=tmp.line;
|
||||
this->type=tmp.type;
|
||||
this->str=tmp.str;
|
||||
this->num=tmp.num;
|
||||
this->children=tmp.children;
|
||||
line=tmp.line;
|
||||
type=tmp.type;
|
||||
str=tmp.str;
|
||||
num=tmp.num;
|
||||
children=tmp.children;
|
||||
return *this;
|
||||
}
|
||||
|
||||
nasal_ast& nasal_ast::operator=(nasal_ast&& tmp)
|
||||
{
|
||||
line=tmp.line;
|
||||
type=tmp.type;
|
||||
str.swap(tmp.str);
|
||||
num=tmp.num;
|
||||
children.swap(tmp.children);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void nasal_ast::clear()
|
||||
{
|
||||
this->line=0;
|
||||
this->str="";
|
||||
this->num=0;
|
||||
this->type=ast_null;
|
||||
this->children.clear();
|
||||
line=0;
|
||||
str="";
|
||||
num=0;
|
||||
type=ast_null;
|
||||
children.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_ast::set_line(int l)
|
||||
{
|
||||
this->line=l;
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_ast::set_type(int t)
|
||||
{
|
||||
this->type=t;
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_ast::set_str(std::string& s)
|
||||
{
|
||||
this->str=s;
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_ast::set_num(double n)
|
||||
{
|
||||
this->num=n;
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_ast::add_child(nasal_ast ast)
|
||||
{
|
||||
children.push_back(ast);
|
||||
return;
|
||||
}
|
||||
|
||||
int nasal_ast::get_line()
|
||||
{
|
||||
return this->line;
|
||||
}
|
||||
|
||||
int nasal_ast::get_type()
|
||||
{
|
||||
return this->type;
|
||||
}
|
||||
|
||||
std::string nasal_ast::get_str()
|
||||
{
|
||||
return this->str;
|
||||
}
|
||||
|
||||
double nasal_ast::get_num()
|
||||
{
|
||||
return this->num;
|
||||
}
|
||||
|
||||
std::vector<nasal_ast>& nasal_ast::get_children()
|
||||
{
|
||||
return this->children;
|
||||
}
|
||||
|
||||
void nasal_ast::print_ast(int depth)
|
||||
{
|
||||
std::string indentation="";
|
||||
for(int i=0;i<depth;++i) indentation+="| ";
|
||||
indentation+=ast_str(this->type);
|
||||
std::cout<<indentation;
|
||||
if(this->type==ast_string || this->type==ast_identifier || this->type==ast_dynamic_id || this->type==ast_call_hash)
|
||||
std::cout<<":"<<this->str;
|
||||
else if(this->type==ast_number)
|
||||
std::cout<<":"<<this->num;
|
||||
std::cout<<std::endl;
|
||||
int child_size=this->children.size();
|
||||
for(int i=0;i<child_size;++i)
|
||||
this->children[i].print_ast(depth+1);
|
||||
for(int i=0;i<depth;++i)
|
||||
std::cout<<"| ";
|
||||
std::cout<<ast_name[type];
|
||||
if(type==ast_str || type==ast_id || type==ast_default_arg || type==ast_dynamic_id || type==ast_callh)
|
||||
std::cout<<":"<<str;
|
||||
else if(type==ast_num)
|
||||
std::cout<<":"<<num;
|
||||
std::cout<<'\n';
|
||||
for(auto& i:children)
|
||||
i.print_ast(depth+1);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
1161
nasal_builtin.h
1161
nasal_builtin.h
File diff suppressed because it is too large
Load Diff
1466
nasal_bytecode_vm.h
1466
nasal_bytecode_vm.h
File diff suppressed because it is too large
Load Diff
1511
nasal_codegen.h
1511
nasal_codegen.h
File diff suppressed because it is too large
Load Diff
970
nasal_gc.h
970
nasal_gc.h
File diff suppressed because it is too large
Load Diff
112
nasal_import.h
112
nasal_import.h
@@ -4,53 +4,34 @@
|
||||
class nasal_import
|
||||
{
|
||||
private:
|
||||
nasal_lexer import_lex;
|
||||
nasal_parse import_par;
|
||||
nasal_ast import_ast;
|
||||
std::vector<std::string> filename_table;
|
||||
int error;
|
||||
void die(std::string,std::string);
|
||||
void init();
|
||||
nasal_lexer import_lex;
|
||||
nasal_parse import_par;
|
||||
nasal_ast import_ast;
|
||||
std::vector<std::string> filename_table;
|
||||
void die(std::string&,const char*);
|
||||
bool check_import(nasal_ast&);
|
||||
bool check_exist(std::string);
|
||||
void linker(nasal_ast&,nasal_ast&);
|
||||
bool check_exist(std::string&);
|
||||
void linker(nasal_ast&,nasal_ast&&);
|
||||
nasal_ast file_import(nasal_ast&);
|
||||
nasal_ast load(nasal_ast&);
|
||||
public:
|
||||
nasal_import();
|
||||
int get_error();
|
||||
int get_error(){return error;}
|
||||
void link(nasal_ast&);
|
||||
nasal_ast& get_root();
|
||||
nasal_ast& get_root(){return import_ast;}
|
||||
};
|
||||
|
||||
nasal_import::nasal_import()
|
||||
{
|
||||
import_lex.clear();
|
||||
import_par.clear();
|
||||
import_ast.clear();
|
||||
filename_table.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_import::die(std::string filename,std::string error_stage)
|
||||
void nasal_import::die(std::string& filename,const char* error_stage)
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [import] in <\""<<filename<<"\">: error(s) occurred in "<<error_stage<<"."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_import::init()
|
||||
{
|
||||
import_lex.clear();
|
||||
import_par.clear();
|
||||
std::cout<<">> [import] in <\""<<filename<<"\">: error(s) occurred in "<<error_stage<<".\n";
|
||||
return;
|
||||
}
|
||||
|
||||
bool nasal_import::check_import(nasal_ast& node)
|
||||
{
|
||||
/*
|
||||
only this kind of node can be recognized as 'import':
|
||||
|
||||
only this kind of node can be recognized as 'import':
|
||||
call
|
||||
id:import
|
||||
call_func
|
||||
@@ -63,46 +44,39 @@ bool nasal_import::check_import(nasal_ast& node)
|
||||
return false;
|
||||
if(ref_vec[0].get_str()!="import")
|
||||
return false;
|
||||
if(ref_vec[1].get_type()!=ast_call_func)
|
||||
if(ref_vec[1].get_type()!=ast_callf)
|
||||
return false;
|
||||
if(ref_vec[1].get_children().size()!=1 || ref_vec[1].get_children()[0].get_type()!=ast_string)
|
||||
if(ref_vec[1].get_children().size()!=1 || ref_vec[1].get_children()[0].get_type()!=ast_str)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool nasal_import::check_exist(std::string filename)
|
||||
bool nasal_import::check_exist(std::string& file)
|
||||
{
|
||||
// avoid importing the same file
|
||||
int size=filename_table.size();
|
||||
for(int i=0;i<size;++i)
|
||||
if(filename==filename_table[i])
|
||||
for(auto& fname:filename_table)
|
||||
if(file==fname)
|
||||
return true;
|
||||
filename_table.push_back(filename);
|
||||
filename_table.push_back(file);
|
||||
return false;
|
||||
}
|
||||
|
||||
void nasal_import::linker(nasal_ast& root,nasal_ast& add_root)
|
||||
void nasal_import::linker(nasal_ast& root,nasal_ast&& add_root)
|
||||
{
|
||||
// add children of add_root to the back of root
|
||||
std::vector<nasal_ast>& ref_vec=add_root.get_children();
|
||||
int size=ref_vec.size();
|
||||
for(int i=0;i<size;++i)
|
||||
root.add_child(ref_vec[i]);
|
||||
for(auto& i:add_root.get_children())
|
||||
root.add_child(std::move(i));
|
||||
return;
|
||||
}
|
||||
|
||||
nasal_ast nasal_import::file_import(nasal_ast& node)
|
||||
{
|
||||
// initializing
|
||||
nasal_ast tmp;
|
||||
tmp.set_line(0);
|
||||
tmp.set_type(ast_root);
|
||||
init();
|
||||
nasal_ast tmp(0,ast_root);
|
||||
|
||||
// get filename and set node to ast_null
|
||||
std::string filename=node.get_children()[1].get_children()[0].get_str();
|
||||
node.clear();
|
||||
node.set_type(ast_null);
|
||||
|
||||
// avoid infinite loading loop
|
||||
if(check_exist(filename))
|
||||
@@ -113,43 +87,31 @@ nasal_ast nasal_import::file_import(nasal_ast& node)
|
||||
import_lex.scanner();
|
||||
if(import_lex.get_error())
|
||||
{
|
||||
this->die(filename,"lexer");
|
||||
die(filename,"lexer");
|
||||
return tmp;
|
||||
}
|
||||
import_par.set_toklist(import_lex.get_token_list());
|
||||
import_lex.get_token_list().clear();
|
||||
import_par.main_process();
|
||||
if(import_par.get_error())
|
||||
{
|
||||
this->die(filename,"parser");
|
||||
die(filename,"parser");
|
||||
return tmp;
|
||||
}
|
||||
tmp=import_par.get_root();
|
||||
|
||||
tmp=std::move(import_par.get_root());
|
||||
import_par.get_root().clear();
|
||||
// check if tmp has 'import'
|
||||
return load(tmp);
|
||||
}
|
||||
|
||||
nasal_ast nasal_import::load(nasal_ast& root)
|
||||
{
|
||||
nasal_ast new_root;
|
||||
new_root.set_line(0);
|
||||
new_root.set_type(ast_root);
|
||||
|
||||
std::vector<nasal_ast>& ref_vec=root.get_children();
|
||||
int size=ref_vec.size();
|
||||
for(int i=0;i<size;++i)
|
||||
{
|
||||
if(check_import(ref_vec[i]))
|
||||
{
|
||||
nasal_ast tmp=file_import(ref_vec[i]);
|
||||
// add tmp to the back of new_root
|
||||
linker(new_root,tmp);
|
||||
}
|
||||
}
|
||||
nasal_ast new_root(0,ast_root);
|
||||
for(auto& i:root.get_children())
|
||||
if(check_import(i))
|
||||
linker(new_root,file_import(i));
|
||||
// add root to the back of new_root
|
||||
linker(new_root,root);
|
||||
|
||||
// oops,i think it is not efficient if the root is too ... large?
|
||||
linker(new_root,std::move(root));
|
||||
return new_root;
|
||||
}
|
||||
|
||||
@@ -164,14 +126,4 @@ void nasal_import::link(nasal_ast& root)
|
||||
return;
|
||||
}
|
||||
|
||||
nasal_ast& nasal_import::get_root()
|
||||
{
|
||||
return import_ast;
|
||||
}
|
||||
|
||||
int nasal_import::get_error()
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
#endif
|
||||
371
nasal_lexer.h
371
nasal_lexer.h
@@ -7,7 +7,7 @@
|
||||
#define IS_DIGIT(c) ('0'<=c&&c<='9')
|
||||
#define IS_STRING(c) (c=='\''||c=='\"'||c=='`')
|
||||
// single operators have only one character
|
||||
#define IS_SINGLE_OPRATOR(c) (c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}'||c==','||c==';'||c=='|'||c==':'||\
|
||||
#define IS_SINGLE_OPERATOR(c) (c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}'||c==','||c==';'||c=='|'||c==':'||\
|
||||
c=='?'||c=='`'||c=='&'||c=='@'||c=='%'||c=='$'||c=='^'||c=='\\')
|
||||
// calculation operators may have two chars, for example: += -= *= /= ~= != == >= <=
|
||||
#define IS_CALC_OPERATOR(c) (c=='='||c=='+'||c=='-'||c=='*'||c=='!'||c=='/'||c=='<'||c=='>'||c=='~')
|
||||
@@ -16,18 +16,19 @@
|
||||
enum token_type
|
||||
{
|
||||
tok_null=0,
|
||||
tok_number,tok_string,tok_identifier,
|
||||
tok_num,tok_str,tok_id,
|
||||
tok_for,tok_forindex,tok_foreach,tok_while,
|
||||
tok_var,tok_func,tok_break,tok_continue,
|
||||
tok_return,tok_if,tok_elsif,tok_else,tok_nil,
|
||||
tok_left_curve,tok_right_curve,
|
||||
tok_left_bracket,tok_right_bracket,
|
||||
tok_left_brace,tok_right_brace,
|
||||
tok_ret,tok_if,tok_elsif,tok_else,tok_nil,
|
||||
tok_lcurve,tok_rcurve,
|
||||
tok_lbracket,tok_rbracket,
|
||||
tok_lbrace,tok_rbrace,
|
||||
tok_semi,tok_and,tok_or,tok_comma,tok_dot,tok_ellipsis,tok_quesmark,
|
||||
tok_colon,tok_add,tok_sub,tok_mult,tok_div,tok_link,tok_not,
|
||||
tok_equal,
|
||||
tok_add_equal,tok_sub_equal,tok_mult_equal,tok_div_equal,tok_link_equal,
|
||||
tok_cmp_equal,tok_cmp_not_equal,tok_less_than,tok_less_equal,tok_greater_than,tok_greater_equal
|
||||
tok_eq,
|
||||
tok_addeq,tok_subeq,tok_multeq,tok_diveq,tok_lnkeq,
|
||||
tok_cmpeq,tok_neq,tok_less,tok_leq,tok_grt,tok_geq,
|
||||
tok_eof
|
||||
};
|
||||
|
||||
struct
|
||||
@@ -36,52 +37,52 @@ struct
|
||||
int tok_type;
|
||||
}token_table[]=
|
||||
{
|
||||
{"for" ,tok_for },
|
||||
{"forindex",tok_forindex },
|
||||
{"foreach" ,tok_foreach },
|
||||
{"while" ,tok_while },
|
||||
{"var" ,tok_var },
|
||||
{"func" ,tok_func },
|
||||
{"break" ,tok_break },
|
||||
{"continue",tok_continue },
|
||||
{"return" ,tok_return },
|
||||
{"if" ,tok_if },
|
||||
{"elsif" ,tok_elsif },
|
||||
{"else" ,tok_else },
|
||||
{"nil" ,tok_nil },
|
||||
{"(" ,tok_left_curve },
|
||||
{")" ,tok_right_curve },
|
||||
{"[" ,tok_left_bracket },
|
||||
{"]" ,tok_right_bracket},
|
||||
{"{" ,tok_left_brace },
|
||||
{"}" ,tok_right_brace },
|
||||
{";" ,tok_semi },
|
||||
{"and" ,tok_and },
|
||||
{"or" ,tok_or },
|
||||
{"," ,tok_comma },
|
||||
{"." ,tok_dot },
|
||||
{"..." ,tok_ellipsis },
|
||||
{"?" ,tok_quesmark },
|
||||
{":" ,tok_colon },
|
||||
{"+" ,tok_add },
|
||||
{"-" ,tok_sub },
|
||||
{"*" ,tok_mult },
|
||||
{"/" ,tok_div },
|
||||
{"~" ,tok_link },
|
||||
{"!" ,tok_not },
|
||||
{"=" ,tok_equal },
|
||||
{"+=" ,tok_add_equal },
|
||||
{"-=" ,tok_sub_equal },
|
||||
{"*=" ,tok_mult_equal },
|
||||
{"/=" ,tok_div_equal },
|
||||
{"~=" ,tok_link_equal },
|
||||
{"==" ,tok_cmp_equal },
|
||||
{"!=" ,tok_cmp_not_equal},
|
||||
{"<" ,tok_less_than },
|
||||
{"<=" ,tok_less_equal },
|
||||
{">" ,tok_greater_than },
|
||||
{">=" ,tok_greater_equal},
|
||||
{NULL ,-1 }
|
||||
{"for" ,tok_for },
|
||||
{"forindex",tok_forindex },
|
||||
{"foreach" ,tok_foreach },
|
||||
{"while" ,tok_while },
|
||||
{"var" ,tok_var },
|
||||
{"func" ,tok_func },
|
||||
{"break" ,tok_break },
|
||||
{"continue",tok_continue },
|
||||
{"return" ,tok_ret },
|
||||
{"if" ,tok_if },
|
||||
{"elsif" ,tok_elsif },
|
||||
{"else" ,tok_else },
|
||||
{"nil" ,tok_nil },
|
||||
{"(" ,tok_lcurve },
|
||||
{")" ,tok_rcurve },
|
||||
{"[" ,tok_lbracket },
|
||||
{"]" ,tok_rbracket },
|
||||
{"{" ,tok_lbrace },
|
||||
{"}" ,tok_rbrace },
|
||||
{";" ,tok_semi },
|
||||
{"and" ,tok_and },
|
||||
{"or" ,tok_or },
|
||||
{"," ,tok_comma },
|
||||
{"." ,tok_dot },
|
||||
{"..." ,tok_ellipsis },
|
||||
{"?" ,tok_quesmark },
|
||||
{":" ,tok_colon },
|
||||
{"+" ,tok_add },
|
||||
{"-" ,tok_sub },
|
||||
{"*" ,tok_mult },
|
||||
{"/" ,tok_div },
|
||||
{"~" ,tok_link },
|
||||
{"!" ,tok_not },
|
||||
{"=" ,tok_eq },
|
||||
{"+=" ,tok_addeq },
|
||||
{"-=" ,tok_subeq },
|
||||
{"*=" ,tok_multeq },
|
||||
{"/=" ,tok_diveq },
|
||||
{"~=" ,tok_lnkeq },
|
||||
{"==" ,tok_cmpeq },
|
||||
{"!=" ,tok_neq },
|
||||
{"<" ,tok_less },
|
||||
{"<=" ,tok_leq },
|
||||
{">" ,tok_grt },
|
||||
{">=" ,tok_geq },
|
||||
{nullptr ,-1 }
|
||||
};
|
||||
|
||||
struct token
|
||||
@@ -89,6 +90,7 @@ struct token
|
||||
int line;
|
||||
int type;
|
||||
std::string str;
|
||||
token(int l=0,int t=tok_null,std::string s=""){line=l;type=t;str=s;return;}
|
||||
};
|
||||
|
||||
class nasal_lexer
|
||||
@@ -101,32 +103,20 @@ private:
|
||||
std::string line_code;
|
||||
std::vector<char> res;
|
||||
std::vector<token> token_list;
|
||||
std::string identifier_gen();
|
||||
std::string number_gen();
|
||||
std::string string_gen();
|
||||
int get_tok_type(std::string&);
|
||||
void die(const char*);
|
||||
std::string id_gen();
|
||||
std::string num_gen();
|
||||
std::string str_gen();
|
||||
public:
|
||||
void clear();
|
||||
void openfile(std::string);
|
||||
void die(std::string,int,int);
|
||||
void openfile(std::string&);
|
||||
void scanner();
|
||||
void print_token();
|
||||
int get_error();
|
||||
std::vector<token>& get_token_list();
|
||||
int get_error(){return error;}
|
||||
std::vector<token>& get_token_list(){return token_list;}
|
||||
};
|
||||
|
||||
void nasal_lexer::clear()
|
||||
{
|
||||
error=0;
|
||||
res_size=0;
|
||||
line=0;
|
||||
ptr=0;
|
||||
line_code="";
|
||||
res.clear();
|
||||
token_list.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_lexer::openfile(std::string filename)
|
||||
void nasal_lexer::openfile(std::string& filename)
|
||||
{
|
||||
error=0;
|
||||
res.clear();
|
||||
@@ -135,7 +125,7 @@ void nasal_lexer::openfile(std::string filename)
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [lexer] cannot open file \""<<filename<<"\".\n";
|
||||
fin.close();
|
||||
fin.close();
|
||||
return;
|
||||
}
|
||||
while(!fin.eof())
|
||||
@@ -146,18 +136,28 @@ void nasal_lexer::openfile(std::string filename)
|
||||
res.push_back(c);
|
||||
}
|
||||
fin.close();
|
||||
res_size=res.size();
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_lexer::die(std::string error_info,int line=-1,int column=-1)
|
||||
int nasal_lexer::get_tok_type(std::string& tk_str)
|
||||
{
|
||||
for(int i=0;token_table[i].str;++i)
|
||||
if(tk_str==token_table[i].str)
|
||||
return token_table[i].tok_type;
|
||||
return tok_null;
|
||||
}
|
||||
|
||||
void nasal_lexer::die(const char* error_info)
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [lexer] line "<<line<<" column "<<column<<": "<<error_info<<"\n";
|
||||
std::cout<<">> [lexer] line "<<line<<" column "<<line_code.length()<<": \n"<<line_code<<"\n";
|
||||
for(auto i:line_code)
|
||||
std::cout<<(i=='\t'?'\t':' ');
|
||||
std::cout<<"^"<<error_info<<'\n';
|
||||
return;
|
||||
}
|
||||
|
||||
std::string nasal_lexer::identifier_gen()
|
||||
std::string nasal_lexer::id_gen()
|
||||
{
|
||||
std::string token_str="";
|
||||
while(ptr<res_size && (IS_IDENTIFIER(res[ptr])||IS_DIGIT(res[ptr])))
|
||||
@@ -167,91 +167,62 @@ std::string nasal_lexer::identifier_gen()
|
||||
// after running this process, ptr will point to the next token's beginning character
|
||||
}
|
||||
|
||||
std::string nasal_lexer::number_gen()
|
||||
std::string nasal_lexer::num_gen()
|
||||
{
|
||||
bool scientific_notation=false;// numbers like 1e8 are scientific_notation
|
||||
std::string token_str="";
|
||||
// generate hex number
|
||||
if(res[ptr]=='0' && ptr+1<res_size && res[ptr+1]=='x')
|
||||
if(ptr+1<res_size && res[ptr]=='0' && res[ptr+1]=='x')
|
||||
{
|
||||
token_str="0x";
|
||||
std::string token_str="0x";
|
||||
ptr+=2;
|
||||
while(ptr<res_size && IS_HEX_NUMBER(res[ptr]))
|
||||
token_str+=res[ptr++];
|
||||
line_code+=token_str;
|
||||
if(token_str=="0x")
|
||||
{
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
return "0";
|
||||
}
|
||||
die("incorrect number.");
|
||||
return token_str;
|
||||
}
|
||||
// generate oct number
|
||||
else if(res[ptr]=='0' && ptr+1<res_size && res[ptr+1]=='o')
|
||||
else if(ptr+1<res_size && res[ptr]=='0' && res[ptr+1]=='o')
|
||||
{
|
||||
token_str="0o";
|
||||
std::string token_str="0o";
|
||||
ptr+=2;
|
||||
while(ptr<res_size && IS_OCT_NUMEBR(res[ptr]))
|
||||
token_str+=res[ptr++];
|
||||
line_code+=token_str;
|
||||
if(token_str=="0o")
|
||||
{
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
return "0";
|
||||
}
|
||||
die("incorrect number.");
|
||||
return token_str;
|
||||
}
|
||||
// generate dec number
|
||||
// dec number -> [0~9][0~9]*(.[0~9]*)(e|E(+|-)0|[1~9][0~9]*)
|
||||
std::string token_str="";
|
||||
while(ptr<res_size && IS_DIGIT(res[ptr]))
|
||||
token_str+=res[ptr++];
|
||||
if(ptr<res_size && res[ptr]=='.')
|
||||
{
|
||||
token_str+=res[ptr++];
|
||||
// "xxxx." is not a correct number
|
||||
if(ptr>=res_size)
|
||||
{
|
||||
line_code+=token_str;
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
return "0";
|
||||
}
|
||||
while(ptr<res_size && IS_DIGIT(res[ptr]))
|
||||
token_str+=res[ptr++];
|
||||
// "xxxx." is not a correct number
|
||||
if(token_str.back()=='.')
|
||||
{
|
||||
line_code+=token_str;
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
die("incorrect number.");
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
if(ptr<res_size && (res[ptr]=='e' || res[ptr]=='E'))
|
||||
{
|
||||
token_str+=res[ptr++];
|
||||
// "xxxe" is not a correct number
|
||||
if(ptr>=res_size)
|
||||
{
|
||||
line_code+=token_str;
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
return "0";
|
||||
}
|
||||
if(ptr<res_size && (res[ptr]=='-' || res[ptr]=='+'))
|
||||
token_str+=res[ptr++];
|
||||
if(ptr>=res_size)
|
||||
{
|
||||
line_code+=token_str;
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
return "0";
|
||||
}
|
||||
if(ptr<res_size && res[ptr]=='0')
|
||||
token_str+=res[ptr++];
|
||||
while(ptr<res_size && IS_DIGIT(res[ptr]))
|
||||
token_str+=res[ptr++];
|
||||
// "xxxe(-|+)" is not a correct number
|
||||
if(token_str.back()=='e' || token_str.back()=='E' || token_str.back()=='-' || token_str.back()=='+')
|
||||
{
|
||||
line_code+=token_str;
|
||||
die("["+line_code+"_] incorrect number.",line,line_code.length());
|
||||
die("incorrect number.");
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
@@ -259,12 +230,12 @@ std::string nasal_lexer::number_gen()
|
||||
return token_str;
|
||||
}
|
||||
|
||||
std::string nasal_lexer::string_gen()
|
||||
std::string nasal_lexer::str_gen()
|
||||
{
|
||||
std::string token_str="";
|
||||
line_code+=res[ptr];
|
||||
char str_begin=res[ptr++];
|
||||
while(ptr<res_size && res[ptr]!=str_begin)
|
||||
char str_begin=res[ptr];
|
||||
line_code+=str_begin;
|
||||
while(++ptr<res_size && res[ptr]!=str_begin)
|
||||
{
|
||||
line_code+=res[ptr];
|
||||
if(res[ptr]=='\n')
|
||||
@@ -274,33 +245,32 @@ std::string nasal_lexer::string_gen()
|
||||
}
|
||||
if(res[ptr]=='\\' && ptr+1<res_size)
|
||||
{
|
||||
++ptr;
|
||||
line_code+=res[ptr];
|
||||
line_code+=res[++ptr];
|
||||
switch(res[ptr])
|
||||
{
|
||||
case 'a':token_str.push_back('\a');break;
|
||||
case 'b':token_str.push_back('\b');break;
|
||||
case 'f':token_str.push_back('\f');break;
|
||||
case 'n':token_str.push_back('\n');break;
|
||||
case 'r':token_str.push_back('\r');break;
|
||||
case 't':token_str.push_back('\t');break;
|
||||
case 'v':token_str.push_back('\v');break;
|
||||
case '?':token_str.push_back('\?');break;
|
||||
case '0':token_str.push_back('\0');break;
|
||||
case 'a': token_str.push_back('\a');break;
|
||||
case 'b': token_str.push_back('\b');break;
|
||||
case 'f': token_str.push_back('\f');break;
|
||||
case 'n': token_str.push_back('\n');break;
|
||||
case 'r': token_str.push_back('\r');break;
|
||||
case 't': token_str.push_back('\t');break;
|
||||
case 'v': token_str.push_back('\v');break;
|
||||
case '?': token_str.push_back('\?');break;
|
||||
case '0': token_str.push_back('\0');break;
|
||||
case '\\':token_str.push_back('\\');break;
|
||||
case '\'':token_str.push_back('\'');break;
|
||||
case '\"':token_str.push_back('\"');break;
|
||||
default: token_str.push_back(res[ptr]);break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else
|
||||
token_str+=res[ptr];
|
||||
++ptr;
|
||||
token_str+=res[ptr];
|
||||
}
|
||||
// check if this string ends with a " or '
|
||||
if(ptr>=res_size)
|
||||
die("["+line_code+"_] get EOF when generating string.",line,line_code.length());
|
||||
++ptr;
|
||||
if(ptr++>=res_size)
|
||||
die("get EOF when generating string.");
|
||||
if(str_begin=='`' && token_str.length()!=1)
|
||||
die("\'`\' is used for string that includes one character.");
|
||||
return token_str;
|
||||
}
|
||||
|
||||
@@ -310,6 +280,7 @@ void nasal_lexer::scanner()
|
||||
line=1;
|
||||
ptr=0;
|
||||
line_code="";
|
||||
res_size=res.size();
|
||||
|
||||
std::string token_str;
|
||||
while(ptr<res_size)
|
||||
@@ -318,67 +289,39 @@ void nasal_lexer::scanner()
|
||||
{
|
||||
// these characters will be ignored, and '\n' will cause ++line
|
||||
line_code+=res[ptr];
|
||||
if(res[ptr]=='\n')
|
||||
if(res[ptr++]=='\n')
|
||||
{
|
||||
++line;
|
||||
line_code="";
|
||||
}
|
||||
++ptr;
|
||||
}
|
||||
if(ptr>=res_size) break;
|
||||
if(IS_IDENTIFIER(res[ptr]))
|
||||
{
|
||||
token_str=identifier_gen();
|
||||
token new_token;
|
||||
new_token.line=line;
|
||||
new_token.str=token_str;
|
||||
new_token.type=0;
|
||||
for(int i=0;token_table[i].str;++i)
|
||||
if(token_str==token_table[i].str)
|
||||
{
|
||||
new_token.type=token_table[i].tok_type;
|
||||
break;
|
||||
}
|
||||
token_str=id_gen();
|
||||
token new_token(line,get_tok_type(token_str),token_str);
|
||||
if(!new_token.type)
|
||||
new_token.type=tok_identifier;
|
||||
new_token.type=tok_id;
|
||||
token_list.push_back(new_token);
|
||||
}
|
||||
else if(IS_DIGIT(res[ptr]))
|
||||
{
|
||||
token_str=number_gen();
|
||||
token new_token;
|
||||
new_token.line=line;
|
||||
new_token.str=token_str;
|
||||
new_token.type=tok_number;
|
||||
token_list.push_back(new_token);
|
||||
token_str=num_gen();
|
||||
token_list.push_back({line,tok_num,token_str});
|
||||
}
|
||||
else if(IS_STRING(res[ptr]))
|
||||
{
|
||||
token_str=string_gen();
|
||||
token new_token;
|
||||
new_token.line=line;
|
||||
new_token.type=tok_string;
|
||||
new_token.str=token_str;
|
||||
token_list.push_back(new_token);
|
||||
token_str=str_gen();
|
||||
token_list.push_back({line,tok_str,token_str});
|
||||
}
|
||||
else if(IS_SINGLE_OPRATOR(res[ptr]))
|
||||
else if(IS_SINGLE_OPERATOR(res[ptr]))
|
||||
{
|
||||
token_str="";
|
||||
token_str+=res[ptr];
|
||||
token_str=res[ptr];
|
||||
line_code+=res[ptr];
|
||||
token new_token;
|
||||
new_token.line=line;
|
||||
new_token.str=token_str;
|
||||
new_token.type=-1;
|
||||
for(int i=0;token_table[i].str;++i)
|
||||
if(token_str==token_table[i].str)
|
||||
{
|
||||
new_token.type=token_table[i].tok_type;
|
||||
break;
|
||||
}
|
||||
if(new_token.type<0)
|
||||
die("["+line_code+"_] incorrect operator.",line,line_code.length());
|
||||
token_list.push_back(new_token);
|
||||
int type=get_tok_type(token_str);
|
||||
if(!type)
|
||||
die("incorrect operator.");
|
||||
token_list.push_back({line,type,token_str});
|
||||
++ptr;
|
||||
}
|
||||
else if(res[ptr]=='.')
|
||||
@@ -394,71 +337,35 @@ void nasal_lexer::scanner()
|
||||
++ptr;
|
||||
}
|
||||
line_code+=token_str;
|
||||
token new_token;
|
||||
new_token.line=line;
|
||||
new_token.str=token_str;
|
||||
for(int i=0;token_table[i].str;++i)
|
||||
if(token_str==token_table[i].str)
|
||||
{
|
||||
new_token.type=token_table[i].tok_type;
|
||||
break;
|
||||
}
|
||||
token_list.push_back(new_token);
|
||||
token_list.push_back({line,get_tok_type(token_str),token_str});
|
||||
}
|
||||
else if(IS_CALC_OPERATOR(res[ptr]))
|
||||
{
|
||||
// get calculation operator
|
||||
token_str=res[ptr];
|
||||
++ptr;
|
||||
if(ptr<res.size() && res[ptr]=='=')
|
||||
{
|
||||
token_str+=res[ptr];
|
||||
++ptr;
|
||||
}
|
||||
token_str=res[ptr++];
|
||||
if(ptr<res_size && res[ptr]=='=')
|
||||
token_str+=res[ptr++];
|
||||
line_code+=token_str;
|
||||
token new_token;
|
||||
new_token.line=line;
|
||||
new_token.str=token_str;
|
||||
for(int i=0;token_table[i].str;++i)
|
||||
if(token_str==token_table[i].str)
|
||||
{
|
||||
new_token.type=token_table[i].tok_type;
|
||||
break;
|
||||
}
|
||||
token_list.push_back(new_token);
|
||||
}
|
||||
else if(IS_NOTE(res[ptr]))
|
||||
{
|
||||
// avoid note
|
||||
while(ptr<res_size && res[ptr]!='\n') ++ptr;
|
||||
// after this process ptr will point to a '\n'
|
||||
// don't ++ptr then the counter for line can work correctly
|
||||
token_list.push_back({line,get_tok_type(token_str),token_str});
|
||||
}
|
||||
else if(IS_NOTE(res[ptr]))// avoid note, after this process ptr will point to a '\n', so next loop line counter+1
|
||||
while(++ptr<res_size && res[ptr]!='\n');
|
||||
else
|
||||
{
|
||||
line_code+=res[ptr];
|
||||
die("["+line_code+"_] unknown character.",line,line_code.length());
|
||||
++ptr;
|
||||
line_code+=res[ptr++];
|
||||
die("unknown character.");
|
||||
}
|
||||
}
|
||||
token_list.push_back({line,tok_eof,""});
|
||||
res.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_lexer::print_token()
|
||||
{
|
||||
int size=token_list.size();
|
||||
for(int i=0;i<size;++i)
|
||||
std::cout<<"("<<token_list[i].line<<" | "<<token_list[i].str<<")\n";
|
||||
for(auto tok:token_list)
|
||||
std::cout<<"("<<tok.line<<" | "<<tok.str<<")\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int nasal_lexer::get_error()
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
std::vector<token>& nasal_lexer::get_token_list()
|
||||
{
|
||||
return token_list;
|
||||
}
|
||||
#endif
|
||||
1658
nasal_parse.h
1658
nasal_parse.h
File diff suppressed because it is too large
Load Diff
1896
nasal_runtime.h
1896
nasal_runtime.h
File diff suppressed because it is too large
Load Diff
850
nasal_vm.h
Normal file
850
nasal_vm.h
Normal file
@@ -0,0 +1,850 @@
|
||||
#ifndef __NASAL_VM_H__
|
||||
#define __NASAL_VM_H__
|
||||
|
||||
class nasal_vm
|
||||
{
|
||||
private:
|
||||
/* reference from nasal_gc */
|
||||
nasal_val**& stack_top;// stack top
|
||||
/* values of nasal_vm */
|
||||
uint32_t pc; // program counter
|
||||
bool loop_mark;// when mark is false,break the main loop
|
||||
std::stack<int> ret; // ptr stack stores address for function to return
|
||||
std::stack<int> counter; // iterator stack for forindex/foreach
|
||||
std::vector<std::string> str_table;// symbols used in process
|
||||
std::vector<void (nasal_vm::*)()>
|
||||
exec_code;//function pointer
|
||||
std::vector<uint32_t> imm; // immediate number
|
||||
nasal_val** mem_addr; // used for mem_call
|
||||
nasal_gc gc; // garbage collector
|
||||
|
||||
void die(std::string);
|
||||
bool condition(nasal_val*);
|
||||
void opr_nop();
|
||||
void opr_intg();
|
||||
void opr_intl();
|
||||
void opr_offset();
|
||||
void opr_loadg();
|
||||
void opr_loadl();
|
||||
void opr_pnum();
|
||||
void opr_pone();
|
||||
void opr_pzero();
|
||||
void opr_pnil();
|
||||
void opr_pstr();
|
||||
void opr_newv();
|
||||
void opr_newh();
|
||||
void opr_newf();
|
||||
void opr_happ();
|
||||
void opr_para();
|
||||
void opr_defpara();
|
||||
void opr_dynpara();
|
||||
void opr_unot();
|
||||
void opr_usub();
|
||||
void opr_add();
|
||||
void opr_sub();
|
||||
void opr_mul();
|
||||
void opr_div();
|
||||
void opr_lnk();
|
||||
void opr_addeq();
|
||||
void opr_subeq();
|
||||
void opr_muleq();
|
||||
void opr_diveq();
|
||||
void opr_lnkeq();
|
||||
void opr_meq();
|
||||
void opr_eq();
|
||||
void opr_neq();
|
||||
void opr_less();
|
||||
void opr_leq();
|
||||
void opr_grt();
|
||||
void opr_geq();
|
||||
void opr_pop();
|
||||
void opr_jmp();
|
||||
void opr_jt();
|
||||
void opr_jf();
|
||||
void opr_counter();
|
||||
void opr_cntpop();
|
||||
void opr_findex();
|
||||
void opr_feach();
|
||||
void opr_callg();
|
||||
void opr_calll();
|
||||
void opr_callv();
|
||||
void opr_callvi();
|
||||
void opr_callh();
|
||||
void opr_callfv();
|
||||
void opr_callfh();
|
||||
void opr_callb();
|
||||
void opr_slcbegin();
|
||||
void opr_slcend();
|
||||
void opr_slc();
|
||||
void opr_slc2();
|
||||
void opr_mcallg();
|
||||
void opr_mcalll();
|
||||
void opr_mcallv();
|
||||
void opr_mcallh();
|
||||
void opr_ret();
|
||||
public:
|
||||
nasal_vm():stack_top(gc.stack_top){};
|
||||
void init(
|
||||
std::vector<std::string>&,
|
||||
std::vector<double>&,
|
||||
std::vector<opcode>&);
|
||||
void clear();
|
||||
void run();
|
||||
};
|
||||
|
||||
void nasal_vm::init(
|
||||
std::vector<std::string>& strs,
|
||||
std::vector<double>& nums,
|
||||
std::vector<opcode>& exec)
|
||||
{
|
||||
gc.gc_init(nums,strs);
|
||||
gc.val_stack[STACK_MAX_DEPTH-1]=nullptr;
|
||||
str_table=strs; // get constant strings & symbols
|
||||
void (nasal_vm::*opr_table[])()=
|
||||
{
|
||||
&nasal_vm::opr_nop,
|
||||
&nasal_vm::opr_intg,
|
||||
&nasal_vm::opr_intl,
|
||||
&nasal_vm::opr_offset,
|
||||
&nasal_vm::opr_loadg,
|
||||
&nasal_vm::opr_loadl,
|
||||
&nasal_vm::opr_pnum,
|
||||
&nasal_vm::opr_pone,
|
||||
&nasal_vm::opr_pzero,
|
||||
&nasal_vm::opr_pnil,
|
||||
&nasal_vm::opr_pstr,
|
||||
&nasal_vm::opr_newv,
|
||||
&nasal_vm::opr_newh,
|
||||
&nasal_vm::opr_newf,
|
||||
&nasal_vm::opr_happ,
|
||||
&nasal_vm::opr_para,
|
||||
&nasal_vm::opr_defpara,
|
||||
&nasal_vm::opr_dynpara,
|
||||
&nasal_vm::opr_unot,
|
||||
&nasal_vm::opr_usub,
|
||||
&nasal_vm::opr_add,
|
||||
&nasal_vm::opr_sub,
|
||||
&nasal_vm::opr_mul,
|
||||
&nasal_vm::opr_div,
|
||||
&nasal_vm::opr_lnk,
|
||||
&nasal_vm::opr_addeq,
|
||||
&nasal_vm::opr_subeq,
|
||||
&nasal_vm::opr_muleq,
|
||||
&nasal_vm::opr_diveq,
|
||||
&nasal_vm::opr_lnkeq,
|
||||
&nasal_vm::opr_meq,
|
||||
&nasal_vm::opr_eq,
|
||||
&nasal_vm::opr_neq,
|
||||
&nasal_vm::opr_less,
|
||||
&nasal_vm::opr_leq,
|
||||
&nasal_vm::opr_grt,
|
||||
&nasal_vm::opr_geq,
|
||||
&nasal_vm::opr_pop,
|
||||
&nasal_vm::opr_jmp,
|
||||
&nasal_vm::opr_jt,
|
||||
&nasal_vm::opr_jf,
|
||||
&nasal_vm::opr_counter,
|
||||
&nasal_vm::opr_cntpop,
|
||||
&nasal_vm::opr_findex,
|
||||
&nasal_vm::opr_feach,
|
||||
&nasal_vm::opr_callg,
|
||||
&nasal_vm::opr_calll,
|
||||
&nasal_vm::opr_callv,
|
||||
&nasal_vm::opr_callvi,
|
||||
&nasal_vm::opr_callh,
|
||||
&nasal_vm::opr_callfv,
|
||||
&nasal_vm::opr_callfh,
|
||||
&nasal_vm::opr_callb,
|
||||
&nasal_vm::opr_slcbegin,
|
||||
&nasal_vm::opr_slcend,
|
||||
&nasal_vm::opr_slc,
|
||||
&nasal_vm::opr_slc2,
|
||||
&nasal_vm::opr_mcallg,
|
||||
&nasal_vm::opr_mcalll,
|
||||
&nasal_vm::opr_mcallv,
|
||||
&nasal_vm::opr_mcallh,
|
||||
&nasal_vm::opr_ret
|
||||
};
|
||||
for(auto& i:exec)
|
||||
{
|
||||
exec_code.push_back(opr_table[i.op]);
|
||||
imm.push_back(i.num);
|
||||
}
|
||||
loop_mark=true; // set loop mark to true
|
||||
return;
|
||||
}
|
||||
void nasal_vm::clear()
|
||||
{
|
||||
gc.gc_clear();
|
||||
while(!ret.empty())
|
||||
ret.pop();
|
||||
while(!counter.empty())
|
||||
counter.pop();
|
||||
str_table.clear();
|
||||
exec_code.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_vm::die(std::string str)
|
||||
{
|
||||
printf(">> [vm] 0x%.8x: %s\n",pc,str.c_str());
|
||||
loop_mark=false;
|
||||
return;
|
||||
}
|
||||
bool nasal_vm::condition(nasal_val* val_addr)
|
||||
{
|
||||
if(val_addr->type==vm_num)
|
||||
return val_addr->ptr.num;
|
||||
else if(val_addr->type==vm_str)
|
||||
{
|
||||
std::string& str=*val_addr->ptr.str;
|
||||
double num=str2num(str.c_str());
|
||||
if(std::isnan(num))
|
||||
return str.empty();
|
||||
return num;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void nasal_vm::opr_nop()
|
||||
{
|
||||
loop_mark=false;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_intg()
|
||||
{
|
||||
gc.global.resize(imm[pc],gc.nil_addr);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_intl()
|
||||
{
|
||||
stack_top[0]->ptr.func->closure.resize(imm[pc],gc.nil_addr);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_offset()
|
||||
{
|
||||
stack_top[0]->ptr.func->offset=imm[pc];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_loadg()
|
||||
{
|
||||
gc.global[imm[pc]]=(stack_top--)[0];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_loadl()
|
||||
{
|
||||
gc.local.back()[imm[pc]]=(stack_top--)[0];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_pnum()
|
||||
{
|
||||
(++stack_top)[0]=gc.num_addrs[imm[pc]];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_pone()
|
||||
{
|
||||
(++stack_top)[0]=gc.one_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_pzero()
|
||||
{
|
||||
(++stack_top)[0]=gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_pnil()
|
||||
{
|
||||
(++stack_top)[0]=gc.nil_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_pstr()
|
||||
{
|
||||
(++stack_top)[0]=gc.str_addrs[imm[pc]];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_newv()
|
||||
{
|
||||
nasal_val* vec_addr=gc.gc_alloc(vm_vec);
|
||||
nasal_val** begin=stack_top-imm[pc]+1;
|
||||
auto& vec=vec_addr->ptr.vec->elems;// stack_top-imm[pc] stores the vector
|
||||
vec.resize(imm[pc]);
|
||||
for(int i=0;i<imm[pc];++i)
|
||||
vec[i]=begin[i];
|
||||
begin[0]=vec_addr;
|
||||
stack_top=begin;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_newh()
|
||||
{
|
||||
(++stack_top)[0]=gc.gc_alloc(vm_hash);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_newf()
|
||||
{
|
||||
nasal_val* val=gc.gc_alloc(vm_func);
|
||||
val->ptr.func->entry=imm[pc];
|
||||
if(gc.local.empty())
|
||||
val->ptr.func->closure.push_back(gc.nil_addr);// me
|
||||
else
|
||||
val->ptr.func->closure=gc.local.back();// local contains 'me'
|
||||
(++stack_top)[0]=val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_happ()
|
||||
{
|
||||
nasal_val* val=stack_top[0];
|
||||
(--stack_top)[0]->ptr.hash->elems[str_table[imm[pc]]]=val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_para()
|
||||
{
|
||||
nasal_func* func=stack_top[0]->ptr.func;
|
||||
int size=func->key_table.size();
|
||||
func->key_table[str_table[imm[pc]]]=size;
|
||||
func->default_para.push_back(nullptr);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_defpara()
|
||||
{
|
||||
nasal_val* def_val=stack_top[0];
|
||||
nasal_func* func=(--stack_top)[0]->ptr.func;
|
||||
int size=func->key_table.size();
|
||||
func->key_table[str_table[imm[pc]]]=size;
|
||||
func->default_para.push_back(def_val);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_dynpara()
|
||||
{
|
||||
stack_top[0]->ptr.func->dynpara=imm[pc];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_unot()
|
||||
{
|
||||
nasal_val* val=stack_top[0];
|
||||
int type=val->type;
|
||||
if(type==vm_nil)
|
||||
stack_top[0]=gc.one_addr;
|
||||
else if(type==vm_num)
|
||||
stack_top[0]=val->ptr.num?gc.zero_addr:gc.one_addr;
|
||||
else if(type==vm_str)
|
||||
{
|
||||
double num=str2num(val->ptr.str->c_str());
|
||||
if(std::isnan(num))
|
||||
stack_top[0]=val->ptr.str->empty()?gc.one_addr:gc.zero_addr;
|
||||
else
|
||||
stack_top[0]=num?gc.zero_addr:gc.one_addr;
|
||||
}
|
||||
else
|
||||
die("unot: incorrect value type");
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_usub()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=-stack_top[0]->to_number();
|
||||
stack_top[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_add()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=stack_top[-1]->to_number()+stack_top[0]->to_number();
|
||||
(--stack_top)[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_sub()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=stack_top[-1]->to_number()-stack_top[0]->to_number();
|
||||
(--stack_top)[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_mul()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=stack_top[-1]->to_number()*stack_top[0]->to_number();
|
||||
(--stack_top)[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_div()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=stack_top[-1]->to_number()/stack_top[0]->to_number();
|
||||
(--stack_top)[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_lnk()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_str);
|
||||
*new_val->ptr.str=stack_top[-1]->to_string()+stack_top[0]->to_string();
|
||||
(--stack_top)[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_addeq()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=mem_addr[0]->to_number()+stack_top[-1]->to_number();
|
||||
(--stack_top)[0]=mem_addr[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_subeq()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=mem_addr[0]->to_number()-stack_top[-1]->to_number();
|
||||
(--stack_top)[0]=mem_addr[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_muleq()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=mem_addr[0]->to_number()*stack_top[-1]->to_number();
|
||||
(--stack_top)[0]=mem_addr[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_diveq()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_num);
|
||||
new_val->ptr.num=mem_addr[0]->to_number()/stack_top[-1]->to_number();
|
||||
(--stack_top)[0]=mem_addr[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_lnkeq()
|
||||
{
|
||||
nasal_val* new_val=gc.gc_alloc(vm_str);
|
||||
*new_val->ptr.str=mem_addr[0]->to_string()+stack_top[-1]->to_string();
|
||||
(--stack_top)[0]=mem_addr[0]=new_val;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_meq()
|
||||
{
|
||||
mem_addr[0]=(--stack_top)[0];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_eq()
|
||||
{
|
||||
nasal_val* val2=stack_top[0];
|
||||
nasal_val* val1=(--stack_top)[0];
|
||||
int a_type=val1->type;
|
||||
int b_type=val2->type;
|
||||
if(a_type==vm_nil && b_type==vm_nil)
|
||||
stack_top[0]=gc.one_addr;
|
||||
else if(a_type==vm_str && b_type==vm_str)
|
||||
stack_top[0]=(*val1->ptr.str==*val2->ptr.str)?gc.one_addr:gc.zero_addr;
|
||||
else if(a_type==vm_num || b_type==vm_num)
|
||||
stack_top[0]=(val1->to_number()==val2->to_number())?gc.one_addr:gc.zero_addr;
|
||||
else
|
||||
stack_top[0]=(val1==val2)?gc.one_addr:gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_neq()
|
||||
{
|
||||
nasal_val* val2=stack_top[0];
|
||||
nasal_val* val1=(--stack_top)[0];
|
||||
int a_type=val1->type;
|
||||
int b_type=val2->type;
|
||||
if(a_type==vm_nil && b_type==vm_nil)
|
||||
stack_top[0]=gc.zero_addr;
|
||||
else if(a_type==vm_str && b_type==vm_str)
|
||||
stack_top[0]=(*val1->ptr.str!=*val2->ptr.str)?gc.one_addr:gc.zero_addr;
|
||||
else if(a_type==vm_num || b_type==vm_num)
|
||||
stack_top[0]=(val1->to_number()!=val2->to_number())?gc.one_addr:gc.zero_addr;
|
||||
else
|
||||
stack_top[0]=(val1!=val2)?gc.one_addr:gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_less()
|
||||
{
|
||||
--stack_top;
|
||||
stack_top[0]=(stack_top[0]->to_number()<stack_top[1]->to_number())?gc.one_addr:gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_leq()
|
||||
{
|
||||
--stack_top;
|
||||
stack_top[0]=(stack_top[0]->to_number()<=stack_top[1]->to_number())?gc.one_addr:gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_grt()
|
||||
{
|
||||
--stack_top;
|
||||
stack_top[0]=(stack_top[0]->to_number()>stack_top[1]->to_number())?gc.one_addr:gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_geq()
|
||||
{
|
||||
--stack_top;
|
||||
stack_top[0]=(stack_top[0]->to_number()>=stack_top[1]->to_number())?gc.one_addr:gc.zero_addr;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_pop()
|
||||
{
|
||||
--stack_top;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_jmp()
|
||||
{
|
||||
pc=imm[pc]-1;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_jt()
|
||||
{
|
||||
if(condition(stack_top[0]))
|
||||
pc=imm[pc]-1;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_jf()
|
||||
{
|
||||
if(!condition(stack_top[0]))
|
||||
pc=imm[pc]-1;
|
||||
--stack_top;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_counter()
|
||||
{
|
||||
counter.push(-1);
|
||||
if(stack_top[0]->type!=vm_vec)
|
||||
die("cnt: must use vector in forindex/foreach");
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_cntpop()
|
||||
{
|
||||
counter.pop();
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_findex()
|
||||
{
|
||||
if(++counter.top()>=stack_top[0]->ptr.vec->elems.size())
|
||||
{
|
||||
pc=imm[pc]-1;
|
||||
return;
|
||||
}
|
||||
(++stack_top)[0]=gc.gc_alloc(vm_num);
|
||||
stack_top[0]->ptr.num=counter.top();
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_feach()
|
||||
{
|
||||
std::vector<nasal_val*>& ref=stack_top[0]->ptr.vec->elems;
|
||||
if(++counter.top()>=ref.size())
|
||||
{
|
||||
pc=imm[pc]-1;
|
||||
return;
|
||||
}
|
||||
(++stack_top)[0]=ref[counter.top()];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callg()
|
||||
{
|
||||
(++stack_top)[0]=gc.global[imm[pc]];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_calll()
|
||||
{
|
||||
(++stack_top)[0]=gc.local.back()[imm[pc]];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callv()
|
||||
{
|
||||
nasal_val* val=stack_top[0];
|
||||
nasal_val* vec_addr=(--stack_top)[0];
|
||||
int type=vec_addr->type;
|
||||
if(type==vm_vec)
|
||||
{
|
||||
stack_top[0]=vec_addr->ptr.vec->get_val(val->to_number());
|
||||
if(!stack_top[0])
|
||||
die("callv: index out of range:"+num2str(val->to_number()));
|
||||
}
|
||||
else if(type==vm_hash)
|
||||
{
|
||||
if(val->type!=vm_str)
|
||||
{
|
||||
die("callv: must use string as the key");
|
||||
return;
|
||||
}
|
||||
stack_top[0]=vec_addr->ptr.hash->get_val(*val->ptr.str);
|
||||
if(!stack_top[0])
|
||||
{
|
||||
die("callv: cannot find member \""+*val->ptr.str+"\" of this hash");
|
||||
return;
|
||||
}
|
||||
if(stack_top[0]->type==vm_func)
|
||||
stack_top[0]->ptr.func->closure[0]=val;// me
|
||||
}
|
||||
else if(type==vm_str)
|
||||
{
|
||||
std::string& str=*vec_addr->ptr.str;
|
||||
int num=val->to_number();
|
||||
int str_size=str.length();
|
||||
if(num<-str_size || num>=str_size)
|
||||
{
|
||||
die("callv: index out of range:"+num2str(val->to_number()));
|
||||
return;
|
||||
}
|
||||
stack_top[0]=gc.gc_alloc(vm_num);
|
||||
stack_top[0]->ptr.num=(str[num>=0? num:num+str_size]);
|
||||
}
|
||||
else
|
||||
die("callv: must call a vector/hash/string");
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callvi()
|
||||
{
|
||||
nasal_val* val=stack_top[0];
|
||||
if(val->type!=vm_vec)
|
||||
{
|
||||
die("callvi: multi-definition/multi-assignment must use a vector");
|
||||
return;
|
||||
}
|
||||
// cannot use operator[],because this may cause overflow
|
||||
(++stack_top)[0]=val->ptr.vec->get_val(imm[pc]);
|
||||
if(!stack_top[0])
|
||||
die("callvi: index out of range:"+num2str(imm[pc]));
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callh()
|
||||
{
|
||||
nasal_val* val=stack_top[0];
|
||||
if(val->type!=vm_hash)
|
||||
{
|
||||
die("callh: must call a hash");
|
||||
return;
|
||||
}
|
||||
stack_top[0]=val->ptr.hash->get_val(str_table[imm[pc]]);
|
||||
if(!stack_top[0])
|
||||
{
|
||||
die("callh: member \""+str_table[imm[pc]]+"\" does not exist");
|
||||
return;
|
||||
}
|
||||
if(stack_top[0]->type==vm_func)
|
||||
stack_top[0]->ptr.func->closure[0]=val;// me
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callfv()
|
||||
{
|
||||
// get parameter list and function value
|
||||
int args_size=imm[pc];
|
||||
nasal_val** vec=stack_top-args_size+1;
|
||||
nasal_val* func_addr=vec[-1];
|
||||
if(func_addr->type!=vm_func)
|
||||
{
|
||||
die("callfv: must call a function");
|
||||
return;
|
||||
}
|
||||
// push new local scope
|
||||
auto& ref_func=*func_addr->ptr.func;
|
||||
gc.local.push_back(ref_func.closure);
|
||||
// load parameters
|
||||
auto& ref_default=ref_func.default_para;
|
||||
auto& ref_closure=gc.local.back();
|
||||
|
||||
int offset=ref_func.offset;
|
||||
int para_size=ref_func.key_table.size();
|
||||
// load arguments
|
||||
if(args_size<para_size && !ref_default[args_size])
|
||||
{
|
||||
// if the first default value is not nullptr,then values after it are not nullptr
|
||||
die("callfv: lack argument(s)");
|
||||
return;
|
||||
}
|
||||
// if args_size>para_size,for 0 to args_size will cause corruption
|
||||
for(int i=0;i<para_size;++i)
|
||||
ref_closure[i+offset]=(i<args_size)?vec[i]:ref_default[i];
|
||||
// load dynamic argument if args_size>=para_size
|
||||
if(ref_func.dynpara>=0)
|
||||
{
|
||||
nasal_val* vec_addr=gc.gc_alloc(vm_vec);
|
||||
for(int i=para_size;i<args_size;++i)
|
||||
vec_addr->ptr.vec->elems.push_back(vec[i]);
|
||||
ref_closure[para_size+offset]=vec_addr;
|
||||
}
|
||||
|
||||
stack_top-=args_size;// pop arguments
|
||||
ret.push(pc);
|
||||
pc=ref_func.entry-1;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callfh()
|
||||
{
|
||||
// get parameter list and function value
|
||||
auto& ref_hash=stack_top[0]->ptr.hash->elems;
|
||||
nasal_val* func_addr=stack_top[-1];
|
||||
if(func_addr->type!=vm_func)
|
||||
{
|
||||
die("callfh: must call a function");
|
||||
return;
|
||||
}
|
||||
// push new local scope
|
||||
auto& ref_func=*func_addr->ptr.func;
|
||||
gc.local.push_back(ref_func.closure);
|
||||
// load parameters
|
||||
auto& ref_default=ref_func.default_para;
|
||||
auto& ref_closure=gc.local.back();
|
||||
|
||||
if(ref_func.dynpara>=0)
|
||||
{
|
||||
die("callfh: special call cannot use dynamic argument");
|
||||
return;
|
||||
}
|
||||
int offset=ref_func.offset;
|
||||
for(auto& i:ref_func.key_table)
|
||||
{
|
||||
if(ref_hash.count(i.first))
|
||||
ref_closure[i.second+offset]=ref_hash[i.first];
|
||||
else if(ref_default[i.second])
|
||||
ref_closure[i.second+offset]=ref_default[i.second];
|
||||
else
|
||||
{
|
||||
die("callfh: lack argument(s): \""+i.first+"\"");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
--stack_top;// pop hash
|
||||
ret.push(pc);
|
||||
pc=ref_func.entry-1;
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_callb()
|
||||
{
|
||||
loop_mark=(++stack_top)[0]=(*builtin_func[imm[pc]].func)(gc.local.back(),gc);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_slcbegin()
|
||||
{
|
||||
gc.slice_stack.push_back(gc.gc_alloc(vm_vec));
|
||||
if(stack_top[0]->type!=vm_vec)
|
||||
die("slcbegin: must slice a vector");
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_slcend()
|
||||
{
|
||||
stack_top[0]=gc.slice_stack.back();
|
||||
gc.slice_stack.pop_back();
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_slc()
|
||||
{
|
||||
nasal_val* val=(stack_top--)[0];
|
||||
nasal_val* res=stack_top[0]->ptr.vec->get_val(val->to_number());
|
||||
if(!res)
|
||||
die("slc: index out of range:"+num2str(val->to_number()));
|
||||
gc.slice_stack.back()->ptr.vec->elems.push_back(res);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_slc2()
|
||||
{
|
||||
nasal_val* val2=(stack_top--)[0];
|
||||
nasal_val* val1=(stack_top--)[0];
|
||||
std::vector<nasal_val*>& ref=stack_top[0]->ptr.vec->elems;
|
||||
std::vector<nasal_val*>& aim=gc.slice_stack.back()->ptr.vec->elems;
|
||||
|
||||
int type1=val1->type,type2=val2->type;
|
||||
int num1=val1->to_number();
|
||||
int num2=val2->to_number();
|
||||
int ref_size=ref.size();
|
||||
if(type1==vm_nil && type2==vm_nil)
|
||||
{
|
||||
num1=0;
|
||||
num2=ref_size-1;
|
||||
}
|
||||
else if(type1==vm_nil && type2!=vm_nil)
|
||||
num1=num2<0? -ref_size:0;
|
||||
else if(type1!=vm_nil && type2==vm_nil)
|
||||
num2=num1<0? -1:ref_size-1;
|
||||
|
||||
if(num1>=num2)
|
||||
{
|
||||
die("slc2: begin index must be less than end index");
|
||||
return;
|
||||
}
|
||||
else if(num1<-ref_size || num1>=ref_size)
|
||||
{
|
||||
die("slc2: begin index out of range: "+num2str(num1));
|
||||
return;
|
||||
}
|
||||
else if(num2<-ref_size || num2>=ref_size)
|
||||
{
|
||||
die("slc2: end index out of range: "+num2str(num2));
|
||||
return;
|
||||
}
|
||||
for(int i=num1;i<=num2;++i)
|
||||
aim.push_back(i>=0?ref[i]:ref[i+ref_size]);
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_mcallg()
|
||||
{
|
||||
mem_addr=&gc.global[imm[pc]];
|
||||
(++stack_top)[0]=mem_addr[0];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_mcalll()
|
||||
{
|
||||
mem_addr=&gc.local.back()[imm[pc]];
|
||||
(++stack_top)[0]=mem_addr[0];
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_mcallv()
|
||||
{
|
||||
nasal_val* val=stack_top[0];
|
||||
nasal_val* vec_addr=(--stack_top)[0];
|
||||
int type=vec_addr->type;
|
||||
if(type==vm_vec)
|
||||
{
|
||||
mem_addr=vec_addr->ptr.vec->get_mem(val->to_number());
|
||||
if(!mem_addr)
|
||||
die("mcallv: index out of range:"+num2str(val->to_number()));
|
||||
}
|
||||
else if(type==vm_hash)
|
||||
{
|
||||
if(val->type!=vm_str)
|
||||
{
|
||||
die("mcallv: must use string as the key");
|
||||
return;
|
||||
}
|
||||
nasal_hash& ref=*vec_addr->ptr.hash;
|
||||
std::string& str=*val->ptr.str;
|
||||
mem_addr=ref.get_mem(str);
|
||||
if(!mem_addr)
|
||||
{
|
||||
ref.elems[str]=gc.nil_addr;
|
||||
mem_addr=ref.get_mem(str);
|
||||
}
|
||||
}
|
||||
else
|
||||
die("mcallv: cannot get memory space in other types");
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_mcallh()
|
||||
{
|
||||
nasal_val* hash_addr=stack_top[0];
|
||||
if(hash_addr->type!=vm_hash)
|
||||
{
|
||||
die("mcallh: must call a hash");
|
||||
return;
|
||||
}
|
||||
nasal_hash& ref=*hash_addr->ptr.hash;
|
||||
std::string& str=str_table[imm[pc]];
|
||||
mem_addr=ref.get_mem(str);
|
||||
if(!mem_addr) // create a new key
|
||||
{
|
||||
ref.elems[str]=gc.nil_addr;
|
||||
mem_addr=ref.get_mem(str);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void nasal_vm::opr_ret()
|
||||
{
|
||||
gc.local.pop_back();// delete local scope
|
||||
pc=ret.top();ret.pop();// fetch pc
|
||||
(--stack_top)[0]->ptr.func->closure[0]=gc.nil_addr;// set 'me' to nil
|
||||
stack_top[0]=stack_top[1];// rewrite nasal_func with returned value
|
||||
return;
|
||||
}
|
||||
void nasal_vm::run()
|
||||
{
|
||||
clock_t begin_time=clock();
|
||||
for(pc=0;loop_mark&&!gc.val_stack[STACK_MAX_DEPTH-1];++pc)
|
||||
(this->*exec_code[pc])();
|
||||
if(gc.val_stack[STACK_MAX_DEPTH-1])
|
||||
die("stack overflow");
|
||||
std::cout<<">> [vm] process exited after "<<((double)(clock()-begin_time))/CLOCKS_PER_SEC<<"s.\n";
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 62 KiB |
152
props.nas
Normal file
152
props.nas
Normal file
@@ -0,0 +1,152 @@
|
||||
import("lib.nas");
|
||||
|
||||
var props=
|
||||
{
|
||||
globals:nil,
|
||||
Node:nil,
|
||||
getNode:func(path,index)
|
||||
{
|
||||
path=split('/',path);
|
||||
var tmp=me.globals;
|
||||
var path_size=size(path);
|
||||
for(var i=0;i<path_size-1;i+=1)
|
||||
tmp=tmp.val[path[i]];
|
||||
if(path_size>0)
|
||||
{
|
||||
if(contains(tmp.val,path[i]~'['~index~']'))
|
||||
return tmp.val[path[i]~'['~index~']'];
|
||||
else
|
||||
return tmp.val[path[i]];
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
};
|
||||
|
||||
props.Node=
|
||||
{
|
||||
new:func(values=nil)
|
||||
{
|
||||
var result={
|
||||
parents:[props.Node],
|
||||
val:{},
|
||||
type:'GHOST',
|
||||
parent:nil
|
||||
};
|
||||
if(typeof(values)=="hash")
|
||||
result.val=values;
|
||||
return result;
|
||||
},
|
||||
addChild:func(name)
|
||||
{
|
||||
if(!contains(me.val,name))
|
||||
{
|
||||
me.val[name]=props.Node.new();
|
||||
me.val[name].parent=me;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
addChildren:func(name,cnt=0)
|
||||
{
|
||||
for(var i=0;i<cnt;i+=1)
|
||||
{
|
||||
var label=name~'['~i~']';
|
||||
me.val[label]=props.Node.new();
|
||||
me.val[label].parent=me;
|
||||
}
|
||||
return;
|
||||
},
|
||||
setValue:func(path,val)
|
||||
{
|
||||
path=split('/',path);
|
||||
var tmp=me;
|
||||
foreach(var label;path)
|
||||
tmp=tmp.val[label];
|
||||
tmp.val=val;
|
||||
if(typeof(val)=='string')
|
||||
{
|
||||
if(val=='true' or val=='false')
|
||||
tmp.type='BOOL';
|
||||
else
|
||||
tmp.type='STRING';
|
||||
}
|
||||
elsif(typeof(val)=='number')
|
||||
tmp.type='DOUBLE';
|
||||
return;
|
||||
},
|
||||
setIntValue:func(num)
|
||||
{
|
||||
me.val=num;
|
||||
me.type='INT';
|
||||
return;
|
||||
},
|
||||
setBoolValue:func(state)
|
||||
{
|
||||
me.val=state;
|
||||
me.type='BOOL';
|
||||
return;
|
||||
},
|
||||
setDoubleValue:func(num)
|
||||
{
|
||||
me.val=num;
|
||||
me.type='DOUBLE';
|
||||
return;
|
||||
},
|
||||
getValue:func(){return me.val;},
|
||||
getName:func()
|
||||
{
|
||||
var val=me.parent.val;
|
||||
var key=keys(val);
|
||||
foreach(var k;key)
|
||||
if(val[k]==me)
|
||||
return k;
|
||||
return '';
|
||||
},
|
||||
getParent:func()
|
||||
{
|
||||
return me.parent;
|
||||
},
|
||||
getPath:func()
|
||||
{
|
||||
if(me.parent==nil) return '';
|
||||
return me.parent.getPath()~'/'~me.getName();
|
||||
},
|
||||
equals:func(node){return me==node;},
|
||||
debug:func(s='')
|
||||
{
|
||||
if(typeof(me.val)=='hash')
|
||||
{
|
||||
var key=keys(me.val);
|
||||
println('{');
|
||||
foreach(var k;key)
|
||||
{
|
||||
print(s~' ',k,':');
|
||||
me.val[k].debug(s~' ');
|
||||
}
|
||||
println(s,'}');
|
||||
}
|
||||
else
|
||||
println(me.val,' (',me.type,')');
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
props.globals=props.Node.new();
|
||||
var c=['aircraft','ai','models','position','orientation','controls','sim'];
|
||||
foreach(var i;c)
|
||||
props.getNode('/',1).addChild(i);
|
||||
props.getNode('/ai',1).addChildren('ai',4);
|
||||
props.getNode('/aircraft',1).setValue('/','IDG MD-11');
|
||||
for(var i=0;i<4;i+=1)
|
||||
props.getNode('/ai/ai['~i~']',1).setBoolValue('true');
|
||||
props.getNode('/models',1).addChildren('building',4);
|
||||
for(var i=0;i<4;i+=1)
|
||||
props.getNode('/models/building['~i~']',1).setIntValue(i);
|
||||
props.getNode('/',1).addChild('test');
|
||||
props.getNode('/test',1).addChildren('in',4);
|
||||
props.getNode('/test/in',0).setValue('/','true');
|
||||
props.getNode('/test/in',1).setValue('/','false');
|
||||
props.getNode('/test/in',2).setValue('/','welcome aboard,need help? use help->tutorial');
|
||||
props.getNode('/test/in',3).setValue('/',2147483648);
|
||||
props.globals.debug();
|
||||
println(props.getNode('/test/in',3).getPath());
|
||||
144
stl/lib.nas
Normal file
144
stl/lib.nas
Normal file
@@ -0,0 +1,144 @@
|
||||
var import=func(filename)
|
||||
{
|
||||
return __builtin_import(filename);
|
||||
}
|
||||
var print=func(elems...)
|
||||
{
|
||||
return __builtin_print(elems);
|
||||
};
|
||||
var println=func(elems...)
|
||||
{
|
||||
__builtin_print(elems);
|
||||
elems=['\n'];
|
||||
return __builtin_print(elems);
|
||||
}
|
||||
var append=func(vec,elems...)
|
||||
{
|
||||
return __builtin_append(vec,elems);
|
||||
}
|
||||
var setsize=func(vec,size)
|
||||
{
|
||||
return __builtin_setsize(vec,size);
|
||||
}
|
||||
var system=func(str)
|
||||
{
|
||||
return __builtin_system(str);
|
||||
}
|
||||
var input=func()
|
||||
{
|
||||
return __builtin_input();
|
||||
}
|
||||
var sleep=func(duration)
|
||||
{
|
||||
return __builtin_sleep(duration);
|
||||
}
|
||||
var split=func(deli,str)
|
||||
{
|
||||
return __builtin_split(deli,str);
|
||||
}
|
||||
var rand=func(seed=nil)
|
||||
{
|
||||
return __builtin_rand(seed);
|
||||
}
|
||||
var id=func(object)
|
||||
{
|
||||
return __builtin_id(object);
|
||||
}
|
||||
var int=func(val)
|
||||
{
|
||||
return __builtin_int(val);
|
||||
}
|
||||
var num=func(val)
|
||||
{
|
||||
return __builtin_num(val);
|
||||
}
|
||||
var pop=func(vec)
|
||||
{
|
||||
return __builtin_pop(vec);
|
||||
}
|
||||
var str=func(num)
|
||||
{
|
||||
return __builtin_str(num);
|
||||
}
|
||||
var size=func(object)
|
||||
{
|
||||
return __builtin_size(object);
|
||||
}
|
||||
var contains=func(hash,key)
|
||||
{
|
||||
return __builtin_contains(hash,key);
|
||||
}
|
||||
var delete=func(hash,key)
|
||||
{
|
||||
return __builtin_delete(hash,key);
|
||||
}
|
||||
var keys=func(hash)
|
||||
{
|
||||
return __builtin_keys(hash);
|
||||
}
|
||||
var time=func(begin_time)
|
||||
{
|
||||
return __builtin_time(begin_time);
|
||||
}
|
||||
var die=func(str)
|
||||
{
|
||||
return __builtin_die(str);
|
||||
}
|
||||
var typeof=func(object)
|
||||
{
|
||||
return __builtin_type(object);
|
||||
}
|
||||
var substr=func(str,begin,len)
|
||||
{
|
||||
return __builtin_substr(str,begin,len);
|
||||
}
|
||||
var streq=func(a,b)
|
||||
{
|
||||
return __builtin_streq(a,b);
|
||||
}
|
||||
var left=func(str,len)
|
||||
{
|
||||
return __builtin_left(str,len);
|
||||
}
|
||||
var right=func(str,len)
|
||||
{
|
||||
return __builtin_right(str,len);
|
||||
}
|
||||
var cmp=func(a,b)
|
||||
{
|
||||
return __builtin_cmp(a,b);
|
||||
}
|
||||
var chr=func(code)
|
||||
{
|
||||
return __builtin_chr(code);
|
||||
}
|
||||
|
||||
var io=
|
||||
{
|
||||
fin: func(filename){return __builtin_fin(filename);},
|
||||
fout:func(filename,str){return __builtin_fout(filename,str);}
|
||||
};
|
||||
|
||||
var bits=
|
||||
{
|
||||
bitxor: func(a,b){return __builtin_xor(a,b); },
|
||||
bitand: func(a,b){return __builtin_and(a,b); },
|
||||
bitor: func(a,b){return __builtin_or(a,b); },
|
||||
bitnand: func(a,b){return __builtin_nand(a,b);},
|
||||
bitnot: func(a) {return __builtin_not(a); }
|
||||
};
|
||||
|
||||
var math=
|
||||
{
|
||||
e: 2.7182818284590452354,
|
||||
pi: 3.14159265358979323846264338327950288,
|
||||
sin: func(x) {return __builtin_sin(x); },
|
||||
cos: func(x) {return __builtin_cos(x); },
|
||||
tan: func(x) {return __builtin_tan(x); },
|
||||
exp: func(x) {return __builtin_exp(x); },
|
||||
ln: func(x) {return __builtin_ln(x); },
|
||||
sqrt: func(x) {return __builtin_sqrt(x); },
|
||||
atan2: func(x,y){return __builtin_atan2(x,y);}
|
||||
};
|
||||
|
||||
var D2R=math.pi/180;
|
||||
73
stl/list.nas
Normal file
73
stl/list.nas
Normal file
@@ -0,0 +1,73 @@
|
||||
# lib list.nas
|
||||
# valkmjolnir 2021/3/31
|
||||
var list=func()
|
||||
{
|
||||
var _={begin:nil,end:nil};
|
||||
return
|
||||
{
|
||||
push_back:func(elem)
|
||||
{
|
||||
var tmp={elem:elem,prev:nil,next:nil};
|
||||
if(_.end!=nil)
|
||||
{
|
||||
_.end.next=tmp;
|
||||
tmp.prev=_.end;
|
||||
_.end=tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
_.begin=tmp;
|
||||
_.end=tmp;
|
||||
}
|
||||
return;
|
||||
},
|
||||
push_front:func(elem)
|
||||
{
|
||||
var tmp={elem:elem,prev:nil,next:nil};
|
||||
if(_.begin!=nil)
|
||||
{
|
||||
_.begin.prev=tmp;
|
||||
tmp.next=_.begin;
|
||||
_.begin=tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
_.begin=tmp;
|
||||
_.end=tmp;
|
||||
}
|
||||
return;
|
||||
},
|
||||
pop_back:func()
|
||||
{
|
||||
if(_.end!=nil)
|
||||
_.end=_.end.prev;
|
||||
if(_.end==nil)
|
||||
_.begin=nil;
|
||||
else
|
||||
_.end.next=nil;
|
||||
return;
|
||||
},
|
||||
pop_front:func()
|
||||
{
|
||||
if(_.begin!=nil)
|
||||
_.begin=_.begin.next;
|
||||
if(_.begin==nil)
|
||||
_.end=nil;
|
||||
else
|
||||
_.begin.prev=nil;
|
||||
return;
|
||||
},
|
||||
front:func()
|
||||
{
|
||||
if(_.begin!=nil)
|
||||
return _.begin.elem;
|
||||
return nil;
|
||||
},
|
||||
back:func()
|
||||
{
|
||||
if(_.end!=nil)
|
||||
return _.end.elem;
|
||||
return nil;
|
||||
},
|
||||
};
|
||||
}
|
||||
46
stl/queue.nas
Normal file
46
stl/queue.nas
Normal file
@@ -0,0 +1,46 @@
|
||||
# lib queue.nas
|
||||
# valkmjolnir 2021/3/31
|
||||
var queue=func()
|
||||
{
|
||||
var _={begin:nil,end:nil};
|
||||
return
|
||||
{
|
||||
push:func(elem)
|
||||
{
|
||||
var new_node=
|
||||
{
|
||||
elem:elem,
|
||||
next:nil
|
||||
};
|
||||
if(_.begin==nil)
|
||||
_.begin=_.end=new_node;
|
||||
else
|
||||
{
|
||||
_.end.next=new_node;
|
||||
_.end=new_node;
|
||||
}
|
||||
return;
|
||||
},
|
||||
pop:func()
|
||||
{
|
||||
if(_.begin!=nil)
|
||||
_.begin=_.begin.next;
|
||||
if(_.begin==nil)
|
||||
_.end=nil;
|
||||
},
|
||||
front:func()
|
||||
{
|
||||
if(_.begin!=nil)
|
||||
return _.begin.elem;
|
||||
return nil;
|
||||
},
|
||||
clear:func()
|
||||
{
|
||||
_.begin=_.end=nil;
|
||||
},
|
||||
empty:func()
|
||||
{
|
||||
return _.begin==nil;
|
||||
}
|
||||
};
|
||||
}
|
||||
27
stl/sort.nas
Normal file
27
stl/sort.nas
Normal file
@@ -0,0 +1,27 @@
|
||||
# lib sort.nas
|
||||
# valkmjolnir 2021/4/2
|
||||
var sort=func(vec,left,right,cmp=func(a,b){return a<=b;})
|
||||
{
|
||||
if(left>=right) return nil;
|
||||
var L=left;
|
||||
var R=right;
|
||||
var tmp=vec[L];
|
||||
while(left<right)
|
||||
{
|
||||
while(left<right and cmp(tmp,vec[right]))
|
||||
right-=1;
|
||||
while(left<right and cmp(vec[left],tmp))
|
||||
left+=1;
|
||||
if(left!=right)
|
||||
{
|
||||
var t=vec[left];
|
||||
vec[left]=vec[right];
|
||||
vec[right]=t;
|
||||
}
|
||||
}
|
||||
vec[L]=vec[left];
|
||||
vec[left]=tmp;
|
||||
sort(vec,L,left-1,cmp);
|
||||
sort(vec,left+1,R,cmp);
|
||||
return nil;
|
||||
}
|
||||
36
stl/stack.nas
Normal file
36
stl/stack.nas
Normal file
@@ -0,0 +1,36 @@
|
||||
# lib stack.nas
|
||||
# valkmjolnir 2021/3/31
|
||||
var stack=func()
|
||||
{
|
||||
var _={next:nil};
|
||||
return
|
||||
{
|
||||
push:func(elem)
|
||||
{
|
||||
_.next={elem:elem,next:_.next};
|
||||
return;
|
||||
},
|
||||
pop:func()
|
||||
{
|
||||
var tmp=_.next;
|
||||
if(tmp!=nil)
|
||||
_.next=tmp.next;
|
||||
return;
|
||||
},
|
||||
top:func()
|
||||
{
|
||||
var tmp=_.next;
|
||||
if(tmp!=nil)
|
||||
return tmp.elem;
|
||||
return nil;
|
||||
},
|
||||
clear:func()
|
||||
{
|
||||
_.next=nil;
|
||||
},
|
||||
empty:func()
|
||||
{
|
||||
return _.next==nil;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
# NasNeuron lib written by ValKmjolnir
|
||||
|
||||
# Basic Class NasMatrix
|
||||
# NasMatGen : generate a new matrix
|
||||
# NasMatAdd : add two matrixes
|
||||
# NasMatSub : sub two matrixes
|
||||
# NasMatMul : multiply two matrix
|
||||
# NasMatTrans: transpose a matrix
|
||||
# NasMatPrt : print a matrix
|
||||
var NasMatrix=
|
||||
{
|
||||
NasMatGen:func(row,col)
|
||||
{
|
||||
var GenMat={Row:row,Col:col,Elem:[]};
|
||||
for(var i=0;i<row;i+=1)
|
||||
{
|
||||
var TmpVec=[];
|
||||
for(var j=0;j<col;j+=1)
|
||||
append(TmpVec,0);
|
||||
append(GenMat.Elem,TmpVec);
|
||||
}
|
||||
return GenMat;
|
||||
},
|
||||
NasMatAdd:func(mat1,mat2)
|
||||
{
|
||||
var ResultMat=nil;
|
||||
if(mat1.Row==mat2.Row and mat1.Col==mat2.Col)
|
||||
{
|
||||
ResultMat=me.NasMatGen(mat1.Row,mat1.Col);
|
||||
for(var i=0;i<ResultMat.Row;i+=1)
|
||||
for(var j=0;j<ResultMat.Col;j+=1)
|
||||
ResultMat.Elem[i][j]=mat1.Elem[i][j]+mat2.Elem[i][j];
|
||||
}
|
||||
else
|
||||
print("NasNeuron: Mat1 and Mat2 have different rows and cols.");
|
||||
return ResultMat;
|
||||
},
|
||||
NasMatSub:func(mat1,mat2)
|
||||
{
|
||||
var ResultMat=nil;
|
||||
if(mat1.Row==mat2.Row and mat1.Col==mat2.Col)
|
||||
{
|
||||
ResultMat=me.NasMatGen(mat1.Row,mat1.Col);
|
||||
for(var i=0;i<ResultMat.Row;i+=1)
|
||||
for(var j=0;j<ResultMat.Col;j+=1)
|
||||
ResultMat.Elem[i][j]=mat1.Elem[i][j]-mat2.Elem[i][j];
|
||||
}
|
||||
else
|
||||
print("NasNeuron: Mat1 and Mat2 have different rows and cols.");
|
||||
return ResultMat;
|
||||
},
|
||||
NasMatMul:func(mat1,mat2)
|
||||
{
|
||||
var ResultMat=nil;
|
||||
if(mat1.Col==mat2.Row)
|
||||
{
|
||||
ResultMat=me.NasMatGen(mat1.Row,mat2.Col);
|
||||
for(var i=0;i<ResultMat.Row;i+=1)
|
||||
for(var j=0;j<ResultMat.Col;j+=1)
|
||||
{
|
||||
var sum=0;
|
||||
for(var k=0;k<mat1.Col;k+=1)
|
||||
sum+=mat1.Elem[i][k]*mat2.Elem[k][j];
|
||||
ResultMat.Elem[i][j]=sum;
|
||||
}
|
||||
}
|
||||
else
|
||||
print("NasNeuron: Mat1's Col is different from Mat2's Row.");
|
||||
return ResultMat;
|
||||
},
|
||||
NasMatTrans:func(mat)
|
||||
{
|
||||
var ResultMat=nil;
|
||||
ResultMat=me.NasMatGen(mat.Col,mat.Row);
|
||||
for(var i=0;i<ResultMat.Row;i+=1)
|
||||
for(var j=0;j<ResultMat.Col;j+=1)
|
||||
ResultMat.Elem[i][j]=mat.Elem[j][i];
|
||||
return ResultMat;
|
||||
},
|
||||
NasMatPrt:func(mat)
|
||||
{
|
||||
for(var i=0;i<mat.Row;i+=1)
|
||||
{
|
||||
for(var j=0;j<mat.Col;j+=1)
|
||||
print(mat.Elem[i][j],' ');
|
||||
print('\n');
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
};
|
||||
|
||||
NasMatrix.NasMatPrt(
|
||||
NasMatrix.NasMatGen(
|
||||
10,100
|
||||
)
|
||||
);
|
||||
335
test/ai.nas
335
test/ai.nas
@@ -1,335 +0,0 @@
|
||||
import("lib.nas");
|
||||
var activate_function=
|
||||
{
|
||||
sigmoid_func:func(x)
|
||||
{
|
||||
return 1.0/(1.0+math.exp(-x));
|
||||
},
|
||||
diffsigmoid_func:func(x)
|
||||
{
|
||||
var t=func(x){return 1.0/(1.0+math.exp(-x));}(x);
|
||||
return t*(1-t);
|
||||
},
|
||||
tanh_func:func(x)
|
||||
{
|
||||
var t1=math.exp(x);
|
||||
var t2=math.exp(-x);
|
||||
return (t1-t2)/(t1+t2);
|
||||
},
|
||||
difftanh_func:func(x)
|
||||
{
|
||||
var t1=math.exp(x);
|
||||
var t2=math.exp(-x);
|
||||
var t=(t1-t2)/(t1+t2);
|
||||
return 1-t*t;
|
||||
},
|
||||
relu_func:func(x)
|
||||
{
|
||||
return x>0? x:0;
|
||||
},
|
||||
diffrelu_func:func(x)
|
||||
{
|
||||
return x>0;
|
||||
},
|
||||
leaky_relu_func:func(k,x)
|
||||
{
|
||||
return x>0? x:k*x;
|
||||
},
|
||||
diffleaky_relu_func:func(k,x)
|
||||
{
|
||||
return x>0? 1:k;
|
||||
}
|
||||
};
|
||||
|
||||
var matrix=
|
||||
{
|
||||
new:func(col,row)
|
||||
{
|
||||
var new_mat=
|
||||
{
|
||||
col:col,
|
||||
row:row,
|
||||
mat:[]
|
||||
};
|
||||
for(var i=0;i<row;i+=1)
|
||||
{
|
||||
append(new_mat.mat,[]);
|
||||
for(var j=0;j<col;j+=1)
|
||||
append(new_mat.mat[i],nil);
|
||||
}
|
||||
return new_mat;
|
||||
},
|
||||
srand:func(x)
|
||||
{
|
||||
rand(x);
|
||||
return nil;
|
||||
},
|
||||
rand_init:func(mat)
|
||||
{
|
||||
for(var i=0;i<mat.row;i+=1)
|
||||
for(var j=0;j<mat.col;j+=1)
|
||||
{
|
||||
if(rand()>0.5)
|
||||
mat.mat[i][j]=-rand();
|
||||
else
|
||||
mat.mat[i][j]=rand();
|
||||
}
|
||||
return;
|
||||
},
|
||||
prt_mat:func(mat)
|
||||
{
|
||||
var prt_s='[\n';
|
||||
foreach(var i;mat.mat)
|
||||
{
|
||||
var s='[';
|
||||
foreach(var j;i)
|
||||
s~=(j~',');
|
||||
s~='],\n';
|
||||
prt_s~=s;
|
||||
}
|
||||
prt_s~=']';
|
||||
print(prt_s);
|
||||
return nil;
|
||||
},
|
||||
mult_mat:func(mat1,mat2)
|
||||
{
|
||||
if(mat1.col!=mat2.row)
|
||||
{
|
||||
die("[error-mult] mat1\'s col does not match mat2\'s row.");
|
||||
return nil;
|
||||
}
|
||||
var new_mat=me.new(mat2.col,mat1.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
{
|
||||
var sum=0;
|
||||
for(var k=0;k<mat1.col;k+=1)
|
||||
sum+=mat1.mat[i][k]*mat2.mat[k][j];
|
||||
new_mat.mat[i][j]=sum;
|
||||
}
|
||||
return new_mat;
|
||||
},
|
||||
add_mat:func(mat1,mat2)
|
||||
{
|
||||
if(mat1.col!=mat2.col or mat1.row!=mat2.row)
|
||||
{
|
||||
die("[error-add] mat1\'s col or row does not match mat2\'s.");
|
||||
return nil;
|
||||
}
|
||||
var new_mat=me.new(mat1.col,mat1.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=mat1.mat[i][j]+mat2.mat[i][j];
|
||||
return new_mat;
|
||||
},
|
||||
sub_mat:func(mat1,mat2)
|
||||
{
|
||||
if(mat1.col!=mat2.col or mat1.row!=mat2.row)
|
||||
{
|
||||
die("[error-sub] mat1\'s col or row does not match mat2\'s.");
|
||||
return nil;
|
||||
}
|
||||
var new_mat=me.new(mat1.col,mat1.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=mat1.mat[i][j]-mat2.mat[i][j];
|
||||
return new_mat;
|
||||
},
|
||||
hadamard:func(mat1,mat2)
|
||||
{
|
||||
if(mat1.col!=mat2.col or mat1.row!=mat2.row)
|
||||
{
|
||||
die("[error-hadamard] mat1\'s col or row does not match mat2\'s.");
|
||||
return nil;
|
||||
}
|
||||
var new_mat=me.new(mat1.col,mat1.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=mat1.mat[i][j]*mat2.mat[i][j];
|
||||
return new_mat;
|
||||
},
|
||||
transpose:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.row,mat.col);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=mat.mat[j][i];
|
||||
return new_mat;
|
||||
},
|
||||
sigmoid:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.sigmoid_func(mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
diffsigmoid:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.diffsigmoid_func(mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
tanh:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.tanh_func(mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
difftanh:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.difftanh_func(mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
relu:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.relu_func(mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
diffrelu:func(mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.diffrelu_func(mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
leaky_relu:func(k,mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.leaky_relu_func(k,mat.mat[i][j]);
|
||||
return new_mat;
|
||||
},
|
||||
diffleaky_relu:func(k,mat)
|
||||
{
|
||||
var new_mat=me.new(mat.col,mat.row);
|
||||
for(var i=0;i<new_mat.row;i+=1)
|
||||
for(var j=0;j<new_mat.col;j+=1)
|
||||
new_mat.mat[i][j]=activate_function.diffleaky_relu_func(k,mat.mat[i][j]);
|
||||
return new_mat;
|
||||
}
|
||||
};
|
||||
|
||||
var bp=
|
||||
{
|
||||
inum:2,
|
||||
hnum:4,
|
||||
onum:1,
|
||||
learning_rate:nil,
|
||||
hidden_layer:nil,
|
||||
hidden_res:nil,
|
||||
output_layer:nil,
|
||||
output_res:nil,
|
||||
result:nil,
|
||||
training_set:[],
|
||||
expect_set:[],
|
||||
init:func()
|
||||
{
|
||||
matrix.srand(time(0));
|
||||
me.hidden_layer=matrix.new(me.hnum,me.inum);
|
||||
matrix.rand_init(me.hidden_layer);
|
||||
me.output_layer=matrix.new(me.onum,me.hnum);
|
||||
matrix.rand_init(me.output_layer);
|
||||
return;
|
||||
},
|
||||
set_learning_rate:func(lr)
|
||||
{
|
||||
me.learning_rate=matrix.new(me.onum,1);
|
||||
for(var i=0;i<me.onum;i+=1)
|
||||
me.learning_rate.mat[i][0]=lr;
|
||||
return;
|
||||
},
|
||||
set_training_set:func()
|
||||
{
|
||||
for(var i=0;i<4;i+=1)
|
||||
append(me.training_set,matrix.new(me.inum,1));
|
||||
me.training_set[0].mat[0][0]=0;
|
||||
me.training_set[0].mat[0][1]=0;
|
||||
me.training_set[1].mat[0][0]=0;
|
||||
me.training_set[1].mat[0][1]=1;
|
||||
me.training_set[2].mat[0][0]=1;
|
||||
me.training_set[2].mat[0][1]=0;
|
||||
me.training_set[3].mat[0][0]=1;
|
||||
me.training_set[3].mat[0][1]=1;
|
||||
return;
|
||||
},
|
||||
set_expect_set:func()
|
||||
{
|
||||
for(var i=0;i<4;i+=1)
|
||||
append(me.expect_set,matrix.new(me.onum,1))
|
||||
me.expect_set[0].mat[0][0]=0;
|
||||
me.expect_set[1].mat[0][0]=1;
|
||||
me.expect_set[2].mat[0][0]=1;
|
||||
me.expect_set[3].mat[0][0]=0;
|
||||
return;
|
||||
},
|
||||
forward:func(i)
|
||||
{
|
||||
var tmp=nil;
|
||||
me.hidden_res=matrix.mult_mat(me.training_set[i],me.hidden_layer);
|
||||
tmp=matrix.sigmoid(me.hidden_res);
|
||||
me.output_res=matrix.mult_mat(tmp,me.output_layer);
|
||||
tmp=matrix.sigmoid(me.output_res);
|
||||
me.result=tmp;
|
||||
return;
|
||||
},
|
||||
backward:func(i)
|
||||
{
|
||||
var output_diff=matrix.sub_mat(me.expect_set[i],me.result);
|
||||
output_diff=matrix.hadamard(output_diff,me.learning_rate);
|
||||
output_diff=matrix.hadamard(output_diff,matrix.diffsigmoid(me.output_res));
|
||||
output_diff=matrix.mult_mat(output_diff,matrix.transpose(me.output_layer));
|
||||
matrix.prt_mat(output_diff);
|
||||
var hidden_diff=matrix.mult_mat();
|
||||
matrix.prt_mat(hidden_diff);
|
||||
|
||||
output_layer=matrix.add_mat(output_layer,output_diff);
|
||||
var error=0;
|
||||
foreach(var i;tmp.mat[0])
|
||||
error+=i;
|
||||
error*=0.5;
|
||||
return error;
|
||||
},
|
||||
training_process:func()
|
||||
{
|
||||
var cnt=0;
|
||||
var error=1e8;
|
||||
while(error>0.01)
|
||||
{
|
||||
error=0;
|
||||
for(var i=0;i<4;i+=1)
|
||||
{
|
||||
me.forward(i);
|
||||
error+=me.backward(i);
|
||||
}
|
||||
print(error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
var main=func()
|
||||
{
|
||||
bp.init();
|
||||
bp.set_learning_rate(0.1);
|
||||
bp.set_training_set();
|
||||
bp.set_expect_set();
|
||||
bp.training_process();
|
||||
return nil;
|
||||
}
|
||||
|
||||
# var (a,b,c)=[1,2,3];
|
||||
# (a,b,c)=(b,c,a);
|
||||
# print(a,b,c);
|
||||
main();
|
||||
@@ -4,7 +4,7 @@ var char_ttf=[
|
||||
[" ████╗"," ██╔██║"," ██╔╝██║"," ███████║","██╔═══██║","╚═╝ ╚═╝"],
|
||||
["██████╗ ","██╔══██╗","██████╔╝","██╔══██╗","██████╔╝","╚═════╝ "],
|
||||
[" ██████╗","██╔════╝","██║ ","██║ ","╚██████╗"," ╚═════╝"],
|
||||
["██████╗ ","██╔══██╗","██║ ██║","██║ ██║","██████╔╝","╚═════╝ "],
|
||||
["██████╗ ","██╔══██╗","██║ ██║","██║ ██║","██████╔╝","╚═════╝ "],
|
||||
["███████╗","██╔════╝","█████╗ ","██╔══╝ ","███████╗","╚══════╝"],
|
||||
["███████╗","██╔════╝","█████╗ ","██╔══╝ ","██║ ","╚═╝ "],
|
||||
[" █████╗ ","██╔═══╝ ","██║ ██╗ ","██║ ╚██╗","╚█████╔╝"," ╚════╝ "],
|
||||
@@ -45,7 +45,7 @@ var trans_ttf=func(string)
|
||||
str[j]~=char_ttf[0][j];
|
||||
}
|
||||
foreach(var i;str)
|
||||
print(i);
|
||||
println(i);
|
||||
return;
|
||||
}
|
||||
var curve1=func()
|
||||
@@ -53,9 +53,9 @@ var curve1=func()
|
||||
var shadow=["░","▒","▓","█","▀","▄","▐","▌"];
|
||||
rand(100);
|
||||
var s="";
|
||||
for(var i=0;i<25;i+=1)
|
||||
for(var i=0;i<10;i+=1)
|
||||
{
|
||||
for(var j=0;j<100;j+=1)
|
||||
for(var j=0;j<40;j+=1)
|
||||
s~=shadow[int(8*rand())];
|
||||
s~='\n';
|
||||
}
|
||||
@@ -66,28 +66,15 @@ var curve2=func()
|
||||
var table=["╚","═","╝","╔","║","╗"];
|
||||
rand(100);
|
||||
var s="";
|
||||
for(var i=0;i<25;i+=1)
|
||||
for(var i=0;i<10;i+=1)
|
||||
{
|
||||
for(var j=0;j<100;j+=1)
|
||||
for(var j=0;j<40;j+=1)
|
||||
s~=table[int(6*rand())];
|
||||
s~='\n';
|
||||
}
|
||||
print(s);
|
||||
}
|
||||
var curve3=func()
|
||||
{
|
||||
var block=["░░","▒▒","▓▓","██","▀▀","▄▄","▄▀","▀▄","▐▐","▌▌"];
|
||||
rand(100);
|
||||
var s="";
|
||||
for(var i=0;i<25;i+=1)
|
||||
{
|
||||
for(var j=0;j<50;j+=1)
|
||||
s~=block[int(10*rand())];
|
||||
s~='\n';
|
||||
}
|
||||
print(s);
|
||||
}
|
||||
var curve4=func()
|
||||
{
|
||||
var s=["","","","","",""];
|
||||
var cnt=0;
|
||||
@@ -100,7 +87,7 @@ var curve4=func()
|
||||
{
|
||||
forindex(var i;s)
|
||||
{
|
||||
print(s[i]);
|
||||
println(s[i]);
|
||||
s[i]='';
|
||||
}
|
||||
cnt=0;
|
||||
@@ -108,12 +95,12 @@ var curve4=func()
|
||||
}
|
||||
return;
|
||||
}
|
||||
var curve5=func()
|
||||
var curve4=func()
|
||||
{
|
||||
for(var loop=0;loop<100;loop+=1)
|
||||
var arr=[0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8];
|
||||
for(var loop=0;loop<10;loop+=1)
|
||||
{
|
||||
var arr=[0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8];
|
||||
for(var i=17;i>=0;i-=1)
|
||||
for(var i=26;i>=0;i-=1)
|
||||
{
|
||||
var rand_index=int(i*rand());
|
||||
(arr[i],arr[rand_index])=(arr[rand_index],arr[i]);
|
||||
@@ -123,13 +110,13 @@ var curve5=func()
|
||||
var s="";
|
||||
for(var i=0;i<size(arr);i+=1)
|
||||
s~=shadow[arr[i]];
|
||||
print(s);
|
||||
println(s);
|
||||
}
|
||||
return;
|
||||
}
|
||||
trans_ttf("just for test");
|
||||
trans_ttf(" ValKmjolnir ");
|
||||
curve1();
|
||||
curve2();
|
||||
curve3();
|
||||
curve4();
|
||||
curve5();
|
||||
curve4();
|
||||
@@ -1,5 +1,10 @@
|
||||
# Road check and auto pilot(??) by ValKmjolnir
|
||||
|
||||
# Road check and auto pilot by ValKmjolnir
|
||||
import("lib.nas");
|
||||
import("props.nas");
|
||||
var dt=0.01;
|
||||
var intergral=0;
|
||||
var derivative=0;
|
||||
var previous_error=0;
|
||||
var position_change = func(position_val,value){
|
||||
if(position_val+value>180)
|
||||
position_val += value-360;
|
||||
@@ -10,12 +15,11 @@ var position_change = func(position_val,value){
|
||||
return position_val;
|
||||
}
|
||||
var road_check_func = func(){
|
||||
|
||||
|
||||
var lat = props.getNode("/position/latitude-deg",1).getValue();
|
||||
var lon = props.getNode("/position/longitude-deg",1).getValue();
|
||||
var position_info = geodinfo(lat,lon);
|
||||
var position_names = position_info[1].names;
|
||||
# the friction_factor of freeway runway and road is 1
|
||||
|
||||
if((position_names[0]=="Freeway") or (position_names[0]=="Road"))
|
||||
{
|
||||
@@ -24,14 +28,14 @@ var road_check_func = func(){
|
||||
var lon_change = 0;
|
||||
var left_range = 0;
|
||||
var right_range = 0;
|
||||
|
||||
|
||||
for(var i=0;i>-0.00005;i-=0.000001)
|
||||
{
|
||||
car_heading = props.getNode("/orientation/heading-deg",1).getValue();
|
||||
lat_change = math.sin(math.pi*car_heading/180);
|
||||
lon_change = -math.cos(math.pi*car_heading/180);
|
||||
lat = props.getNode("/position/latitude-deg",1).getValue()+0.0001*math.cos(math.pi*car_heading/180);
|
||||
lon = props.getNode("/position/longitude-deg",1).getValue()+0.0001*math.sin(math.pi*car_heading/180);
|
||||
lat_change = math.sin(D2R*car_heading);
|
||||
lon_change = -math.cos(D2R*car_heading);
|
||||
lat = props.getNode("/position/latitude-deg",1).getValue()+0.0001*math.cos(D2R*car_heading);
|
||||
lon = props.getNode("/position/longitude-deg",1).getValue()+0.0001*math.sin(D2R*car_heading);
|
||||
var other_position_info = geodinfo(position_change(lat,i*lat_change),position_change(lon,i*lon_change));
|
||||
var other_names = other_position_info[1].names;
|
||||
if((other_names[0]=="Freeway") or (other_names[0]=="Road"))
|
||||
@@ -42,10 +46,10 @@ var road_check_func = func(){
|
||||
for(var i=0;i<0.00005;i+=0.000001)
|
||||
{
|
||||
car_heading = props.getNode("/orientation/heading-deg",1).getValue();
|
||||
lat_change = math.sin(math.pi*car_heading/180);
|
||||
lon_change = -math.cos(math.pi*car_heading/180);
|
||||
lat = props.getNode("/position/latitude-deg",1).getValue()+0.0001*math.cos(math.pi*car_heading/180);
|
||||
lon = props.getNode("/position/longitude-deg",1).getValue()+0.0001*math.sin(math.pi*car_heading/180);
|
||||
lat_change = math.sin(D2R*car_heading);
|
||||
lon_change = -math.cos(D2R*car_heading);
|
||||
lat = props.getNode("/position/latitude-deg",1).getValue()+0.0001*math.cos(D2R*car_heading);
|
||||
lon = props.getNode("/position/longitude-deg",1).getValue()+0.0001*math.sin(D2R*car_heading);
|
||||
var other_position_info = geodinfo(position_change(lat,i*lat_change),position_change(lon,i*lon_change));
|
||||
var other_names = other_position_info[1].names;
|
||||
if((other_names[0]=="Freeway") or (other_names[0]=="Road"))
|
||||
@@ -53,25 +57,26 @@ var road_check_func = func(){
|
||||
else
|
||||
break;
|
||||
}
|
||||
#if(left_range>right_range)
|
||||
#{
|
||||
# setprop("/controls/flight/rudder",-(right_range-left_range)*(right_range-left_range)/10000);
|
||||
# print("right ",right_range);
|
||||
#}
|
||||
#else if(left_range<right_range)
|
||||
#{
|
||||
# setprop("/controls/flight/rudder",(right_range-left_range)*(right_range-left_range)/10000);
|
||||
# print("left ",left_range);
|
||||
#}
|
||||
#else
|
||||
# setprop("/controls/flight/rudder",0);
|
||||
props.getNode("/controls/flight/rudder",1).setValue((right_range-left_range)/200);
|
||||
var error=right_range-left_range;
|
||||
intergral+=error*dt;
|
||||
derivative=(error-previous_error)/dt;
|
||||
var (Kp,Ki,Kd)=(1/900,0.05,0.005);
|
||||
# print("change p ",Kp*error*error,' i ',Ki*intergral,' d ',Kd*derivative);
|
||||
if(error<0)
|
||||
props.getNode("/", 1).setValue("/controls/flight/rudder",-Kp*error*error+Ki*intergral+Kd*derivative);
|
||||
else if(error>0)
|
||||
props.getNode("/", 1).setValue("/controls/flight/rudder",Kp*error*error+Ki*intergral+Kd*derivative);
|
||||
else
|
||||
props.getNode("/", 1).setValue("/controls/flight/rudder",0);
|
||||
previous_error=error;
|
||||
}
|
||||
};
|
||||
var road_check_timer = maketimer(0.1,road_check_func);
|
||||
|
||||
var road_check_timer = maketimer(0.01,road_check_func);
|
||||
var toggle_auto_pilot = func(){
|
||||
if(!road_check_timer.isRunning)
|
||||
{
|
||||
intergral=0;
|
||||
road_check_timer.start();
|
||||
props.getNode("/sim/messages/copilot",1).setValue("ze dong sheng teaan see tong yee tse yung. Auto Sheng Teaan System Activated!");
|
||||
}
|
||||
@@ -80,4 +85,4 @@ var toggle_auto_pilot = func(){
|
||||
road_check_timer.stop();
|
||||
props.getNode("/sim/messages/copilot",1).setValue("ze dong sheng teaan see tong yee guan bee. Auto Sheng Teaan System is off.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
240
test/bf.nas
Normal file
240
test/bf.nas
Normal file
@@ -0,0 +1,240 @@
|
||||
import("lib.nas");
|
||||
|
||||
var mandelbrot=
|
||||
"[A mandelbrot set fractal viewer in brainf*** written by Erik Bosman]
|
||||
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
|
||||
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
|
||||
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>
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+<
|
||||
<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<
|
||||
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>
|
||||
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
|
||||
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
|
||||
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
|
||||
<<<<<]]>>>]";
|
||||
|
||||
var paper=[];
|
||||
var (ptr,pc)=(0,0);
|
||||
var (code,inum,stack)=(nil,nil,nil);
|
||||
var (add,mov,jt,jf,in,out)=(0,1,2,3,4,5);
|
||||
var func_table=[
|
||||
func(){paper[ptr]+=inum[pc];return;},
|
||||
func(){ptr+=inum[pc];return;},
|
||||
func(){if(paper[ptr])pc=inum[pc];return;},
|
||||
func(){if(!paper[ptr])pc=inum[pc];return;},
|
||||
func(){paper[ptr]=input()[0];return;},
|
||||
func(){print(chr(paper[ptr]));return;}
|
||||
];
|
||||
|
||||
var bf=func(program)
|
||||
{
|
||||
setsize(paper,131072);
|
||||
(ptr,code,inum,stack)=(0,[],[],[]);
|
||||
var len=size(program);
|
||||
|
||||
for(var i=0;i<len;i+=1)
|
||||
{
|
||||
var c=chr(program[i]);
|
||||
if(c=='+' or c=='-')
|
||||
{
|
||||
append(code,func_table[add]);
|
||||
append(inum,0);
|
||||
for(;i<len;i+=1)
|
||||
{
|
||||
if(chr(program[i])=='+')
|
||||
inum[-1]+=1;
|
||||
elsif(chr(program[i])=='-')
|
||||
inum[-1]-=1;
|
||||
else
|
||||
{
|
||||
i-=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
elsif(c=='<' or c=='>')
|
||||
{
|
||||
append(code,func_table[mov]);
|
||||
append(inum,0);
|
||||
for(;i<len;i+=1)
|
||||
{
|
||||
if(chr(program[i])=='>')
|
||||
inum[-1]+=1;
|
||||
elsif(chr(program[i])=='<')
|
||||
inum[-1]-=1;
|
||||
else
|
||||
{
|
||||
i-=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
elsif(c==',')
|
||||
{
|
||||
append(code,func_table[in]);
|
||||
append(inum,0);
|
||||
}
|
||||
elsif(c=='.')
|
||||
{
|
||||
append(code,func_table[out]);
|
||||
append(inum,0);
|
||||
}
|
||||
elsif(c=='[')
|
||||
{
|
||||
append(code,func_table[jf]);
|
||||
append(inum,0);
|
||||
append(stack,size(code)-1);
|
||||
}
|
||||
elsif(c==']')
|
||||
{
|
||||
if(!size(stack))
|
||||
die("lack [");
|
||||
var label=pop(stack);
|
||||
append(code,func_table[jt]);
|
||||
append(inum,label-1);
|
||||
inum[label]=size(code)-2;
|
||||
}
|
||||
}
|
||||
if(size(stack))
|
||||
die("lack ]");
|
||||
len=size(code);
|
||||
for(pc=0;pc<len;pc+=1)
|
||||
code[pc]();
|
||||
return;
|
||||
}
|
||||
|
||||
bf(mandelbrot);
|
||||
248
test/bfconvertor.nas
Normal file
248
test/bfconvertor.nas
Normal file
@@ -0,0 +1,248 @@
|
||||
import("lib.nas");
|
||||
|
||||
var mandelbrot=
|
||||
"[A mandelbrot set fractal viewer in brainf*** written by Erik Bosman]
|
||||
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
|
||||
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
|
||||
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>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>[>>>>>>[-<<<<<<->>>>>>]+<
|
||||
<<<<<[->>>>>>->>[-<<<<<<<<+>>>>>>>>]<<<<<<<<[->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<
|
||||
<<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>>
|
||||
-<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>>
|
||||
>>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
|
||||
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
|
||||
<<<<<]]>>>]";
|
||||
|
||||
var bf=func(program)
|
||||
{
|
||||
var stack=[];
|
||||
var len=size(program);
|
||||
var f="import('lib.nas');\nvar ptr=0;\nvar paper=[];\nsetsize(paper,131072);\n";
|
||||
for(var i=0;i<len;i+=1)
|
||||
{
|
||||
var c=chr(program[i]);
|
||||
if(c=='+' or c=='-')
|
||||
{
|
||||
var cnt=0;
|
||||
for(;i<len;i+=1)
|
||||
{
|
||||
if(chr(program[i])=='+')
|
||||
cnt+=1;
|
||||
elsif(chr(program[i])=='-')
|
||||
cnt-=1;
|
||||
else
|
||||
{
|
||||
i-=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(cnt!=0)
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
if(cnt>0)
|
||||
f~="paper[ptr]+="~cnt~";\n";
|
||||
elsif(cnt<0)
|
||||
f~="paper[ptr]-="~(-cnt)~";\n";
|
||||
}
|
||||
elsif(c=='<' or c=='>')
|
||||
{
|
||||
var cnt=0;
|
||||
for(;i<len;i+=1)
|
||||
{
|
||||
if(chr(program[i])=='>')
|
||||
cnt+=1;
|
||||
elsif(chr(program[i])=='<')
|
||||
cnt-=1;
|
||||
else
|
||||
{
|
||||
i-=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(cnt!=0)
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
if(cnt>0)
|
||||
f~="ptr+="~cnt~";\n";
|
||||
elsif(cnt<0)
|
||||
f~="ptr-="~(-cnt)~";\n";
|
||||
}
|
||||
elsif(c==',')
|
||||
{
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
f~="paper[ptr]=input();\n";
|
||||
}
|
||||
elsif(c=='.')
|
||||
{
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
f~="print(chr(paper[ptr]));\n";
|
||||
}
|
||||
elsif(c=='[')
|
||||
{
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
f~="while(paper[ptr])\n";
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
f~="{\n";
|
||||
append(stack,0);
|
||||
}
|
||||
elsif(c==']')
|
||||
{
|
||||
if(!size(stack))
|
||||
{
|
||||
println("lack [");
|
||||
return;
|
||||
}
|
||||
pop(stack);
|
||||
for(var j=0;j<size(stack);j+=1)
|
||||
f~='\t';
|
||||
f~="}\n";
|
||||
}
|
||||
}
|
||||
if(size(stack))
|
||||
{
|
||||
println("lack ]");
|
||||
return;
|
||||
}
|
||||
io.fout("mandel.nas",f);
|
||||
return;
|
||||
}
|
||||
|
||||
bf(mandelbrot);
|
||||
29
test/bfs.nas
29
test/bfs.nas
@@ -1,5 +1,5 @@
|
||||
import("lib.nas");
|
||||
import("queue.nas");
|
||||
import("stl/lib.nas");
|
||||
import("stl/queue.nas");
|
||||
|
||||
rand(time(0));
|
||||
|
||||
@@ -20,19 +20,20 @@ var prt=func()
|
||||
s~=map[i][j];
|
||||
s~='\n';
|
||||
}
|
||||
s~='----------\n';
|
||||
print(s);
|
||||
}
|
||||
|
||||
var bfs=func(begin,end)
|
||||
{
|
||||
var move=[[1,0],[0,1],[-1,0],[0,-1]];
|
||||
var queue=new_queue();
|
||||
queue_push(queue,begin);
|
||||
var que=queue();
|
||||
que.push(begin);
|
||||
map[begin[0]][begin[1]]=3;
|
||||
while(!queue_empty(queue))
|
||||
while(!que.empty())
|
||||
{
|
||||
var vertex=queue_front(queue);
|
||||
queue_pop(queue);
|
||||
var vertex=que.front();
|
||||
que.pop();
|
||||
foreach(var i;move)
|
||||
{
|
||||
var x=vertex[0]+i[0];
|
||||
@@ -44,22 +45,14 @@ var bfs=func(begin,end)
|
||||
}
|
||||
if(0<=x and x<10 and 0<=y and y<10 and map[x][y]==0)
|
||||
{
|
||||
queue_push(queue,[x,y]);
|
||||
que.push([x,y]);
|
||||
map[x][y]=3;
|
||||
}
|
||||
}
|
||||
prt();
|
||||
}
|
||||
print("cannot reach.");
|
||||
print("cannot reach.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
prt();
|
||||
var x=num(input());
|
||||
var y=num(input());
|
||||
var begin=[x,y];
|
||||
x=num(input());
|
||||
y=num(input());
|
||||
var end=[x,y];
|
||||
bfs(begin,end);
|
||||
prt();
|
||||
bfs([0,0],[9,9]);
|
||||
1
test/bigloop.nas
Normal file
1
test/bigloop.nas
Normal file
@@ -0,0 +1 @@
|
||||
for(var i=0;i<4e6;i+=1);
|
||||
42
test/bp.nas
42
test/bp.nas
@@ -4,14 +4,13 @@ rand(time(0));
|
||||
|
||||
var new_neuron=func()
|
||||
{
|
||||
var neuron={
|
||||
return {
|
||||
in:0,
|
||||
out:0,
|
||||
w:[],
|
||||
bia:0,
|
||||
diff:0
|
||||
};
|
||||
return neuron;
|
||||
}
|
||||
|
||||
var sigmoid=func(x)
|
||||
@@ -24,12 +23,9 @@ var diffsigmoid=func(x)
|
||||
return x*(1-x);
|
||||
}
|
||||
|
||||
var inum=2;
|
||||
var hnum=4;
|
||||
var onum=1;
|
||||
var lr=0.1;
|
||||
var (inum,hnum,onum,lr)=(2,4,1,0.1);
|
||||
var training_set=[[0,0],[0,1],[1,0],[1,1]];
|
||||
var expect=[[0],[1],[1],[0]];
|
||||
var expect=[0,1,1,0];
|
||||
|
||||
var hidden=[];
|
||||
for(var i=0;i<hnum;i+=1)
|
||||
@@ -89,19 +85,12 @@ var run=func(vec)
|
||||
}
|
||||
var get_error=func(x)
|
||||
{
|
||||
var error=0;
|
||||
var expect_set=expect[x];
|
||||
for(var i=0;i<onum;i+=1)
|
||||
error+=(expect_set[i]-output[i].out)*(expect_set[i]-output[i].out);
|
||||
error*=0.5;
|
||||
return error;
|
||||
return 0.5*(expect[x]-output[0].out)*(expect[x]-output[0].out);
|
||||
}
|
||||
var backward=func(x)
|
||||
{
|
||||
var input=training_set[x];
|
||||
var expect_set=expect[x];
|
||||
for(var i=0;i<onum;i+=1)
|
||||
output[i].diff=(expect_set[i]-output[i].out)*diffsigmoid(output[i].in);
|
||||
output[0].diff=(expect[x]-output[0].out)*diffsigmoid(output[0].in);
|
||||
for(var i=0;i<hnum;i+=1)
|
||||
{
|
||||
hidden[i].diff=0;
|
||||
@@ -124,9 +113,8 @@ var backward=func(x)
|
||||
return;
|
||||
}
|
||||
|
||||
var cnt=0;
|
||||
var error=1e8;
|
||||
while(error>0.01)
|
||||
var (cnt,error)=(0,100);
|
||||
while(error>0.0005)
|
||||
{
|
||||
error=0;
|
||||
for(var i=0;i<4;i+=1)
|
||||
@@ -136,18 +124,10 @@ while(error>0.01)
|
||||
backward(i);
|
||||
}
|
||||
cnt+=1;
|
||||
print('epoch ',cnt,':',error);
|
||||
}
|
||||
print('\afinished.');
|
||||
while(1)
|
||||
print('finished after ',cnt,' epoch.\n');
|
||||
foreach(var v;training_set)
|
||||
{
|
||||
var vec=[];
|
||||
var command=input();
|
||||
if(command=="exit")break;
|
||||
append(vec,num(command));
|
||||
command=input();
|
||||
if(command=="exit")break;
|
||||
append(vec,num(command));
|
||||
run(vec);
|
||||
print(output[0].out);
|
||||
run(v);
|
||||
print(v,': ',output[0].out,'\n');
|
||||
}
|
||||
32
test/calc.nas
Normal file
32
test/calc.nas
Normal file
@@ -0,0 +1,32 @@
|
||||
import("lib.nas");
|
||||
var filename=["main.cpp","nasal_ast.h","nasal_builtin.h","nasal_codegen.h","nasal_gc.h","nasal_import.h","nasal_lexer.h","nasal_parse.h","nasal_vm.h","nasal.h"];
|
||||
var space=[" "," ","",""," "," "," "," "," "," "];
|
||||
var enter_cnt=func(s)
|
||||
{
|
||||
var (cnt,len,enter)=(0,size(s),'\n'[0]);
|
||||
for(var i=0;i<len;i+=1)
|
||||
cnt+=(s[i]==enter);
|
||||
return cnt;
|
||||
}
|
||||
var semic_cnt=func(s)
|
||||
{
|
||||
var (cnt,len,semi)=(0,size(s),';'[0]);
|
||||
for(var i=0;i<len;i+=1)
|
||||
cnt+=(s[i]==semi);
|
||||
return cnt;
|
||||
}
|
||||
func(){
|
||||
var (bytes,line,semi)=(0,0,0);
|
||||
forindex(var i;filename)
|
||||
{
|
||||
var s=io.fin(filename[i]);
|
||||
bytes+=size(s);
|
||||
var line_cnt=enter_cnt(s);
|
||||
var semi_cnt=semic_cnt(s);
|
||||
println(filename[i],space[i],'| ',line_cnt,' \tline | ',semi_cnt,' \tsemi');
|
||||
line+=line_cnt;
|
||||
semi+=semi_cnt;
|
||||
}
|
||||
println('total: | ',line,' \tline | ',semi,' \tsemi');
|
||||
println('bytes: | ',bytes,' bytes| ',int(bytes/1024),' \tkb');
|
||||
}();
|
||||
@@ -1,30 +0,0 @@
|
||||
id[0] and id[1];
|
||||
1 or 2;
|
||||
"str" ==1;
|
||||
1*1/1+1;
|
||||
2*3*4/1-2+3;
|
||||
1-1+20-2*10;
|
||||
1+s1*(s2+s3[0])-1;
|
||||
var e=1+s1*(s2+s3[0])-1;
|
||||
id;
|
||||
"str";
|
||||
id(id);
|
||||
id("str",1,1);
|
||||
var e=1;
|
||||
var x=10*2-20;
|
||||
var id;
|
||||
var id=[1,2,3,4];
|
||||
var id={id:"str"};
|
||||
id();
|
||||
id.id();
|
||||
id.id.id();
|
||||
id[0].id.id(id,"str",1,2,3,4).id[10];
|
||||
id(0)[1].id;
|
||||
var hash={
|
||||
h:"hello",
|
||||
parent:[id],
|
||||
};
|
||||
function_test([1,2,3,4,55],1,2,3,{str:"str"});
|
||||
var (i,j,k,l,m,n) =[1,2,3,4,5,6];
|
||||
(var i,j,k)=[1,2,3];
|
||||
e=e[1:][0];
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
import("lib.nas");
|
||||
var condition_true=1;
|
||||
var condition_false=0;
|
||||
if(condition_true)
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
if(this_token.type==__if)
|
||||
{
|
||||
parse.push(this_token);
|
||||
return;
|
||||
}
|
||||
elsif(this_token.type==__elsif)
|
||||
{
|
||||
parse.push(this_token);
|
||||
return;
|
||||
}
|
||||
else if(this_token.type!=__else)
|
||||
{
|
||||
exit(0);
|
||||
}
|
||||
elsif(this_token.type==__elsif)
|
||||
{
|
||||
parse.push(this_token);
|
||||
return;
|
||||
}
|
||||
elsif(this_token.type==__elsif)
|
||||
{
|
||||
parse.push(this_token);
|
||||
return;
|
||||
}
|
||||
else if(this==(1+2+3*1))
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
else
|
||||
{
|
||||
parse.push(this_token);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!id)
|
||||
{
|
||||
exit(0);
|
||||
}
|
||||
27
test/class.nas
Normal file
27
test/class.nas
Normal file
@@ -0,0 +1,27 @@
|
||||
import("lib.nas");
|
||||
|
||||
var student=func(name,age)
|
||||
{
|
||||
var val={
|
||||
name:name,
|
||||
age:age
|
||||
};
|
||||
return {
|
||||
print_info:func(){println(val.name,' ',val.age);},
|
||||
set_age: func(age){val.age=age;},
|
||||
get_age: func(){return val.age;},
|
||||
set_name: func(name){val.name=name;},
|
||||
get_name: func(){return val.name;}
|
||||
};
|
||||
}
|
||||
var s=student('valk',24);
|
||||
s.print_info();
|
||||
println(s.get_age(),' ',s.get_name());
|
||||
s.set_age(20);
|
||||
s.set_name('aluo');
|
||||
s.print_info();
|
||||
println(s.get_age(),' ',s.get_name());
|
||||
s.set_age(20);
|
||||
s.set_name('Sidi Liang');
|
||||
s.print_info();
|
||||
println(s.get_age(),' ',s.get_name());
|
||||
2044
test/efb.nas
2044
test/efb.nas
File diff suppressed because it is too large
Load Diff
9
test/fib.nas
Normal file
9
test/fib.nas
Normal file
@@ -0,0 +1,9 @@
|
||||
import("lib.nas");
|
||||
var fib=func(x)
|
||||
{
|
||||
if(x<2) return x;
|
||||
return fib(x-1)+fib(x-2);
|
||||
}
|
||||
for(var i=0;i<31;i+=1)
|
||||
print(fib(i),'\n');
|
||||
|
||||
210
test/final.nas
210
test/final.nas
@@ -1,210 +0,0 @@
|
||||
var smartScreen = canvas.new({
|
||||
"name": "smartScreen", # The name is optional but allow for easier identification
|
||||
"size": [2048, 2048], # Size of the underlying texture (should be a power of 2, required) [Resolution]
|
||||
"view": [768, 768], # Virtual resolution (Defines the coordinate system of the canvas [Dimensions]
|
||||
# which will be stretched the size of the texture, required)
|
||||
"mipmapping": 1 # Enable mipmapping (optional)
|
||||
});
|
||||
|
||||
smartScreen.addPlacement({"node": "screen", "texture": "screen.jpeg"});
|
||||
var group = smartScreen.createGroup();
|
||||
|
||||
# Create a text element and set some values
|
||||
var text = group.createChild("text", "optional-id-for element")
|
||||
.setTranslation(10, 20) # The origin is in the top left corner
|
||||
.setAlignment("left-center") # All values from osgText are supported (see $FG_ROOT/Docs/README.osgtext)
|
||||
.setFont("LiberationFonts/LiberationSans-Regular.ttf") # Fonts are loaded either from $AIRCRAFT_DIR/Fonts or $FG_ROOT/Fonts
|
||||
.setFontSize(14, 1.2) # Set fontsize and optionally character aspect ratio
|
||||
.setColor(1,0,0) # Text color
|
||||
.setText("This is a text element");
|
||||
text.hide();
|
||||
text.setText("SELF TEST NORMAL").show();
|
||||
|
||||
|
||||
|
||||
var ui_root = smartScreen.createGroup();
|
||||
var vbox = canvas.VBoxLayout.new();
|
||||
smartScreen.setLayout(vbox);
|
||||
|
||||
|
||||
var button_onl = canvas.gui.widgets.Button.new(ui_root, canvas.style, {}).setText("Online OSM").listen("clicked", func showOnlineMap());
|
||||
var button_offl = canvas.gui.widgets.Button.new(ui_root, canvas.style, {}).setText("Offline OSM").listen("clicked", func showOfflineMap());
|
||||
button_onl.setSizeHint([32, 128]);
|
||||
button_offl.setSizeHint([32, 128]);
|
||||
|
||||
var label = canvas.gui.widgets.Label.new(ui_root, canvas.style, {});
|
||||
|
||||
var button_box = canvas.HBoxLayout.new();
|
||||
button_box.addItem(button_onl);
|
||||
button_box.addItem(button_offl);
|
||||
button_box.addItem(label);
|
||||
button_box.addStretch(1);
|
||||
|
||||
vbox.addItem(button_box);
|
||||
vbox.addStretch(1);
|
||||
|
||||
|
||||
var showOnlineMap = func(){
|
||||
TestMap.show();
|
||||
g.hide();
|
||||
label.setText("Online Mode");
|
||||
}
|
||||
|
||||
|
||||
var showOfflineMap = func(){
|
||||
TestMap.hide();
|
||||
g.show();
|
||||
label.setText("Offline Mode");
|
||||
}
|
||||
|
||||
|
||||
# Online Map using MapStructure
|
||||
var TestMap = smartScreen.createGroup().createChild("map");
|
||||
TestMap.setTranslation(smartScreen.get("view[0]")/2,smartScreen.get("view[1]")/2);
|
||||
|
||||
|
||||
var ctrl_ns = canvas.Map.Controller.get("Aircraft position");
|
||||
var source = ctrl_ns.SOURCES["map-dialog"];
|
||||
if (source == nil) {
|
||||
# TODO: amend
|
||||
var source = ctrl_ns.SOURCES["map-dialog"] = {
|
||||
getPosition: func subvec(geo.aircraft_position().latlon(), 0, 2),# ? ? ?
|
||||
getAltitude: func getprop('/position/altitude-ft'),
|
||||
getHeading: func {
|
||||
if (me.aircraft_heading)
|
||||
getprop('/orientation/heading-deg');
|
||||
else
|
||||
0;
|
||||
},
|
||||
aircraft_heading: 1,
|
||||
};
|
||||
}
|
||||
setlistener("/sim/gui/dialogs/map-canvas/aircraft-heading-up", func(n){source.aircraft_heading = n.getBoolValue();}, 1);
|
||||
TestMap.setController("Aircraft position", "map-dialog");
|
||||
TestMap.setRange(1);
|
||||
|
||||
var r = func(name,vis=1,zindex=nil){return caller(0)[0];};
|
||||
# TODO: we'll need some z-indexing here, right now it's just random
|
||||
|
||||
foreach(var type; [r('APS')] ){
|
||||
TestMap.addLayer(factory: canvas.SymbolLayer, type_arg: type.name, visible: type.vis, priority: 2);
|
||||
}
|
||||
|
||||
foreach(var type; [ r('OSM')]) {
|
||||
TestMap.addLayer(factory: canvas.OverlayLayer,
|
||||
type_arg: type.name,
|
||||
visible: type.vis,
|
||||
priority: 1);
|
||||
}
|
||||
|
||||
|
||||
TestMap.hide();
|
||||
|
||||
# Offline map
|
||||
|
||||
var g = smartScreen.createGroup();
|
||||
var zoom = 15;
|
||||
var type = "intl";
|
||||
var tile_size = 256;
|
||||
|
||||
|
||||
var changeZoom = func(d)
|
||||
{
|
||||
zoom = math.max(2, math.min(19, zoom + d));
|
||||
updateTiles();
|
||||
}
|
||||
|
||||
# http://polymaps.org/docs/
|
||||
# https://github.com/simplegeo/polymaps
|
||||
# https://github.com/Leaflet/Leaflet
|
||||
|
||||
var maps_base = getprop("/sim/fg-home") ~ '/cache/maps';
|
||||
|
||||
var makePath =
|
||||
string.compileTemplate(maps_base ~ '/osm-{type}/{z}/{x}/{y}.jpg');
|
||||
var num_tiles = [4, 4];
|
||||
|
||||
var center_tile_offset = [(num_tiles[0]-1)/2, (num_tiles[1]-1)/ 2];
|
||||
|
||||
# simple aircraft icon at current position/center of the map
|
||||
g.createChild("path")
|
||||
.moveTo( tile_size*center_tile_offset[0]-10, tile_size*center_tile_offset[1])
|
||||
.horiz(20)
|
||||
.move(-10,-10)
|
||||
.vert(20)
|
||||
.set("stroke", "red")
|
||||
.set("stroke-width", 2)
|
||||
.set("z-index", 1);
|
||||
|
||||
# initialize the map by setting up
|
||||
# a grid of raster images
|
||||
|
||||
var tiles = setsize([], num_tiles[0]);
|
||||
for(var x=0; x<num_tiles[0]; x+=1)
|
||||
{
|
||||
tiles[x] = setsize([], num_tiles[1]);
|
||||
for(var y=0; y<num_tiles[1]; y+=1)
|
||||
tiles[x][y] = g.createChild("image", "map-tile");
|
||||
}
|
||||
|
||||
var last_tile = [-1,-1];
|
||||
var last_type = type;
|
||||
|
||||
# this is the callback that will be regularly called by the timer
|
||||
# to update the map
|
||||
var updateTiles = func()
|
||||
{
|
||||
# get current position
|
||||
var lat = getprop('/position/latitude-deg');
|
||||
var lon = getprop('/position/longitude-deg');
|
||||
|
||||
var n = math.pow(2, zoom);
|
||||
var offset = [n*((lon+180)/360)-center_tile_offset[0], (1-math.ln(math.tan(lat*math.pi/180)+1/math.cos(lat*math.pi/180))/math.pi)/2*n-center_tile_offset[1]];
|
||||
var tile_index = [int(offset[0]), int(offset[1])];
|
||||
|
||||
var ox = tile_index[0] - offset[0];
|
||||
var oy = tile_index[1] - offset[1];
|
||||
|
||||
for(var x = 0; x < num_tiles[0]; x += 1)
|
||||
for(var y = 0; y < num_tiles[1]; y += 1)
|
||||
tiles[x][y].setTranslation(int((ox + x) * tile_size + 0.5), int((oy + y) * tile_size + 0.5));
|
||||
|
||||
if( tile_index[0] != last_tile[0]
|
||||
or tile_index[1] != last_tile[1]
|
||||
or type != last_type )
|
||||
{
|
||||
for(var x = 0; x < num_tiles[0]; x += 1)
|
||||
for(var y = 0; y < num_tiles[1]; y += 1)
|
||||
{
|
||||
var pos = {
|
||||
z: zoom,
|
||||
x: int(offset[0] + x),
|
||||
y: int(offset[1] + y),
|
||||
type: type
|
||||
};
|
||||
|
||||
(func {
|
||||
var img_path = makePath(pos);
|
||||
var tile = tiles[x][y];
|
||||
print('loading ' ~ img_path);
|
||||
tile.set("src", img_path);
|
||||
})();
|
||||
# lambda
|
||||
}
|
||||
|
||||
last_tile = tile_index;
|
||||
last_type = type;
|
||||
}
|
||||
};
|
||||
|
||||
# set up a timer that will invoke updateTiles at 2-second intervals
|
||||
var update_timer = maketimer(2, updateTiles);
|
||||
# actually start the timer
|
||||
update_timer.start();
|
||||
|
||||
# set up default zoom level
|
||||
changeZoom(0);
|
||||
|
||||
#g.hide();
|
||||
|
||||
|
||||
643
test/final2.nas
643
test/final2.nas
@@ -1,643 +0,0 @@
|
||||
###############################################################################
|
||||
##
|
||||
## Nasal module for dual control over the multiplayer network.
|
||||
##
|
||||
## Copyright (C) 2007 - 2010 Anders Gidenstam (anders(at)gidenstam.org)
|
||||
## This file is licensed under the GPL license version 2 or later.
|
||||
##
|
||||
###############################################################################
|
||||
## MP properties
|
||||
var lat_mpp = "position/latitude-deg";
|
||||
var lon_mpp = "position/longitude-deg";
|
||||
var alt_mpp = "position/altitude-ft";
|
||||
var heading_mpp = "orientation/true-heading-deg";
|
||||
var pitch_mpp = "orientation/pitch-deg";
|
||||
var roll_mpp = "orientation/roll-deg";
|
||||
|
||||
# Import components from the mp_broadcast module.
|
||||
var Binary = mp_broadcast.Binary;
|
||||
var MessageChannel = mp_broadcast.MessageChannel;
|
||||
|
||||
###############################################################################
|
||||
# Utility classes
|
||||
|
||||
############################################################
|
||||
# Translate a property into another.
|
||||
# Factor and offsets are only used for numeric values.
|
||||
# src - source : property node
|
||||
# dest - destination : property node
|
||||
# factor - : double
|
||||
# offset - : double
|
||||
var Translator = {};
|
||||
Translator.new = func (src = nil, dest = nil, factor = 1, offset = 0) {
|
||||
var obj = { parents : [Translator],
|
||||
src : src,
|
||||
dest : dest,
|
||||
factor : factor,
|
||||
offset : offset };
|
||||
if (obj.src == nil or obj.dest == nil) {
|
||||
print("Translator[");
|
||||
print(" ", debug.string(obj.src));
|
||||
print(" ", debug.string(obj.dest));
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
Translator.update = func () {
|
||||
var v = me.src.getValue();
|
||||
if (is_num(v)) {
|
||||
me.dest.setValue(me.factor * v + me.offset);
|
||||
} else {
|
||||
if (typeof(v) == "scalar")
|
||||
me.dest.setValue(v);
|
||||
}
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Detects flanks on two insignals encoded in a property.
|
||||
# - positive signal up/down flank
|
||||
# - negative signal up/down flank
|
||||
# n - source : property node
|
||||
# on_positive_flank - action : func (v)
|
||||
# on_negative_flank - action : func (v)
|
||||
var EdgeTrigger = {};
|
||||
EdgeTrigger.new = func (n, on_positive_flank, on_negative_flank) {
|
||||
var obj = { parents : [EdgeTrigger],
|
||||
old : 0,
|
||||
node : n,
|
||||
pos_flank : on_positive_flank,
|
||||
neg_flank : on_negative_flank };
|
||||
if (obj.node == nil) {
|
||||
print("EdgeTrigger[");
|
||||
print(" ", debug.string(obj.node));
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
EdgeTrigger.update = func {
|
||||
# NOTE: float MP properties get interpolated.
|
||||
# This detector relies on that steady state is reached between
|
||||
# flanks.
|
||||
var val = me.node.getValue();
|
||||
if (!is_num(val)) return;
|
||||
if (me.old == 1) {
|
||||
if (val < me.old) {
|
||||
me.pos_flank(0);
|
||||
}
|
||||
} elsif (me.old == 0) {
|
||||
if (val > me.old) {
|
||||
me.pos_flank(1);
|
||||
} elsif (val < me.old) {
|
||||
me.neg_flank(1);
|
||||
}
|
||||
} elsif (me.old == -1) {
|
||||
if (val > me.old) {
|
||||
me.neg_flank(0);
|
||||
}
|
||||
}
|
||||
me.old = val;
|
||||
}
|
||||
|
||||
############################################################
|
||||
# StableTrigger: Triggers an action when a MPP property
|
||||
# becomes stable (i.e. doesn't change for
|
||||
# MIN_STABLE seconds).
|
||||
# src - MP prop : property node
|
||||
# action - action to take when the value becomes stable : [func(v)]
|
||||
# An action is triggered when value has stabilized.
|
||||
var StableTrigger = {};
|
||||
StableTrigger.new = func (src, action) {
|
||||
var obj = { parents : [StableTrigger],
|
||||
src : src,
|
||||
action : action,
|
||||
old : 0,
|
||||
stable_since : 0,
|
||||
wait : 0,
|
||||
MIN_STABLE : 0.01 };
|
||||
# Error checking.
|
||||
var bad = (obj.src == nil) or (action = nil);
|
||||
|
||||
if (bad) {
|
||||
print("StableTrigger[");
|
||||
print(" ", debug.string(obj.src));
|
||||
print(" ", debug.string(obj.action));
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
StableTrigger.update = func () {
|
||||
var v = me.src.getValue();
|
||||
if (!is_num(v)) return;
|
||||
var t = getprop("/sim/time/elapsed-sec"); # NOTE: simulated time.
|
||||
|
||||
if ((me.old == v) and
|
||||
((t - me.stable_since) > me.MIN_STABLE) and (me.wait == 1)) {
|
||||
# Trigger action.
|
||||
me.action(v);
|
||||
|
||||
me.wait = 0;
|
||||
} elsif (me.old == v) {
|
||||
# Wait. This is either before the signal is stable or after the action.
|
||||
} else {
|
||||
me.stable_since = t;
|
||||
me.wait = 1;
|
||||
me.old = me.src.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Selects the most recent value of two properties.
|
||||
# src1 - : property node
|
||||
# src2 - : property node
|
||||
# dest - : property node
|
||||
# threshold - : double
|
||||
var MostRecentSelector = {};
|
||||
MostRecentSelector.new = func (src1, src2, dest, threshold) {
|
||||
var obj = { parents : [MostRecentSelector],
|
||||
old1 : 0,
|
||||
old2 : 0,
|
||||
src1 : src1,
|
||||
src2 : src2,
|
||||
dest : dest,
|
||||
thres : threshold };
|
||||
if (obj.src1 == nil or obj.src2 == nil or obj.dest == nil) {
|
||||
print("MostRecentSelector[");
|
||||
print(" ", debug.string(obj.src1));
|
||||
print(" ", debug.string(obj.src2));
|
||||
print(" ", debug.string(obj.dest));
|
||||
print("]");
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
MostRecentSelector.update = func {
|
||||
var v1 = me.src1.getValue();
|
||||
var v2 = me.src2.getValue();
|
||||
if (!is_num(v1) and !is_num(v2)) return;
|
||||
elsif (!is_num(v1)) me.dest.setValue(v2);
|
||||
elsif (!is_num(v2)) me.dest.setValue(v1);
|
||||
else {
|
||||
if (abs (v2 - me.old2) > me.thres) {
|
||||
me.old2 = v2;
|
||||
me.dest.setValue(me.old2);
|
||||
}
|
||||
if (abs (v1 - me.old1) > me.thres) {
|
||||
me.old1 = v1;
|
||||
me.dest.setValue(me.old1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Adds two input properties.
|
||||
# src1 - : property node
|
||||
# src2 - : property node
|
||||
# dest - : property node
|
||||
var Adder = {};
|
||||
Adder.new = func (src1, src2, dest) {
|
||||
var obj = { parents : [DeltaAccumulator],
|
||||
src1 : src1,
|
||||
src2 : src2,
|
||||
dest : dest };
|
||||
if (obj.src1 == nil or obj.src2 == nil or obj.dest == nil) {
|
||||
print("Adder[");
|
||||
print(" ", debug.string(obj.src1));
|
||||
print(" ", debug.string(obj.src2));
|
||||
print(" ", debug.string(obj.dest));
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
Adder.update = func () {
|
||||
var v1 = me.src1.getValue();
|
||||
var v2 = me.src2.getValue();
|
||||
if (!is_num(v1) or !is_num(v2)) return;
|
||||
me.dest.setValue(v1 + v2);
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Adds the delta of src to dest.
|
||||
# src - : property node
|
||||
# dest - : property node
|
||||
var DeltaAdder = {};
|
||||
DeltaAdder.new = func (src, dest) {
|
||||
var obj = { parents : [DeltaAdder],
|
||||
old : 0,
|
||||
src : src,
|
||||
dest : dest };
|
||||
if (obj.src == nil or obj.dest == nil) {
|
||||
print("DeltaAdder[", debug.string(obj.src), ", ",
|
||||
debug.string(obj.dest), "]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
DeltaAdder.update = func () {
|
||||
var v = me.src.getValue();
|
||||
if (!is_num(v)) return;
|
||||
me.dest.setValue((v - me.old) + me.dest.getValue());
|
||||
me.old = v;
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Switch encoder: Encodes upto 32 boolean properties in one
|
||||
# int property.
|
||||
# inputs - list of property nodes
|
||||
# dest - where the bitmask is stored : property node
|
||||
var SwitchEncoder = {};
|
||||
SwitchEncoder.new = func (inputs, dest) {
|
||||
var obj = { parents : [SwitchEncoder],
|
||||
inputs : inputs,
|
||||
dest : dest };
|
||||
# Error checking.
|
||||
var bad = (obj.dest == nil);
|
||||
foreach (var i; inputs) {
|
||||
if (i == nil) { bad = 1; }
|
||||
}
|
||||
|
||||
if (bad) {
|
||||
print("SwitchEncoder[");
|
||||
foreach (var i; inputs) {
|
||||
print(" ", debug.string(i));
|
||||
}
|
||||
print(" ", debug.string(obj.dest));
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
SwitchEncoder.update = func () {
|
||||
var v = 0;
|
||||
var b = 1;
|
||||
forindex (var i; me.inputs) {
|
||||
if (me.inputs[i].getBoolValue()) {
|
||||
v = v + b;
|
||||
}
|
||||
b *= 2;
|
||||
}
|
||||
me.dest.setIntValue(v);
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Switch decoder: Decodes a bitmask in an int property.
|
||||
# src - : property node
|
||||
# actions - list of actions : [func(b)]
|
||||
# Actions are triggered when their input bit change.
|
||||
# Due to interpolation the decoder needs to wait for a
|
||||
# stable input value.
|
||||
var SwitchDecoder = {};
|
||||
SwitchDecoder.new = func (src, actions) {
|
||||
var obj = { parents : [SwitchDecoder],
|
||||
wait : 0,
|
||||
old : 0,
|
||||
old_stable : 0,
|
||||
stable_since : 0,
|
||||
reset : 1,
|
||||
src : src,
|
||||
actions : actions,
|
||||
MIN_STABLE : 0.1 };
|
||||
# Error checking.
|
||||
var bad = (obj.src == nil);
|
||||
foreach (var a; obj.actions) {
|
||||
if (a == nil) { bad = 1; }
|
||||
}
|
||||
|
||||
if (bad) {
|
||||
print("SwitchDecoder[");
|
||||
print(" ", debug.string(obj.src));
|
||||
foreach (var a; obj.actions) {
|
||||
print(" ", debug.string(a));
|
||||
}
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
SwitchDecoder.update = func () {
|
||||
var t = getprop("/sim/time/elapsed-sec"); # NOTE: simulated time.
|
||||
var v = me.src.getValue();
|
||||
if (!is_num(v)) return;
|
||||
|
||||
if ((me.old == v) and ((t - me.stable_since) > me.MIN_STABLE) and
|
||||
(me.wait == 1)) {
|
||||
var ov = me.old_stable;
|
||||
# Use this to improve.
|
||||
#<cptf> here's the boring version: var bittest = func(u, b) { while (b) { u = int(u / 2); b -= 1; } u != int(u / 2) * 2; }
|
||||
forindex (var i; me.actions) {
|
||||
var m = math.mod(v, 2);
|
||||
var om = math.mod(ov, 2);
|
||||
if ((m != om or me.reset)) { me.actions[i](m?1:0); }
|
||||
v = (v - m)/2;
|
||||
ov = (ov - om)/2;
|
||||
}
|
||||
me.old_stable = me.src.getValue();
|
||||
me.wait = 0;
|
||||
me.reset = 0;
|
||||
} elsif (me.old == v) {
|
||||
# Wait. This is either before the bitmask is stable or after
|
||||
# it has been processed.
|
||||
} else {
|
||||
me.stable_since = t;
|
||||
me.wait = 1;
|
||||
me.old = me.src.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Time division multiplexing encoder: Transmits a list of
|
||||
# properties over a MP enabled string property.
|
||||
# inputs - input properties : [property node]
|
||||
# dest - MP string prop : property node
|
||||
# Note: TDM can have high latency so it is best used for
|
||||
# non-time critical properties.
|
||||
var TDMEncoder = {};
|
||||
TDMEncoder.new = func (inputs, dest) {
|
||||
var obj = { parents : [TDMEncoder],
|
||||
inputs : inputs,
|
||||
channel : MessageChannel.new(dest,
|
||||
func (msg) {
|
||||
print("This should not happen!");
|
||||
}),
|
||||
MIN_INT : 0.25,
|
||||
last_time : 0,
|
||||
next_item : 0,
|
||||
old : [] };
|
||||
# Error checking.
|
||||
var bad = (dest == nil) or (obj.channel == nil);
|
||||
foreach (var i; inputs) {
|
||||
if (i == nil) { bad = 1; }
|
||||
}
|
||||
|
||||
if (bad) {
|
||||
print("TDMEncoder[");
|
||||
foreach (var i; inputs) {
|
||||
print(" ", debug.string(i));
|
||||
}
|
||||
print(" ", debug.string(dest));
|
||||
print("]");
|
||||
}
|
||||
|
||||
setsize(obj.old, size(obj.inputs));
|
||||
|
||||
return obj;
|
||||
}
|
||||
TDMEncoder.update = func () {
|
||||
var t = getprop("/sim/time/elapsed-sec"); # NOTE: simulated time.
|
||||
if (t > me.last_time + me.MIN_INT) {
|
||||
var n = size(me.inputs);
|
||||
while (1) {
|
||||
var v = me.inputs[me.next_item].getValue();
|
||||
|
||||
if ((n <= 0) or (me.old[me.next_item] != v)) {
|
||||
# Set the MP properties to send the next item.
|
||||
me.channel.send(Binary.encodeByte(me.next_item) ~
|
||||
Binary.encodeDouble(v));
|
||||
|
||||
me.old[me.next_item] = v;
|
||||
|
||||
me.last_time = t;
|
||||
me.next_item += 1;
|
||||
if (me.next_item >= size(me.inputs)) { me.next_item = 0; }
|
||||
return;
|
||||
} else {
|
||||
# Search for changed property.
|
||||
n -= 1;
|
||||
me.next_item += 1;
|
||||
if (me.next_item >= size(me.inputs)) { me.next_item = 0; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
############################################################
|
||||
# Time division multiplexing decoder: Receives a list of
|
||||
# properties over a MP enabled string property.
|
||||
# src - MP string prop : property node
|
||||
# actions - list of actions : [func(v)]
|
||||
# An action is triggered when its value is received.
|
||||
# Note: TDM can have high latency so it is best used for
|
||||
# non-time critical properties.
|
||||
var TDMDecoder = {};
|
||||
TDMDecoder.new = func (src, actions) {
|
||||
var obj = { parents : [TDMDecoder],
|
||||
actions : actions };
|
||||
obj.channel = MessageChannel.new(src,
|
||||
func (msg) {
|
||||
obj.process(msg);
|
||||
});
|
||||
|
||||
# Error checking.
|
||||
var bad = (src == nil) or (obj.channel == nil);
|
||||
foreach (var a; actions) {
|
||||
if (a == nil) { bad = 1; }
|
||||
}
|
||||
|
||||
if (bad) {
|
||||
print("TDMDecoder[");
|
||||
print(" ", debug.string(src));
|
||||
foreach (var a; actions) {
|
||||
print(" ", debug.string(a));
|
||||
}
|
||||
print("]");
|
||||
fail();
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
TDMDecoder.process = func (msg) {
|
||||
var v1 = Binary.decodeByte(msg);
|
||||
var v2 = Binary.decodeDouble(substr(msg, 1));
|
||||
# Trigger action.
|
||||
me.actions[v1](v2);
|
||||
}
|
||||
TDMDecoder.update = func {
|
||||
me.channel.update();
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
# Internal utility functions
|
||||
|
||||
var is_num = func (v) {
|
||||
return num(v) != nil;
|
||||
}
|
||||
|
||||
# fail causes a Nasal runtime error so we get a backtrace.
|
||||
var fail = func {
|
||||
error_detected_in_calling_context();
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
|
||||
###############################################################################
|
||||
# Copilot selection dialog.
|
||||
#
|
||||
# Usage: dual_control_tools.copilot_dialog.show(<copilot type string>);
|
||||
#
|
||||
var COPILOT_DLG = 0;
|
||||
var copilot_dialog = {};
|
||||
############################################################
|
||||
copilot_dialog.init = func (copilot_type, x = nil, y = nil) {
|
||||
me.x = x;
|
||||
me.y = y;
|
||||
me.bg = [0, 0, 0, 0.3]; # background color
|
||||
me.fg = [[1.0, 1.0, 1.0, 1.0]];
|
||||
#
|
||||
# "private"
|
||||
if (contains(aircraft_dual_control, "copilot_view")) {
|
||||
me.title = "Pilot selection";
|
||||
} else {
|
||||
me.title = "Copilot selection";
|
||||
}
|
||||
me.basenode = props.globals.getNode("sim/remote", 1);
|
||||
me.dialog = nil;
|
||||
me.namenode = props.Node.new({"dialog-name" : me.title });
|
||||
me.listeners = [];
|
||||
me.copilot_type = copilot_type;
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog.create = func {
|
||||
if (me.dialog != nil)
|
||||
me.close();
|
||||
|
||||
me.dialog = gui.Widget.new();
|
||||
me.dialog.set("name", me.title);
|
||||
if (me.x != nil)
|
||||
me.dialog.set("x", me.x);
|
||||
if (me.y != nil)
|
||||
me.dialog.set("y", me.y);
|
||||
|
||||
me.dialog.set("layout", "vbox");
|
||||
me.dialog.set("default-padding", 0);
|
||||
var titlebar = me.dialog.addChild("group");
|
||||
titlebar.set("layout", "hbox");
|
||||
titlebar.addChild("empty").set("stretch", 1);
|
||||
if (contains(aircraft_dual_control, "copilot_view")) {
|
||||
titlebar.addChild("text").set("label", "Book your flight");
|
||||
} else {
|
||||
titlebar.addChild("text").set("label", "Passengers online");
|
||||
}
|
||||
var w = titlebar.addChild("button");
|
||||
w.set("pref-width", 16);
|
||||
w.set("pref-height", 16);
|
||||
w.set("legend", "");
|
||||
w.set("default", 0);
|
||||
w.set("key", "esc");
|
||||
w.setBinding("nasal", "dual_control_tools.copilot_dialog.destroy(); ");
|
||||
w.setBinding("dialog-close");
|
||||
me.dialog.addChild("hrule");
|
||||
|
||||
var content = me.dialog.addChild("group");
|
||||
content.set("layout", "vbox");
|
||||
content.set("halign", "center");
|
||||
content.set("default-padding", 5);
|
||||
|
||||
# Generate the dialog contents.
|
||||
me.players = me.find_copilot_players();
|
||||
var i = 0;
|
||||
var tmpbase = me.basenode.getNode("dialog", 1);
|
||||
var selected = me.basenode.getNode("pilot-callsign").getValue();
|
||||
foreach (var p; me.players) {
|
||||
var tmp = tmpbase.getNode("b[" ~ i ~ "]", 1);
|
||||
tmp.setBoolValue(streq(selected, p));
|
||||
var w = content.addChild("checkbox");
|
||||
w.node.setValues({"label" : p,
|
||||
"halign" : "left",
|
||||
"property" : tmp.getPath()});
|
||||
w.setBinding
|
||||
("nasal",
|
||||
"dual_control_tools.copilot_dialog.select_action(" ~ i ~ ");");
|
||||
i = i + 1;
|
||||
}
|
||||
me.dialog.addChild("hrule");
|
||||
|
||||
# Display the dialog.
|
||||
fgcommand("dialog-new", me.dialog.prop());
|
||||
fgcommand("dialog-show", me.namenode);
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog.close = func {
|
||||
fgcommand("dialog-close", me.namenode);
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog.destroy = func {
|
||||
COPILOT_DLG = 0;
|
||||
me.close();
|
||||
foreach(var l; me.listeners)
|
||||
removelistener(l);
|
||||
delete(gui.dialog, "\"" ~ me.title ~ "\"");
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog.show = func (copilot_type) {
|
||||
# print("Showing MPCopilots dialog!");
|
||||
if (!COPILOT_DLG) {
|
||||
COPILOT_DLG = int(getprop("/sim/time/elapsed-sec"));
|
||||
me.init(copilot_type);
|
||||
me.create();
|
||||
me._update_(COPILOT_DLG);
|
||||
}
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog._redraw_ = func {
|
||||
if (me.dialog != nil) {
|
||||
me.close();
|
||||
me.create();
|
||||
}
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog._update_ = func (id) {
|
||||
if (COPILOT_DLG != id) return;
|
||||
me._redraw_();
|
||||
settimer(func { me._update_(id); }, 4.1);
|
||||
}
|
||||
############################################################
|
||||
copilot_dialog.select_action = func (n) {
|
||||
var selected = me.basenode.getNode("pilot-callsign").getValue();
|
||||
var bs = me.basenode.getNode("dialog").getChildren();
|
||||
# Assumption: There are two true b:s or none. The one not matching selected
|
||||
# is the new selection.
|
||||
var i = 0;
|
||||
me.basenode.getNode("pilot-callsign").setValue("");
|
||||
foreach (var b; bs) {
|
||||
if (!b.getValue() and (i == n)) {
|
||||
b.setValue(1);
|
||||
me.basenode.getNode("pilot-callsign").setValue(me.players[i]);
|
||||
} else {
|
||||
b.setValue(0);
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
dual_control.main.reset();
|
||||
me._redraw_();
|
||||
}
|
||||
############################################################
|
||||
# Return a list containing all nearby copilot players of the right type.
|
||||
copilot_dialog.find_copilot_players = func {
|
||||
var mpplayers =
|
||||
props.globals.getNode("ai/models").getChildren("multiplayer");
|
||||
|
||||
var res = [];
|
||||
foreach (var pilot; mpplayers) {
|
||||
if ((pilot.getNode("valid") != nil) and
|
||||
(pilot.getNode("valid").getValue()) and
|
||||
(pilot.getNode("sim/model/path") != nil)) {
|
||||
var type = pilot.getNode("sim/model/path").getValue();
|
||||
|
||||
if (type == me.copilot_type) {
|
||||
append(res, pilot.getNode("callsign").getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
# debug.dump(res);
|
||||
return res;
|
||||
}
|
||||
###############################################################################
|
||||
@@ -1,6 +0,0 @@
|
||||
print("hello world!\n");
|
||||
print("This is the first program for nasal--\n");
|
||||
var cnt=0;
|
||||
for(var i=1;i<101;i+=1)
|
||||
cnt+=i;
|
||||
print(cnt);
|
||||
7564
test/fmz.nas
7564
test/fmz.nas
File diff suppressed because it is too large
Load Diff
@@ -1,10 +0,0 @@
|
||||
var f=func(n,m,dynamic...)
|
||||
{
|
||||
print(n+m," ",dynamic);
|
||||
n=dynamic;
|
||||
m=dynamic;
|
||||
n+=m;
|
||||
return dynamic;
|
||||
};
|
||||
print(f(1,1,0,0,0,0,0,(1+2+3+(1+2+3+4)))[3]);
|
||||
function([0,1,2,3],{str:"str"});
|
||||
@@ -1,4 +1,5 @@
|
||||
#lib json.nas
|
||||
import("lib.nas");
|
||||
var json={
|
||||
text:'',
|
||||
line:1,
|
||||
|
||||
32
test/leetcode1319.nas
Normal file
32
test/leetcode1319.nas
Normal file
@@ -0,0 +1,32 @@
|
||||
import("lib.nas");
|
||||
# union set
|
||||
var n=4;
|
||||
var input=[[0,1],[0,2],[1,2]];
|
||||
|
||||
var find_root=func(x,parent)
|
||||
{
|
||||
while(parent[x]!=nil)
|
||||
x=parent[x];
|
||||
return x;
|
||||
}
|
||||
var union_root=func(x,y,parent)
|
||||
{
|
||||
var x_root=find_root(x,parent);
|
||||
var y_root=find_root(y,parent);
|
||||
if(x_root==y_root) return 0;
|
||||
else parent[x_root]=y_root;
|
||||
return 1;
|
||||
}
|
||||
var makeConnect=func(n,connections)
|
||||
{
|
||||
if(size(connections)<n-1) return -1;
|
||||
var cnt=n-1;
|
||||
var parent=[];
|
||||
setsize(parent,n);
|
||||
foreach(var i;connections)
|
||||
if(union_root(i[0],i[1],parent))
|
||||
cnt-=1;
|
||||
return cnt;
|
||||
}
|
||||
|
||||
println(makeConnect(n,input));
|
||||
364
test/lexer.nas
364
test/lexer.nas
@@ -1,183 +1,191 @@
|
||||
import("lib.nas");
|
||||
|
||||
var s=io.fin("a.nas");
|
||||
s=split('',s);
|
||||
var len=size(s);
|
||||
var ptr=0;
|
||||
|
||||
var jump_note=func()
|
||||
var lexer=func(file)
|
||||
{
|
||||
while(ptr<len and s[ptr]!='\n')
|
||||
ptr+=1;
|
||||
ptr+=1;
|
||||
return;
|
||||
var _={s:io.fin(file),len:0,ptr:0,token:[]};
|
||||
_.len=size(_.s);
|
||||
return
|
||||
{
|
||||
jmp_note:func()
|
||||
{
|
||||
while(_.ptr<_.len and chr(_.s[_.ptr])!='\n')
|
||||
_.ptr+=1;
|
||||
_.ptr+=1;
|
||||
},
|
||||
id_gen:func()
|
||||
{
|
||||
var tmp="";
|
||||
while(_.ptr<_.len)
|
||||
{
|
||||
var c=_.s[_.ptr];
|
||||
if(('a'[0]<=c and c<='z'[0])
|
||||
or ('A'[0]<=c and c<='Z'[0])
|
||||
or ('0'[0]<=c and c<='9'[0])
|
||||
or c=='_'[0])
|
||||
tmp~=chr(c);
|
||||
else
|
||||
break;
|
||||
_.ptr+=1;
|
||||
}
|
||||
append(_.token,tmp);
|
||||
},
|
||||
str_gen:func()
|
||||
{
|
||||
var str="";
|
||||
var mark=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
while(_.ptr<_.len and chr(_.s[_.ptr])!=mark)
|
||||
{
|
||||
if(chr(_.s[_.ptr])=='\\')
|
||||
{
|
||||
_.ptr+=1;
|
||||
var c=chr(_.s[_.ptr]);
|
||||
if (c=='a' ) str~='\a';
|
||||
elsif(c=='b' ) str~='\b';
|
||||
elsif(c=='f' ) str~='\f';
|
||||
elsif(c=='n' ) str~='\n';
|
||||
elsif(c=='r' ) str~='\r';
|
||||
elsif(c=='t' ) str~='\t';
|
||||
elsif(c=='v' ) str~='\v';
|
||||
elsif(c=='?' ) str~='?';
|
||||
elsif(c=='0' ) str~='\0';
|
||||
elsif(c=='\\') str~='\\';
|
||||
elsif(c=='\'') str~='\'';
|
||||
elsif(c=='\"') str~='\"';
|
||||
else str~=c;
|
||||
}
|
||||
else
|
||||
str~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
if(_.ptr>=_.len)
|
||||
print("read eof when generating string.\n");
|
||||
_.ptr+=1;
|
||||
append(_.token,str);
|
||||
},
|
||||
num_gen:func()
|
||||
{
|
||||
var number=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
if(_.ptr<_.len and chr(_.s[_.ptr])=='x')
|
||||
{
|
||||
_.ptr+=1;
|
||||
while(_.ptr<_.len and
|
||||
('a'[0]<=_.s[_.ptr] and _.s[_.ptr]<='f'[0]
|
||||
or '0'[0]<=_.s[_.ptr] and _.s[_.ptr]<='9'[0]))
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
append(_.token,num(number));
|
||||
return;
|
||||
}
|
||||
elsif(_.ptr<_.len and chr(_.s[_.ptr])=='o')
|
||||
{
|
||||
_.ptr+=1;
|
||||
while(_.ptr<_.len and ('0'[0]<=_.s[_.ptr] and _.s[_.ptr]<='7'[0]))
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
append(_.token,num(number));
|
||||
return;
|
||||
}
|
||||
while(_.ptr<_.len and ('0'[0]<=_.s[_.ptr] and _.s[_.ptr]<='9'[0]))
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
if(_.ptr<_.len and chr(_.s[_.ptr])=='.')
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
while(_.ptr<_.len and ('0'[0]<=_.s[_.ptr] and _.s[_.ptr]<='9'[0]))
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
}
|
||||
if(chr(_.s[_.ptr])=='e' or chr(_.s[_.ptr])=='E')
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
if(chr(_.s[_.ptr])=='-' or chr(_.s[_.ptr])=='+')
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
while(_.ptr<_.len and ('0'[0]<=_.s[_.ptr] and _.s[_.ptr]<='9'[0]))
|
||||
{
|
||||
number~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
}
|
||||
var last_c=chr(number[-1]);
|
||||
if(last_c=='.' or last_c=='e' or last_c=='E' or last_c=='-' or last_c=='+')
|
||||
println("error number: ",number);
|
||||
append(_.token,num(number));
|
||||
},
|
||||
opr_gen:func()
|
||||
{
|
||||
var c=chr(_.s[_.ptr]);
|
||||
if(c=='+' or c=='-' or c=='~' or c=='/' or c=='*' or c=='>' or c=='<' or c=='!' or c=='=')
|
||||
{
|
||||
var tmp=c;
|
||||
_.ptr+=1;
|
||||
if(_.ptr<_.len and chr(_.s[_.ptr])=='=')
|
||||
{
|
||||
tmp~=chr(_.s[_.ptr]);
|
||||
_.ptr+=1;
|
||||
}
|
||||
append(_.token,tmp);
|
||||
return;
|
||||
}
|
||||
elsif(c=='.')
|
||||
{
|
||||
if(_.ptr+2<_.len and chr(_.s[_.ptr+1])=='.' and chr(_.s[_.ptr+2])=='.')
|
||||
{
|
||||
append(_.token,"...");
|
||||
_.ptr+=3;
|
||||
}
|
||||
else
|
||||
{
|
||||
append(_.token,".");
|
||||
_.ptr+=1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
elsif(c!=' ' and c!='\t' and c!='\n' and c!='\r' and _.s[_.ptr]>0)
|
||||
append(_.token,c);
|
||||
_.ptr+=1;
|
||||
return;
|
||||
},
|
||||
main:func()
|
||||
{
|
||||
while(_.ptr<_.len)
|
||||
{
|
||||
var c=_.s[_.ptr];
|
||||
if(c=='#'[0])
|
||||
me.jmp_note();
|
||||
elsif('a'[0]<=c and c<='z'[0]
|
||||
or 'A'[0]<=c and c<='Z'[0]
|
||||
or c=='_'[0])
|
||||
me.id_gen();
|
||||
elsif(c=='\''[0] or c=='\"'[0])
|
||||
me.str_gen();
|
||||
elsif('0'[0]<=c and c<='9'[0])
|
||||
me.num_gen();
|
||||
else
|
||||
me.opr_gen();
|
||||
}
|
||||
return;
|
||||
},
|
||||
get_token:func(){return _.token;}
|
||||
};
|
||||
}
|
||||
|
||||
var generate_id=func()
|
||||
{
|
||||
var tmp="";
|
||||
while(ptr<len)
|
||||
{
|
||||
if('a'<=s[ptr] and s[ptr]<='z'
|
||||
or 'A'<=s[ptr] and s[ptr]<='Z'
|
||||
or s[ptr]=='_'
|
||||
or '0'<=s[ptr] and s[ptr]<='9')
|
||||
tmp~=s[ptr];
|
||||
else
|
||||
break;
|
||||
ptr+=1;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
var generate_str=func()
|
||||
{
|
||||
var tok_str="";
|
||||
var mark=s[ptr];
|
||||
ptr+=1;
|
||||
while(ptr<len and s[ptr]!=mark)
|
||||
{
|
||||
if(s[ptr]=='\\')
|
||||
{
|
||||
ptr+=1;
|
||||
if(s[ptr]=='a') tok_str~='\a';
|
||||
elsif(s[ptr]=='b') tok_str~='\b';
|
||||
elsif(s[ptr]=='f') tok_str~='\f';
|
||||
elsif(s[ptr]=='n') tok_str~='\n';
|
||||
elsif(s[ptr]=='r') tok_str~='\r';
|
||||
elsif(s[ptr]=='t') tok_str~='\t';
|
||||
elsif(s[ptr]=='v') tok_str~='\v';
|
||||
elsif(s[ptr]=='?') tok_str~='?';
|
||||
elsif(s[ptr]=='0') tok_str~='\0';
|
||||
elsif(s[ptr]=='\\') tok_str~='\\';
|
||||
elsif(s[ptr]=='\'') tok_str~='\'';
|
||||
elsif(s[ptr]=='\"') tok_str~='\"';
|
||||
else tok_str~=s[ptr];
|
||||
}
|
||||
else
|
||||
tok_str~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
if(ptr>=len)
|
||||
print("read eof when generating string.");
|
||||
ptr+=1;
|
||||
return tok_str;
|
||||
}
|
||||
|
||||
var generate_number=func()
|
||||
{
|
||||
var number=s[ptr];
|
||||
ptr+=1;
|
||||
if(s[ptr]=='x')
|
||||
{
|
||||
ptr+=1;
|
||||
while(ptr<len and ('a'<=s[ptr] and s[ptr]<='f' or '0'<=s[ptr] and s[ptr]<='9'))
|
||||
{
|
||||
number~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
return num(number);
|
||||
}
|
||||
elsif(s[ptr]=='o')
|
||||
{
|
||||
ptr+=1;
|
||||
while(ptr<len and ('0'<=s[ptr] and s[ptr]<='7'))
|
||||
{
|
||||
number~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
return num(number);
|
||||
}
|
||||
|
||||
while(ptr<len and ('0'<=s[ptr] and s[ptr]<='9'))
|
||||
{
|
||||
number~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
if(s[ptr]=='.')
|
||||
number~=s[ptr];
|
||||
else
|
||||
return num(number);
|
||||
ptr+=1;
|
||||
while(ptr<len and ('0'<=s[ptr] and s[ptr]<='9'))
|
||||
{
|
||||
number~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
if(s[ptr]=='e' or s[ptr]=='E')
|
||||
number~=s[ptr];
|
||||
else
|
||||
return num(number);
|
||||
ptr+=1;
|
||||
if(s[ptr]=='-' or s[ptr]=='+')
|
||||
{
|
||||
number~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
while(ptr<len and ('0'<=s[ptr] and s[ptr]<='9'))
|
||||
{
|
||||
number~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
return num(number);
|
||||
}
|
||||
|
||||
var generate_operator=func()
|
||||
{
|
||||
var tmp="";
|
||||
if(s[ptr]=='+' or s[ptr]=='-' or s[ptr]=='~' or s[ptr]=='/' or s[ptr]=='*' or s[ptr]=='>' or s[ptr]=='<' or s[ptr]=='!' or s[ptr]=='=')
|
||||
{
|
||||
tmp=s[ptr];
|
||||
ptr+=1;
|
||||
if(ptr<len and s[ptr]=='=')
|
||||
{
|
||||
tmp~=s[ptr];
|
||||
ptr+=1;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
elsif(s[ptr]=='.')
|
||||
{
|
||||
if(ptr+2<len and s[ptr+1]=='.' and s[ptr+2]=='.')
|
||||
{
|
||||
tmp='...';
|
||||
ptr+=3;
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp='.';
|
||||
ptr+=1;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
elsif(s[ptr]!=' ' and s[ptr]!='\t' and s[ptr]!='\n' and s[ptr]!='\r' and s[ptr][0]>0)
|
||||
tmp=s[ptr];
|
||||
ptr+=1;
|
||||
return tmp;
|
||||
}
|
||||
var cnt=0;
|
||||
var token=[];
|
||||
while(ptr<len)
|
||||
{
|
||||
if(s[ptr]=='#')
|
||||
jump_note();
|
||||
elsif('a'<=s[ptr] and s[ptr]<='z' or 'A'<=s[ptr] and s[ptr]<='Z' or s[ptr]=='_')
|
||||
append(token,generate_id());
|
||||
elsif(s[ptr]=='\'' or s[ptr]=='\"')
|
||||
append(token,generate_str());
|
||||
elsif('0'<=s[ptr] and s[ptr]<='9')
|
||||
append(token,generate_number());
|
||||
else
|
||||
{
|
||||
var tmp=generate_operator();
|
||||
if(size(tmp))
|
||||
append(token,tmp);
|
||||
}
|
||||
if(ptr>=len)
|
||||
break;
|
||||
}
|
||||
foreach(var i;token)
|
||||
{
|
||||
print("(",cnt," | ",i,")");
|
||||
cnt+=1;
|
||||
}
|
||||
var nasal_lexer=lexer("test/lexer.nas");
|
||||
nasal_lexer.main();
|
||||
foreach(var tok;nasal_lexer.get_token())
|
||||
print(tok,' ');
|
||||
println();
|
||||
167
test/lib.nas
167
test/lib.nas
@@ -1,167 +0,0 @@
|
||||
var import=func(filename)
|
||||
{
|
||||
nasal_call_import(filename);
|
||||
return nil;
|
||||
}
|
||||
var print=func(elements...)
|
||||
{
|
||||
nasal_call_builtin_std_cout(elements);
|
||||
return nil;
|
||||
};
|
||||
var append=func(vector,elements...)
|
||||
{
|
||||
nasal_call_builtin_push_back(vector,elements);
|
||||
return nil;
|
||||
}
|
||||
var setsize=func(vector,size)
|
||||
{
|
||||
nasal_call_builtin_set_size(vector,size);
|
||||
return nil;
|
||||
}
|
||||
var system=func(str)
|
||||
{
|
||||
nasal_call_builtin_system(str);
|
||||
return;
|
||||
}
|
||||
var input=func()
|
||||
{
|
||||
return nasal_call_builtin_input();
|
||||
}
|
||||
var sleep=func(duration)
|
||||
{
|
||||
nasal_call_builtin_sleep(duration);
|
||||
return;
|
||||
}
|
||||
var split=func(delimeter,string)
|
||||
{
|
||||
return nasal_call_builtin_split(delimeter,string);
|
||||
}
|
||||
var rand=func(seed=nil)
|
||||
{
|
||||
return nasal_call_builtin_rand(seed);
|
||||
}
|
||||
var id=func(thing)
|
||||
{
|
||||
return nasal_call_builtin_get_id(thing);
|
||||
}
|
||||
var int=func(value)
|
||||
{
|
||||
return nasal_call_builtin_trans_int(value);
|
||||
}
|
||||
var num=func(value)
|
||||
{
|
||||
return nasal_call_builtin_trans_num(value);
|
||||
}
|
||||
var pop=func(vector)
|
||||
{
|
||||
return nasal_call_builtin_pop_back(vector);
|
||||
}
|
||||
var str=func(number)
|
||||
{
|
||||
return nasal_call_builtin_trans_str(number);
|
||||
}
|
||||
var size=func(object)
|
||||
{
|
||||
return nasal_call_builtin_size(object);
|
||||
}
|
||||
var contains=func(hash,key)
|
||||
{
|
||||
return nasal_call_builtin_contains(hash,key);
|
||||
}
|
||||
var delete=func(hash,key)
|
||||
{
|
||||
nasal_call_builtin_delete(hash,key);
|
||||
return;
|
||||
}
|
||||
var keys=func(hash)
|
||||
{
|
||||
return nasal_call_builtin_get_keys(hash);
|
||||
}
|
||||
var time=func(begin_time)
|
||||
{
|
||||
return nasal_call_builtin_time(begin_time);
|
||||
}
|
||||
var die=func(str)
|
||||
{
|
||||
nasal_call_builtin_die(str);
|
||||
return nil;
|
||||
}
|
||||
var typeof=func(object)
|
||||
{
|
||||
return nasal_call_builtin_type(object);
|
||||
}
|
||||
var substr=func(str,begin,length)
|
||||
{
|
||||
return nasal_call_builtin_substr(str,begin,length);
|
||||
}
|
||||
|
||||
var io=
|
||||
{
|
||||
fin:func(filename)
|
||||
{
|
||||
return nasal_call_builtin_finput(filename);
|
||||
},
|
||||
fout:func(filename,str)
|
||||
{
|
||||
nasal_call_builtin_foutput(filename,str);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
var bits=
|
||||
{
|
||||
bitxor:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_xor(a,b);
|
||||
},
|
||||
bitand:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_and(a,b);
|
||||
},
|
||||
bitor:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_or(a,b);
|
||||
},
|
||||
bitnand:func(a,b)
|
||||
{
|
||||
return nasal_call_builtin_nand(a,b);
|
||||
},
|
||||
bitnot:func(a)
|
||||
{
|
||||
return nasal_call_builtin_not(a);
|
||||
}
|
||||
};
|
||||
|
||||
var math=
|
||||
{
|
||||
e:2.7182818284590452354,
|
||||
pi:3.14159265358979323846,
|
||||
sin:func(x)
|
||||
{
|
||||
return nasal_call_builtin_sin(x);
|
||||
},
|
||||
cos:func(x)
|
||||
{
|
||||
return nasal_call_builtin_cos(x);
|
||||
},
|
||||
tan:func(x)
|
||||
{
|
||||
return nasal_call_builtin_tan(x);
|
||||
},
|
||||
exp:func(x)
|
||||
{
|
||||
return nasal_call_builtin_exp(x);
|
||||
},
|
||||
ln:func(x)
|
||||
{
|
||||
return nasal_call_builtin_cpp_math_ln(x);
|
||||
},
|
||||
sqrt:func(x)
|
||||
{
|
||||
return nasal_call_builtin_cpp_math_sqrt(x);
|
||||
},
|
||||
atan2:func(x,y)
|
||||
{
|
||||
return nasal_call_builtin_cpp_atan2(x,y);
|
||||
},
|
||||
};
|
||||
62
test/life.nas
Normal file
62
test/life.nas
Normal file
@@ -0,0 +1,62 @@
|
||||
import("lib.nas");
|
||||
|
||||
var map=nil;
|
||||
|
||||
var check=func(x,y)
|
||||
{
|
||||
if(x>14) x=0;
|
||||
if(y>19) y=0;
|
||||
return map[x][y];
|
||||
}
|
||||
|
||||
var new_map=func()
|
||||
{
|
||||
var tmp=[];
|
||||
setsize(tmp,15);
|
||||
forindex(var i;tmp)
|
||||
{
|
||||
tmp[i]=[];
|
||||
setsize(tmp[i],20);
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
var prt=func()
|
||||
{
|
||||
var s='';
|
||||
foreach(var line;map)
|
||||
{
|
||||
foreach(var elem;line)
|
||||
s~=elem~' ';
|
||||
s~='\n';
|
||||
}
|
||||
system("cls");
|
||||
print(s);
|
||||
}
|
||||
|
||||
func()
|
||||
{
|
||||
rand(time(0));
|
||||
map=new_map();
|
||||
forindex(var i;map)
|
||||
forindex(var j;map[i])
|
||||
map[i][j]=rand()>0.7?'O':' ';
|
||||
var calc=[[0,1],[1,0],[0,-1],[-1,0],[1,1],[1,-1],[-1,-1],[-1,1]];
|
||||
for(var r=0;r<100;r+=1)
|
||||
{
|
||||
prt(map);
|
||||
var tmp=new_map();
|
||||
for(var i=0;i<15;i+=1)
|
||||
for(var j=0;j<20;j+=1)
|
||||
{
|
||||
var cnt=0;
|
||||
foreach(var k;calc)
|
||||
cnt+=(check(i+k[0],j+k[1])=='O');
|
||||
if(cnt==2) tmp[i][j]=map[i][j];
|
||||
elsif(cnt==3) tmp[i][j]='O';
|
||||
else tmp[i][j]=' ';
|
||||
}
|
||||
map=tmp;
|
||||
}
|
||||
return;
|
||||
}();
|
||||
@@ -1,3 +1,4 @@
|
||||
import("lib.nas");
|
||||
for(;;)break;
|
||||
for(;;)
|
||||
{
|
||||
|
||||
382
test/main.nas
382
test/main.nas
@@ -1,382 +0,0 @@
|
||||
# game left in corner by ValKmjolnir
|
||||
# 2020
|
||||
|
||||
# lib function defined here
|
||||
var print=func(elements...)
|
||||
{
|
||||
nasal_call_builtin_std_cout(elements);
|
||||
return nil;
|
||||
};
|
||||
var append=func(vector,elements...)
|
||||
{
|
||||
nasal_call_builtin_push_back(vector,elements);
|
||||
return nil;
|
||||
}
|
||||
var setsize=func(vector,size)
|
||||
{
|
||||
nasal_call_builtin_set_size(vector,size);
|
||||
return nil;
|
||||
}
|
||||
var system=func(str)
|
||||
{
|
||||
nasal_call_builtin_system(str);
|
||||
return;
|
||||
}
|
||||
var sleep=func(duration)
|
||||
{
|
||||
nasal_call_builtin_sleep(duration);
|
||||
return;
|
||||
}
|
||||
var input=func()
|
||||
{
|
||||
return nasal_call_builtin_input();
|
||||
}
|
||||
var io=
|
||||
{
|
||||
fin:func(filename)
|
||||
{
|
||||
return nasal_call_builtin_finput(filename);
|
||||
},
|
||||
fout:func(filename,str)
|
||||
{
|
||||
nasal_call_builtin_foutput(filename,str);
|
||||
return;
|
||||
}
|
||||
};
|
||||
var int=func(str)
|
||||
{
|
||||
return str+0;
|
||||
}
|
||||
var str=func(num)
|
||||
{
|
||||
return num~'';
|
||||
}
|
||||
# string split
|
||||
|
||||
# game elements defined here
|
||||
var role_property=
|
||||
{
|
||||
health:100,
|
||||
mood:100,
|
||||
satiety:100,
|
||||
thirst:100,
|
||||
health_change:func(x)
|
||||
{
|
||||
me.health+=x;
|
||||
if(me.health<0)
|
||||
me.health=0;
|
||||
elsif(me.health>100)
|
||||
me.health=100;
|
||||
return nil;
|
||||
},
|
||||
mood_change:func(x)
|
||||
{
|
||||
me.mood+=x;
|
||||
if(me.mood<0)
|
||||
me.mood=0;
|
||||
elsif(me.mood>100)
|
||||
me.mood=100;
|
||||
return nil;
|
||||
},
|
||||
satiety_change:func(x)
|
||||
{
|
||||
me.satiety+=x;
|
||||
if(me.satiety<0)
|
||||
me.satiety=0;
|
||||
elsif(me.satiety>100)
|
||||
me.satiety=100;
|
||||
return nil;
|
||||
},
|
||||
thirst_change:func(x)
|
||||
{
|
||||
me.thirst+=x;
|
||||
if(me.thirst<0)
|
||||
me.thirst=0;
|
||||
elsif(me.thirst>100)
|
||||
me.thirst=100;
|
||||
return nil;
|
||||
}
|
||||
};
|
||||
var screen=
|
||||
{
|
||||
picture:[],
|
||||
info_below_left:[],
|
||||
info_below_right:[],
|
||||
clear:func()
|
||||
{
|
||||
me.picture=[];
|
||||
me.info_below_left=[];
|
||||
me.info_below_right=[];
|
||||
return;
|
||||
},
|
||||
pic_addline:func(_str)
|
||||
{
|
||||
append(me.picture,_str);
|
||||
return;
|
||||
},
|
||||
left_add:func(_str)
|
||||
{
|
||||
append(me.info_below_left,_str);
|
||||
return;
|
||||
},
|
||||
right_add:func(_str)
|
||||
{
|
||||
append(me.info_below_right,_str);
|
||||
return;
|
||||
},
|
||||
prt_screen:func()
|
||||
{
|
||||
foreach(var i;me.picture)
|
||||
print(i);
|
||||
forindex(var i;me.info_below_left)
|
||||
print(me.info_below_left[i]~me.info_below_right[i]);
|
||||
return;
|
||||
}
|
||||
};
|
||||
var first_shown_info=func()
|
||||
{
|
||||
system("cls");
|
||||
var str_list=[
|
||||
"+-----------------------------------------------+",
|
||||
"| |",
|
||||
"| |",
|
||||
"| Let me tell you a story... |",
|
||||
"| A story that really happened many years ago...|",
|
||||
"| Nearly no one knows and cares about it... |",
|
||||
"| But some children may still suffer from... |",
|
||||
"| This kind of stories... |",
|
||||
"| And this kind of stories never stop hurting...|",
|
||||
"| People that are still alive... |",
|
||||
"| |",
|
||||
"| |",
|
||||
"+-----------------------------------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
print(i);
|
||||
return;
|
||||
}
|
||||
var generate_role_property=func()
|
||||
{
|
||||
screen.left_add("+-----------------------+");
|
||||
var str="";
|
||||
for(var i=10;i<=100;i+=10)
|
||||
{
|
||||
if(i<=role_property.health)
|
||||
str~="=";
|
||||
else
|
||||
str~=" ";
|
||||
}
|
||||
screen.left_add("|[health ]:"~str~" |");
|
||||
str="";
|
||||
for(var i=10;i<=100;i+=10)
|
||||
{
|
||||
if(i<=role_property.mood)
|
||||
str~="=";
|
||||
else
|
||||
str~=" ";
|
||||
}
|
||||
screen.left_add("|[mood ]:"~str~" |");
|
||||
str="";
|
||||
for(var i=10;i<=100;i+=10)
|
||||
{
|
||||
if(i<=role_property.satiety)
|
||||
str~="=";
|
||||
else
|
||||
str~=" ";
|
||||
}
|
||||
screen.left_add("|[satiety]:"~str~" |");
|
||||
str="";
|
||||
for(var i=10;i<=100;i+=10)
|
||||
{
|
||||
if(i<=role_property.thirst)
|
||||
str~="=";
|
||||
else
|
||||
str~=" ";
|
||||
}
|
||||
screen.left_add("|[thirst ]:"~str~" |");
|
||||
screen.left_add("+-----------------------+");
|
||||
return;
|
||||
}
|
||||
var generate_choose_list=func()
|
||||
{
|
||||
var str_list=[
|
||||
"-----------------------+",
|
||||
"[1]| next step |",
|
||||
"[2]| restart |",
|
||||
"[3]| store game |",
|
||||
"[4]| exit |",
|
||||
"-----------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
screen.right_add(i);
|
||||
return;
|
||||
}
|
||||
var next_step=func()
|
||||
{
|
||||
role_property.health_change(-1);
|
||||
role_property.mood_change(-1);
|
||||
role_property.satiety_change(-1);
|
||||
role_property.thirst_change(-10);
|
||||
var str_list=[
|
||||
"+-----------------------------------------------+",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"+-----------------------------------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
screen.pic_addline(i);
|
||||
return;
|
||||
}
|
||||
var restart=func()
|
||||
{
|
||||
role_property.health=100;
|
||||
role_property.mood=100;
|
||||
role_property.satiety=100;
|
||||
role_property.thirst=100;
|
||||
var str_list=[
|
||||
"+-----------------------------------------------+",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"+-----------------------------------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
screen.pic_addline(i);
|
||||
return;
|
||||
}
|
||||
var generate_incorrect_choice_screen=func()
|
||||
{
|
||||
var str_list=[
|
||||
"+-----------------------------------------------+",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| make a correct choice. |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"+-----------------------------------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
screen.pic_addline(i);
|
||||
}
|
||||
var generate_goodbye_screen=func()
|
||||
{
|
||||
var str_list=[
|
||||
"+-----------------------------------------------+",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| see you next life. |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"+-----------------------------------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
screen.pic_addline(i);
|
||||
}
|
||||
var store_file=func()
|
||||
{
|
||||
var str=role_property.health~'\n'~role_property.mood~'\n'~role_property.satiety~'\n'~role_property.thirst~'\n';
|
||||
io.fout("game-left-in-corner.glic",str);
|
||||
var str_list=[
|
||||
"+-----------------------------------------------+",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| data stored. |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"| |",
|
||||
"+-----------------------------------------------+"
|
||||
];
|
||||
foreach(var i;str_list)
|
||||
screen.pic_addline(i);
|
||||
return;
|
||||
}
|
||||
var get_file=func()
|
||||
{
|
||||
var str=io.fin("game-left-in-corner.glic");
|
||||
print(str);
|
||||
return;
|
||||
}
|
||||
var game_main=func()
|
||||
{
|
||||
first_shown_info();
|
||||
screen.clear();
|
||||
generate_role_property();
|
||||
generate_choose_list();
|
||||
screen.prt_screen();
|
||||
while(1)
|
||||
{
|
||||
screen.clear();
|
||||
print("|your choice[1|2|3|4]: |");
|
||||
var choice=input();
|
||||
if((choice!='1') and (choice!='2') and (choice!='3') and (choice!='4'))
|
||||
generate_incorrect_choice_screen();
|
||||
elsif(choice=='1')
|
||||
next_step();
|
||||
elsif(choice=='2')
|
||||
restart();
|
||||
elsif(choice=='3')
|
||||
store_file();
|
||||
elsif(choice=='4')
|
||||
{
|
||||
system("cls");
|
||||
screen.clear();
|
||||
generate_goodbye_screen();
|
||||
generate_role_property();
|
||||
generate_choose_list();
|
||||
screen.prt_screen();
|
||||
break;
|
||||
}
|
||||
system("cls");
|
||||
generate_role_property();
|
||||
generate_choose_list();
|
||||
screen.prt_screen();
|
||||
if(role_property.health==0 or role_property.mood==0 or role_property.satiety==0 or role_property.thirst==0)
|
||||
{
|
||||
print("|you died. |");
|
||||
print("+-----------------------------------------------+");
|
||||
system("pause");
|
||||
screen.clear();
|
||||
restart();
|
||||
system("cls");
|
||||
generate_role_property();
|
||||
generate_choose_list();
|
||||
screen.prt_screen();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
game_main();
|
||||
27
test/mandelbrot.nas
Normal file
27
test/mandelbrot.nas
Normal file
@@ -0,0 +1,27 @@
|
||||
import("lib.nas");
|
||||
var (yMin,yMax,xMin,xMax,line)=(-0.2,0.2,-1.5,-1.0,"");
|
||||
var (yDel,xDel)=(yMax-yMin,xMax-xMin);
|
||||
for(var yPixel=0;yPixel<24;yPixel+=1)
|
||||
{
|
||||
var y=(yPixel/24)*yDel+yMin;
|
||||
for(var xPixel=0;xPixel<80;xPixel+=1)
|
||||
{
|
||||
var x=(xPixel/80)*xDel+xMin;
|
||||
var pixel=" ";
|
||||
var (x0,y0)=(x,y);
|
||||
for(var iter=0;iter<80;iter+=1)
|
||||
{
|
||||
var x1=(x0*x0)-(y0*y0)+x;
|
||||
var y1=2*x0*y0+y;
|
||||
(x0,y0)=(x1,y1);
|
||||
if((x0*x0)+(y0*y0)>4)
|
||||
{
|
||||
pixel=chr(" .:;+=xX$&"[iter/8]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
line~=pixel;
|
||||
}
|
||||
line~='\n';
|
||||
}
|
||||
print(line);
|
||||
@@ -1,10 +1,10 @@
|
||||
#//This is written for Nasal Intepreter
|
||||
#//Sidi Liang
|
||||
|
||||
# This is written for Nasal Intepreter
|
||||
# Sidi Liang
|
||||
import("lib.nas");
|
||||
var w = 1;
|
||||
var x = "hello";
|
||||
var f = func(){
|
||||
print("f is called");
|
||||
println("f is called");
|
||||
}
|
||||
var f2 = func(){
|
||||
return 2;
|
||||
@@ -28,71 +28,27 @@ var z1 = {
|
||||
hashh:z
|
||||
};
|
||||
var y2 = [w, x, y, z1];
|
||||
|
||||
var z2 = {
|
||||
hashh: z1,
|
||||
listt2: y2,
|
||||
};
|
||||
|
||||
print(w);#//1
|
||||
print("\n");
|
||||
print(x);#//hello
|
||||
print("\n");
|
||||
print(y);#//Empty
|
||||
print("\n");
|
||||
print(z);#//Empty
|
||||
print("\n");
|
||||
print(z1);#//Empty
|
||||
print("\n");
|
||||
print(y2);#//Empty
|
||||
print("\n");
|
||||
print(y[0]);#//1
|
||||
print("\n");
|
||||
print(y1[2][1]);#//hello
|
||||
print("\n");
|
||||
print(z.numb);#//1
|
||||
print("\n");
|
||||
print(z.listt[2][1]);#//hello
|
||||
print("\n");
|
||||
print(z1.hashh.listt[2][1]);#//hello
|
||||
print("\n");
|
||||
print(y2[3].hashh.listt[2][1]);#//hello
|
||||
print("\n");
|
||||
print(f);#//Empty
|
||||
print("\n");
|
||||
println(w);#//1
|
||||
println(x);#//hello
|
||||
println(y);#//[1,hello]
|
||||
println(z);#//{...}
|
||||
println(z1);#//{...}
|
||||
println(y2);#//[...]
|
||||
println(y[0]);#//1
|
||||
println(y1[2][1]);#//hello
|
||||
println(z.numb);#//1
|
||||
println(z.listt[2][1]);#//hello
|
||||
println(z1.hashh.listt[2][1]);#//hello
|
||||
println(y2[3].hashh.listt[2][1]);#//hello
|
||||
println(f);#//func(...){...}
|
||||
f();#//f is called
|
||||
print("\n");
|
||||
print(z.funcc);#//Empty
|
||||
print("\n");
|
||||
println(z.funcc);#//func(...){...}
|
||||
z.funcc();#//f is called
|
||||
print("\n");
|
||||
print(z.funcccall);#//Empty
|
||||
print("\n");
|
||||
z.funcccall();#//f is called
|
||||
print("\n");
|
||||
println(z.funcccall);#//nil
|
||||
z2.listt2[3].hashh.funcc();#//f is called
|
||||
print("\n");
|
||||
print(y1[f2()][w]);#//hello
|
||||
print("\n");
|
||||
#//print(z.f3()); Error
|
||||
call(f);#//f is called
|
||||
print("\n");
|
||||
call(z.funcc);#//f is called
|
||||
print("\n");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
println(y1[f2()][w]);#//hello
|
||||
156
test/neo4j.nas
156
test/neo4j.nas
@@ -1,156 +0,0 @@
|
||||
import("lib.nas");
|
||||
|
||||
rand(time(0));
|
||||
var chartable=split('','abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
|
||||
var node=func(type)
|
||||
{
|
||||
var s="";
|
||||
for(var i=0;i<10;i+=1)
|
||||
s~=chartable[rand()*62];
|
||||
return {name:s,type:type,next:[]};
|
||||
}
|
||||
var film_node=[];
|
||||
for(var i=0;i<1000;i+=1)
|
||||
append(film_node,node("film"));
|
||||
var director_node=[];
|
||||
for(var i=0;i<400;i+=1)
|
||||
append(director_node,node("direct"));
|
||||
var actor_node=[];
|
||||
for(var i=0;i<2000;i+=1)
|
||||
append(actor_node,node("actor"));
|
||||
var writer_node=[];
|
||||
for(var i=0;i<300;i+=1)
|
||||
append(writer_node,node("writer"));
|
||||
var type_node=[];
|
||||
for(var i=0;i<20;i+=1)
|
||||
append(type_node,node("type"));
|
||||
var lang_node=[];
|
||||
for(var i=0;i<120;i+=1)
|
||||
append(lang_node,node("lang"));
|
||||
var country_node=[];
|
||||
for(var i=0;i<120;i+=1)
|
||||
append(country_node,node("country"));
|
||||
|
||||
func()
|
||||
{
|
||||
var director_size=size(director_node);
|
||||
var actor_size=size(actor_node);
|
||||
var writer_size=size(writer_node);
|
||||
var type_size=size(type_node);
|
||||
var lang_size=size(lang_node);
|
||||
var country_size=size(country_node);
|
||||
|
||||
var director_link=int(1+rand()*2);
|
||||
var actor_link=int(1+rand()*10);
|
||||
var writer_link=int(1+rand());
|
||||
var type_link=int(1+rand()*3);
|
||||
var lang_link=int(1+rand()*4);
|
||||
var country_link=int(1+rand()*2);
|
||||
foreach(var film;film_node)
|
||||
{
|
||||
for(var i=0;i<director_link;i+=1)
|
||||
{
|
||||
var director=director_node[rand()*director_size];
|
||||
append(film.next,director);
|
||||
append(director.next,film);
|
||||
}
|
||||
for(var i=0;i<actor_link;i+=1)
|
||||
{
|
||||
var actor=actor_node[rand()*actor_size];
|
||||
append(film.next,actor);
|
||||
append(actor.next,film);
|
||||
}
|
||||
for(var i=0;i<writer_link;i+=1)
|
||||
{
|
||||
var writer=writer_node[rand()*writer_size];
|
||||
append(film.next,writer);
|
||||
append(writer.next,film);
|
||||
}
|
||||
for(var i=0;i<type_link;i+=1)
|
||||
{
|
||||
var _type=type_node[rand()*type_size];
|
||||
append(film.next,_type);
|
||||
append(_type.next,film);
|
||||
}
|
||||
for(var i=0;i<lang_link;i+=1)
|
||||
{
|
||||
var lang=lang_node[rand()*lang_size];
|
||||
append(film.next,lang);
|
||||
append(lang.next,film);
|
||||
}
|
||||
for(var i=0;i<country_link;i+=1)
|
||||
{
|
||||
var country=country_node[rand()*country_size];
|
||||
append(film.next,country);
|
||||
append(country.next,film);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}();
|
||||
|
||||
|
||||
var film_list=[];
|
||||
var count_list=[];
|
||||
for(var i=0;i<10;i+=1)
|
||||
{
|
||||
append(film_list,film_node[i]);
|
||||
append(count_list,1);
|
||||
}
|
||||
|
||||
var sort_list=func(begin,end)
|
||||
{
|
||||
for(var i=begin;i<end;i+=1)
|
||||
{
|
||||
var index=i;
|
||||
for(var j=i+1;j<end;j+=1)
|
||||
if(count_list[index]<count_list[j])
|
||||
index=j;
|
||||
if(index!=i)
|
||||
{
|
||||
var tmp=film_list[i];
|
||||
film_list[i]=film_list[index];
|
||||
film_list[index]=tmp;
|
||||
tmp=count_list[i];
|
||||
count_list[i]=count_list[index];
|
||||
count_list[index]=tmp;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
var list_size=size(film_list);
|
||||
list_size=list_size>10?10:list_size;
|
||||
for(var i=0;i<list_size;i+=1)
|
||||
print(i,'\t:',film_list[i].name,'\t',count_list[i]);
|
||||
var choose=input();
|
||||
if(choose=="exit")
|
||||
break;
|
||||
if(num(choose)>=list_size)
|
||||
die("choose a correct index");
|
||||
var label_list=film_node[num(choose)].next;
|
||||
film_list=[];
|
||||
count_list=[];
|
||||
foreach(var label;label_list)
|
||||
foreach(var film;label.next)
|
||||
{
|
||||
var has=0;
|
||||
for(var i=0;i<size(film_list);i+=1)
|
||||
if(film_list[i].name==film.name)
|
||||
{
|
||||
has=1;
|
||||
count_list[i]+=rand();
|
||||
break;
|
||||
}
|
||||
if(has==0)
|
||||
{
|
||||
append(film_list,film);
|
||||
append(count_list,1);
|
||||
}
|
||||
}
|
||||
sort_list(0,size(film_list));
|
||||
}
|
||||
|
||||
foreach(var film;film_node)
|
||||
setsize(film.next,0);
|
||||
9
test/pi.nas
Normal file
9
test/pi.nas
Normal file
@@ -0,0 +1,9 @@
|
||||
import("lib.nas");
|
||||
|
||||
var (t,res)=(1,0);
|
||||
for(var m=1;m<4e6;m+=2)
|
||||
{
|
||||
res+=t*1/m;
|
||||
t=-t;
|
||||
}
|
||||
println(res*4);
|
||||
246
test/prop.nas
246
test/prop.nas
@@ -1,246 +0,0 @@
|
||||
import("lib.nas");
|
||||
|
||||
var property_tree=
|
||||
{
|
||||
accelerations:
|
||||
{
|
||||
'n-z-cg-fps_sec':0,
|
||||
ned:
|
||||
{
|
||||
'down-accel-fps_sec':0,
|
||||
'east-accel-fps_sec':0,
|
||||
'north-accel-fps_sec':0,
|
||||
},
|
||||
nlf:0,
|
||||
pilot:
|
||||
{
|
||||
'x-accel-fps_sec':0,
|
||||
'y-accel-fps_sec':0,
|
||||
'z-accel-fps_sec':0,
|
||||
},
|
||||
'pilot-g':1,
|
||||
'pilot-gdamped':1
|
||||
},
|
||||
ai:
|
||||
{
|
||||
models:
|
||||
{
|
||||
carrier:
|
||||
{
|
||||
callsign:'',
|
||||
controls:{},
|
||||
environment:{},
|
||||
id:2,
|
||||
name:'Nimitz',
|
||||
navaids:{},
|
||||
orientation:{},
|
||||
position:{},
|
||||
radar:{},
|
||||
sign:'CVN-68',
|
||||
sim:{},
|
||||
subID:0,
|
||||
submodels:
|
||||
{
|
||||
path:'',
|
||||
serviceable:1
|
||||
},
|
||||
'surface-positions':{},
|
||||
type:'AI',
|
||||
valid:1,
|
||||
velocities:{},
|
||||
waypoint:{}
|
||||
},
|
||||
'carrier[1]':
|
||||
{
|
||||
callsign:'',
|
||||
controls:{},
|
||||
environment:{},
|
||||
id:3,
|
||||
name:'Eisenhower',
|
||||
navaids:{},
|
||||
orientation:{},
|
||||
position:{},
|
||||
radar:{},
|
||||
sign:'CVN-69',
|
||||
sim:{},
|
||||
subID:0,
|
||||
submodels:
|
||||
{
|
||||
path:'',
|
||||
serviceable:0
|
||||
},
|
||||
'surface-positions':{},
|
||||
type:'AI',
|
||||
valid:1,
|
||||
velocities:{},
|
||||
waypoint:{}
|
||||
},
|
||||
count:2,
|
||||
'model-added':'/ai[0]/models[0]/carrier[1]',
|
||||
'model-removed':nil,
|
||||
'num-players':0
|
||||
},
|
||||
submodels:
|
||||
{
|
||||
contrails:0
|
||||
},
|
||||
},
|
||||
aircraft:
|
||||
{
|
||||
icao:
|
||||
{
|
||||
equipment:'SDFGY',
|
||||
surveillance:'S',
|
||||
type:'ZZZZ',
|
||||
'wake-turbulence-category':'L'
|
||||
},
|
||||
performance:
|
||||
{
|
||||
approach:
|
||||
{
|
||||
'airspeed-knots':150,
|
||||
},
|
||||
climb:'\n\t\t\t\n\t\t\t',
|
||||
cruise:
|
||||
{
|
||||
'airspeed-knots':1000,
|
||||
'altitude-ft':4500,
|
||||
},
|
||||
descent:'\n\t\t\t\n\t\t\t',
|
||||
maximum:'\n\t\t\t\n\t\t\t',
|
||||
minimum:'\n\t\t\t\n\t\t\t',
|
||||
},
|
||||
settings:
|
||||
{
|
||||
fuel_persistent:0,
|
||||
ground_services_persistent:0,
|
||||
radio_persistent:0,
|
||||
tooltips:1,
|
||||
weight_persistent:0
|
||||
}
|
||||
},
|
||||
autopilot:
|
||||
{
|
||||
internal:{},
|
||||
locks:{},
|
||||
'route-manager':{},
|
||||
settings:{},
|
||||
'target-tracking':{},
|
||||
},
|
||||
canvas:
|
||||
{
|
||||
'by-index':
|
||||
{
|
||||
texture:
|
||||
{
|
||||
background:'rgba(0,0,0,0)',
|
||||
group:{},
|
||||
name:'Tooltip',
|
||||
placement:{},
|
||||
size:600,
|
||||
'size[1]':200,
|
||||
status:0,
|
||||
'status-msg':'OK',
|
||||
view:300,
|
||||
'view[1]':100
|
||||
},
|
||||
'texture[1]':
|
||||
{
|
||||
background:'rgba(0,0,0,0)',
|
||||
group:{},
|
||||
mipmapping:1,
|
||||
name:'SymbolCache1024x1024',
|
||||
placement:{},
|
||||
size:1024,
|
||||
'size[1]':1024,
|
||||
status:0,
|
||||
'status-msg':'OK',
|
||||
view:1024,
|
||||
'view[1]':1024
|
||||
},
|
||||
'texture[2]':
|
||||
{
|
||||
background:'rgba(0,0,0,0)',
|
||||
group:{},
|
||||
mipmapping:1,
|
||||
name:'SymbolCache1024x1024',
|
||||
placement:{},
|
||||
size:1024,
|
||||
'size[1]':1024,
|
||||
status:0,
|
||||
'status-msg':'OK',
|
||||
view:1024,
|
||||
'view[1]':1024
|
||||
},
|
||||
}
|
||||
},
|
||||
command:{},
|
||||
consumables:{},
|
||||
controls:{},
|
||||
cursor:'Aircraft/ufo/Models/cursor.ac',
|
||||
devices:{},
|
||||
earthview:{},
|
||||
engines:{},
|
||||
environment:{},
|
||||
ephemeris:{},
|
||||
fdm:{},
|
||||
gear:{},
|
||||
hazards:{},
|
||||
input:{},
|
||||
instrumentation:{},
|
||||
'local-weather':{},
|
||||
logging:{},
|
||||
models:{},
|
||||
nasal:{},
|
||||
orientation:{},
|
||||
position:{},
|
||||
rendering:{},
|
||||
scenery:{},
|
||||
sim:{},
|
||||
source:'Models',
|
||||
'surface-positions':{},
|
||||
systems:{},
|
||||
velocities:{},
|
||||
};
|
||||
|
||||
var setprop=func(prop,value)
|
||||
{
|
||||
if(type(prop)!="string")
|
||||
die("setprop: prop is not a string");
|
||||
var path=split('/',prop);
|
||||
var tmp=property_tree;
|
||||
var path_size=size(path);
|
||||
for(var i=0;i<path_size-1;i+=1)
|
||||
tmp=tmp[path[i]];
|
||||
tmp[path[path_size-1]]=value;
|
||||
return;
|
||||
}
|
||||
var getprop=func(prop)
|
||||
{
|
||||
if(type(prop)!="string")
|
||||
die("getprop: prop is not a string");
|
||||
var path=split('/',prop);
|
||||
var tmp=property_tree;
|
||||
foreach(var i;path)
|
||||
tmp=tmp[i];
|
||||
return tmp;
|
||||
}
|
||||
setprop("aircraft/icao/type",'IDG MD-11');
|
||||
|
||||
var print_prop=func(depth,prop)
|
||||
{
|
||||
var s='';
|
||||
for(var i=0;i<depth;i+=1)
|
||||
s~='| ';
|
||||
if(type(prop)!="hash")
|
||||
return;
|
||||
var m=keys(prop);
|
||||
foreach(var elem;m)
|
||||
{
|
||||
print(s,elem,':',prop[elem]);
|
||||
print_prop(depth+1,prop[elem]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
print_prop(0,property_tree);
|
||||
@@ -9,6 +9,24 @@
|
||||
# local node, there is no equivalent of the "relative path" variants
|
||||
# available in C++; just use node.getNode(path).whatever() instead.
|
||||
#
|
||||
|
||||
##
|
||||
# Utility. Turns any ghosts it finds (either solo, or in an
|
||||
# array) into Node objects.
|
||||
#
|
||||
var wrap = func(node) {
|
||||
var argtype = typeof(node);
|
||||
if(argtype == "ghost") {
|
||||
return wrapNode(node);
|
||||
} elsif(argtype == "vector") {
|
||||
var v = node;
|
||||
var n = size(v);
|
||||
for(var i=0; i<n; i+=1) { v[i] = wrapNode(v[i]); }
|
||||
return v;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
var Node = {
|
||||
getNode : func wrap(_getNode(me._g, arg)),
|
||||
getParent : func wrap(_getParent(me._g, arg)),
|
||||
@@ -166,23 +184,6 @@ var copy = func(src, dest, attr = 0) {
|
||||
if(attr) dest.setAttribute(src.getAttribute());
|
||||
}
|
||||
|
||||
##
|
||||
# Utility. Turns any ghosts it finds (either solo, or in an
|
||||
# array) into Node objects.
|
||||
#
|
||||
var wrap = func(node) {
|
||||
var argtype = typeof(node);
|
||||
if(argtype == "ghost") {
|
||||
return wrapNode(node);
|
||||
} elsif(argtype == "vector") {
|
||||
var v = node;
|
||||
var n = size(v);
|
||||
for(var i=0; i<n; i+=1) { v[i] = wrapNode(v[i]); }
|
||||
return v;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
##
|
||||
# Utility. Returns a new object with its superclass/parent set to the
|
||||
# Node object and its _g (ghost) field set to the specified object.
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
# lib queue.nas
|
||||
var block_alloc=func()
|
||||
{
|
||||
return {elem:nil,next:nil};
|
||||
}
|
||||
var new_queue=func()
|
||||
{
|
||||
return {next:nil};
|
||||
}
|
||||
var queue_push=func(queue,elem)
|
||||
{
|
||||
var tmp=queue;
|
||||
while(tmp.next!=nil)
|
||||
tmp=tmp.next;
|
||||
tmp.next=block_alloc();
|
||||
tmp.next.elem=elem;
|
||||
}
|
||||
var queue_pop=func(queue)
|
||||
{
|
||||
var tmp=queue.next;
|
||||
if(tmp!=nil)
|
||||
queue.next=tmp.next;
|
||||
return;
|
||||
}
|
||||
var queue_front=func(queue)
|
||||
{
|
||||
var tmp=queue.next;
|
||||
if(tmp!=nil)
|
||||
return tmp.elem;
|
||||
return nil;
|
||||
}
|
||||
var queue_empty=func(queue)
|
||||
{
|
||||
return queue.next==nil;
|
||||
}
|
||||
25
test/quick_sort.nas
Normal file
25
test/quick_sort.nas
Normal file
@@ -0,0 +1,25 @@
|
||||
import("lib.nas");
|
||||
var sort=func(vec,left,right)
|
||||
{
|
||||
if(left>=right) return;
|
||||
var (L,R,tmp)=(left,right,vec[left]);
|
||||
while(left<right)
|
||||
{
|
||||
while(left<right and tmp<=vec[right])
|
||||
right-=1;
|
||||
while(left<right and tmp>=vec[left])
|
||||
left+=1;
|
||||
if(left!=right)
|
||||
(vec[left],vec[right])=(vec[right],vec[left]);
|
||||
}
|
||||
(vec[L],vec[left])=(vec[left],tmp);
|
||||
sort(vec,L,left-1);
|
||||
sort(vec,left+1,R);
|
||||
return;
|
||||
}
|
||||
var vec=[];
|
||||
rand(time(0));
|
||||
for(var i=0;i<200;i+=1)
|
||||
append(vec,int(rand()*1000));
|
||||
sort(vec,0,size(vec)-1);
|
||||
println(vec);
|
||||
@@ -1,4 +1,5 @@
|
||||
# basic type
|
||||
import("lib.nas");
|
||||
nil;
|
||||
2147483647;
|
||||
0x7fffffff;
|
||||
@@ -54,7 +55,7 @@ var hash_4={
|
||||
|
||||
# function
|
||||
var func_1=func(){return 1;}
|
||||
var prt=func(x){print(x);return nil;}
|
||||
var prt=func(x){println(x);return nil;}
|
||||
var func_with_dynamic_id=func(a,b,c,d...){return [a,b,c,d];}
|
||||
var func_with_lack_para=func(a,b,c=1,d=2){return a+b+c+d;}
|
||||
var func_with_func_para=func(a,f){return f(a);}
|
||||
@@ -72,21 +73,21 @@ var source={
|
||||
member_1: func func_1(), # this will get a number
|
||||
member_2: func {return 2.71828;} # this will get a function
|
||||
};
|
||||
print(source['member_2']());
|
||||
print(source.member_2());
|
||||
println(source['member_2']());
|
||||
println(source.member_2());
|
||||
|
||||
var test_func=func{return 1;}
|
||||
print(func test_func()); # 1
|
||||
print(test_func()); # 1
|
||||
print(func test_func); # nothing
|
||||
print(test_func); # nothing
|
||||
print(([0,1,2,3])[1]); # 1
|
||||
print(({str:"what?"})["str"]); # what?
|
||||
print(({str:"what?"}).str); # what?
|
||||
println(func test_func()); # 1
|
||||
println(test_func()); # 1
|
||||
println(func test_func); # nothing
|
||||
println(test_func); # nothing
|
||||
println(([0,1,2,3])[1]); # 1
|
||||
println(({str:"what?"})["str"]); # what?
|
||||
println(({str:"what?"}).str); # what?
|
||||
|
||||
# lambda
|
||||
(func(x){return x>0? x:0;})(12);
|
||||
(func{print("hello world");})();
|
||||
(func{println("hello world");})();
|
||||
(((func(x){return 1.0/math.exp(x);})))(0);
|
||||
|
||||
# flexible definition & assignment
|
||||
@@ -114,7 +115,7 @@ var multi_assign_2=[10,9,8,7];
|
||||
((-1*2+9))/7-1;
|
||||
((({num:2})))["num"]*2*2*2;
|
||||
((((([0,1,2])[0:2]))[0:2]))[1]-1;
|
||||
(((((((((((((((((((1+1+2+3+5)+8))+13)))+21))))+34)))))+55))))*89;
|
||||
println((((((((((((((((((((1+1+2+3+5)+8))+13)))+21))))+34)))))+55))))*89); # 12727
|
||||
number_1*(number_2+number_3)/90-number_4;
|
||||
(func test_func)()-1;
|
||||
hash_3.member_3+(func {return {what:"i don't tell you.",case_small:80,case_large:100}})()["case_large"]/10;
|
||||
@@ -123,4 +124,29 @@ nil and 1+7*8;
|
||||
(number_1 or number_2) and (number_3 or number_4-number_4*1);
|
||||
[0,1,4,3,2][4]*2-4+1*2*2*2*2*2/8;
|
||||
{num:0}.num or {what_is_the_secret_of_universe:42}["what_is_the_secret_of_universe"];
|
||||
"123"~"456"-123456*2/2;
|
||||
"123"~"456"-123456*2/2;
|
||||
|
||||
var hash={str:'hello',f:func{return me.str;}};
|
||||
var tmp_f=hash.f;
|
||||
hash=1;
|
||||
println(tmp_f());
|
||||
# undefined symbol 'me'
|
||||
# this means that
|
||||
# when generating local_scope for function f,
|
||||
# nasal_gc will not count 'me' as one reference of this hash
|
||||
|
||||
var h1={str:'hello',f:func{return me.str;}};
|
||||
var h2={str:'world',f:func{return nil;}};
|
||||
h2.f=h1.f;
|
||||
println(h2.f());
|
||||
# print 'world'
|
||||
# this means that 'me' in hash's functions
|
||||
# only points to the hash this function belongs to
|
||||
|
||||
var f1=func(){println(1);return 1;}
|
||||
var f2=func(){println(2);return 0;}
|
||||
f1() or f2();
|
||||
# print '1'
|
||||
# this means that when using 'or' or 'and',
|
||||
# if the result is clear when calculating,
|
||||
# objects behind will not be calculated
|
||||
@@ -1,62 +0,0 @@
|
||||
var global_value=0;
|
||||
var global_hash=
|
||||
{
|
||||
var1:1,
|
||||
var2:2,
|
||||
var3:func(){return me.var2;}
|
||||
};
|
||||
print(global_value);
|
||||
print(global_hash.var3());
|
||||
|
||||
var func1=func()
|
||||
{
|
||||
global_value=1;
|
||||
print(global_value);
|
||||
var closure_value=1;
|
||||
var temp_value=1;
|
||||
print(temp_value);
|
||||
return func{return closure_value;};
|
||||
}
|
||||
|
||||
var func2=func()
|
||||
{
|
||||
for(var temp_value=0;temp_value<100;temp_value+=1)
|
||||
{
|
||||
if(temp_value<10)
|
||||
print(temp_value,"< 10");
|
||||
elsif(10<=temp_value and temp_value<50)
|
||||
print(temp_value,"< 50");
|
||||
temp_value=10;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var func3=func()
|
||||
{
|
||||
var fake_closure_value=1;
|
||||
return func()
|
||||
{
|
||||
var fake_closure_value=2;
|
||||
return fake_closure_value;
|
||||
};
|
||||
}
|
||||
|
||||
func1()();
|
||||
func2();
|
||||
func3()();
|
||||
|
||||
if(!global_value)
|
||||
{
|
||||
var temp_value=1;
|
||||
if(temp_value)
|
||||
{
|
||||
var temp_value=2;
|
||||
if(temp_value>=1)
|
||||
{
|
||||
var temp_value=3;
|
||||
print(temp_value);
|
||||
}
|
||||
print(temp_value);
|
||||
}
|
||||
print(temp_value);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
var hash={str:'hello',f:func{return me.str;}};
|
||||
var tmp_f=hash.f;
|
||||
hash=1;
|
||||
print(tmp_f());
|
||||
# undefined symbol 'me'
|
||||
# this means that
|
||||
# when generating local_scope for function f,
|
||||
# nasal_gc will not count 'me' as one reference of this hash
|
||||
|
||||
var h1={str:'hello',f:func{return me.str;}};
|
||||
var h2={str:'world',f:func{return nil;}};
|
||||
h2.f=h1.f;
|
||||
print(h2.f());
|
||||
# print 'world'
|
||||
# this means that 'me' in hash's functions
|
||||
# only points to the hash this function belongs to
|
||||
|
||||
var f1=func(){print(1);return 1;}
|
||||
var f2=func(){print(2);return 0;}
|
||||
f1() or f2();
|
||||
# print '1'
|
||||
# this means that when using 'or' or 'and',
|
||||
# if the result is clear when calculating,
|
||||
# objects behind will not be calculated
|
||||
@@ -1,34 +0,0 @@
|
||||
# lib stack.nas
|
||||
var block_alloc=func()
|
||||
{
|
||||
return {elem:nil,next:nil};
|
||||
}
|
||||
var new_stack=func()
|
||||
{
|
||||
return {next:nil};
|
||||
}
|
||||
var stack_push=func(stack,elem)
|
||||
{
|
||||
var tmp=stack.next;
|
||||
stack.next=block_alloc();
|
||||
stack.next.elem=elem;
|
||||
stack.next.next=tmp;
|
||||
}
|
||||
var stack_pop=func(stack)
|
||||
{
|
||||
var tmp=stack.next;
|
||||
if(tmp!=nil)
|
||||
stack.next=tmp.next;
|
||||
return;
|
||||
}
|
||||
var stack_top=func(stack)
|
||||
{
|
||||
var tmp=stack.next;
|
||||
if(tmp!=nil)
|
||||
return tmp.elem;
|
||||
return nil;
|
||||
}
|
||||
var stack_empty=func(stack)
|
||||
{
|
||||
return stack.next==nil;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import("lib.nas");
|
||||
var filename="";
|
||||
|
||||
filename=input();
|
||||
print(filename[0]);
|
||||
Reference in New Issue
Block a user