Skip to content

Commit

Permalink
Merge pull request #37 from ValKmjolnir/develop
Browse files Browse the repository at this point in the history
✨ add json cpp lib & argparse module
  • Loading branch information
ValKmjolnir authored Dec 11, 2023
2 parents 1148ef1 + 6e81939 commit a63bb6c
Show file tree
Hide file tree
Showing 110 changed files with 2,986 additions and 2,176 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(NASAL_OBJECT_SOURCE_FILE
${CMAKE_SOURCE_DIR}/src/fg_props.cpp
${CMAKE_SOURCE_DIR}/src/bits_lib.cpp
${CMAKE_SOURCE_DIR}/src/io_lib.cpp
${CMAKE_SOURCE_DIR}/src/json_lib.cpp
${CMAKE_SOURCE_DIR}/src/math_lib.cpp
${CMAKE_SOURCE_DIR}/src/dylib_lib.cpp
${CMAKE_SOURCE_DIR}/src/unix_lib.cpp
Expand Down Expand Up @@ -84,4 +85,4 @@ target_link_libraries(mat module-used-object)

add_library(nasock SHARED ${CMAKE_SOURCE_DIR}/module/nasocket.cpp)
target_include_directories(nasock PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(nasock module-used-object)
target_link_libraries(nasock module-used-object)
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

![GitHub code size](https://img.shields.io/github/languages/code-size/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![GitHub release(latest by date)](https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![in dev](https://img.shields.io/badge/dev-v11.2-blue?style=flat-square&logo=github)
[![license](https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github)](./LICENSE)
![downloads](https://img.shields.io/github/downloads/ValKmjolnir/Nasal-Interpreter/total.svg?style=flat-square&logo=github)

> This document is also available in: [__中文__](./doc/README_zh.md) | [__English__](./README.md)
Expand Down Expand Up @@ -33,17 +33,21 @@ __Contact us if having great ideas to share!__

## __Introduction__

![star](https://img.shields.io/github/stars/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![fork](https://img.shields.io/github/forks/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![issue](https://img.shields.io/github/issues/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![pr](https://img.shields.io/github/issues-pr/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)

[Nasal](http://wiki.flightgear.org/Nasal_scripting_language)
is an ECMAscript-like language used in [FlightGear](https://www.flightgear.org/).
The designer is [Andy Ross](https://github.com/andyross).

This interpreter is totally rewritten by [ValKmjolnir](https://github.com/ValKmjolnir) using `C++`(`-std=c++17`)
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.
This interpreter is rewritten by [ValKmjolnir](https://github.com/ValKmjolnir) using `C++`(`-std=c++17`).
We really appreciate that Andy created this amazing programming language: [Andy Ross's nasal interpreter](https://github.com/andyross/nasal).

This project uses __MIT license__ (2019/7 ~ 2021/5/4 ~ 2023/5), __GPL v2 license__ (since 2023/6).

### __Why writing this nasal interpreter?__
### __Why writing this Nasal interpreter?__

2019 summer,
members in [FGPRC](https://www.fgprc.org/) told me that it is hard to debug with nasal-console in Flightgear,
Expand Down
13 changes: 9 additions & 4 deletions doc/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

![GitHub code size](https://img.shields.io/github/languages/code-size/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![GitHub release(latest by date)](https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![in dev](https://img.shields.io/badge/dev-v11.2-blue?style=flat-square&logo=github)
[![license](https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github)](../LICENSE)
![downloads](https://img.shields.io/github/downloads/ValKmjolnir/Nasal-Interpreter/total.svg?style=flat-square&logo=github)

> 这篇文档包含多语言版本: [__中文__](../doc/README_zh.md) | [__English__](../README.md)
Expand Down Expand Up @@ -33,15 +33,20 @@ __如果有好的意见或建议,欢迎联系我们!__

## __简介__

![star](https://img.shields.io/github/stars/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![fork](https://img.shields.io/github/forks/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![issue](https://img.shields.io/github/issues/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)
![pr](https://img.shields.io/github/issues-pr/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github)

[Nasal](http://wiki.flightgear.org/Nasal_scripting_language)
是一款语法与ECMAscript相似的编程语言,并作为运行脚本被著名开源飞行模拟器 [FlightGear](https://www.flightgear.org/) 所使用。
是一款语法与 ECMAscript 相似的编程语言,并作为脚本语言被著名开源飞行模拟器 [FlightGear](https://www.flightgear.org/) 所使用。
该语言的设计者为 [Andy Ross](https://github.com/andyross)

该解释器项目由 [ValKmjolnir](https://github.com/ValKmjolnir) 完全使用 `C++`(`-std=c++17`)重新实现,没有复用 [Andy Ross的nasal解释器](https://github.com/andyross/nasal) 中的任何一行代码。尽管没有参考任何代码,我们依然非常感谢Andy为我们带来了这样一个神奇且简洁的编程语言
该解释器项目由 [ValKmjolnir](https://github.com/ValKmjolnir) 使用 `C++`(`-std=c++17`)重新实现,我们非常感谢Andy为我们带来了这样一个神奇且简洁的编程语言: [Andy Ross 的 nasal 解释器](https://github.com/andyross/nasal)

该项目使用 __MIT__ 协议开源 (2019/7 ~ 2021/5/4 ~ 2023/5),从 2023/6 开始使用 __GPL v2__ 协议。

### __我们为什么想要重新写一个nasal解释器?__
### __我们为什么想要重新写一个 Nasal 解释器?__

2019年暑假,[FGPRC](https://www.fgprc.org.cn/) 的成员告诉我,在Flightgear中提供的nasal控制台窗口中进行调试很不方便,仅仅是想检查语法错误,也得花时间打开软件等待加载进去后进行调试。所以我就写了一个全新的解释器来帮助他们检查语法错误以及运行时错误。

Expand Down
20 changes: 9 additions & 11 deletions doc/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,12 @@ In 2022/2/17 update we added `\e` into the lexer. And the `bfcolored.nas` uses t

![mandelbrot](../doc/pic/mandelbrot.png)

## More nasal generated pictures

![mandelbrotset](../doc/pic/mandelbrotset.png)

![mandelbrotset_reverse](../doc/pic/mandelbrotset_reverse.png)

![burningship](../doc/pic/burningship.png)

![burningship_reverse](../doc/pic/burningship_reverse.png)

![feigenbaum](../doc/pic/feigenbaum.png)
## More Nasal Generated Pictures

|Mandelbrot Set|Mandelbrot Set|Julia Set|
|:----:|:----:|:----:|
|[mandelbrotset.nas](../test/mandelbrotset.nas)|[mandelbrotset.nas](../test/mandelbrotset.nas)|[juliaset.nas](../test/juliaset.nas)|
|![mandelbrotset](../doc/pic/mandelbrotset.png)|![mandelbrotset_reverse](../doc/pic/mandelbrotset_reverse.png)|![juliaset](../doc/pic/juliaset.png)|
|__Burning Ship__|__Burning Ship__|__Feigenbaum__|
|[burningship.nas](../test/burningship.nas)|[burningship.nas](../test/burningship.nas)|[feigenbaum.nas](../test/feigenbaum.nas)|
|![burningship](../doc/pic/burningship.png)|![burningship_reverse](../doc/pic/burningship_reverse.png)|![feigenbaum](../doc/pic/feigenbaum.png)|
Binary file added doc/pic/juliaset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ module_func_info func_tbl[] = {
// the reason why using this way to get function pointer
// is because `var` has constructors, which is not compatiable in C
// so "extern "C" var fib" may get compilation warnings
extern "C" module_func_info* get() {
NASAL_EXTERN module_func_info* get() {
return func_tbl;
}
```
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ module_func_info func_tbl[] = {
// 之所以用这种方式来获取函数指针, 是因为`var`是有构造函数的
// 有构造函数的类型作为返回值, 和C是不兼容的, 这导致
// 类似 "extern "C" var fib" 的写法会得到编译错误
extern "C" module_func_info* get() {
NASAL_EXTERN module_func_info* get() {
return func_tbl;
}
```
Expand Down
14 changes: 12 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ else
CXXFLAGS = -std=$(STD) -c -O3 -fno-exceptions -fPIC
endif

NASAL_HEADER=\
NASAL_HEADER = \
src/ast_dumper.h\
src/ast_visitor.h\
src/nasal_ast.h\
Expand All @@ -32,11 +32,12 @@ NASAL_HEADER=\
src/io_lib.h\
src/math_lib.h\
src/dylib_lib.h\
src/json_lib.h\
src/unix_lib.h\
src/coroutine.h\
src/repl.h

NASAL_OBJECT=\
NASAL_OBJECT = \
build/nasal_err.o\
build/nasal_ast.o\
build/ast_visitor.o\
Expand All @@ -57,6 +58,7 @@ NASAL_OBJECT=\
build/math_lib.o\
build/unix_lib.o\
build/dylib_lib.o\
build/json_lib.o\
build/coroutine.o\
build/nasal_type.o\
build/nasal_vm.o\
Expand Down Expand Up @@ -161,6 +163,13 @@ build/dylib_lib.o: \
src/dylib_lib.h src/dylib_lib.cpp | build
$(CXX) $(CXXFLAGS) src/dylib_lib.cpp -o build/dylib_lib.o

build/json_lib.o: \
src/nasal.h\
src/nasal_type.h\
src/nasal_gc.h\
src/json_lib.h src/json_lib.cpp | build
$(CXX) $(CXXFLAGS) src/json_lib.cpp -o build/json_lib.o

build/unix_lib.o: \
src/nasal.h\
src/nasal_type.h\
Expand Down Expand Up @@ -237,6 +246,7 @@ clean:

.PHONY: test
test:nasal
@ ./nasal test/argparse_test.nas
@ ./nasal -e test/ascii-art.nas
@ ./nasal -t -d test/bfs.nas
@ ./nasal -t test/bigloop.nas
Expand Down
43 changes: 35 additions & 8 deletions module/fib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,40 @@ var quick_fib(var* args, usize size, gc* ngc) {

const auto ghost_for_test = "ghost_for_test";

struct ghost_obj {
u32 number = 0;
var test_string = nil;
};

// if the dynamic library is closed, the pointer of this function will be unsafe
// make sure gc deletes the object before closing the dynamic library
// or just do not close the dynamic library...
void ghost_for_test_destructor(void* ptr) {
std::cout << "ghost_for_test::destructor (0x";
std::cout << std::hex << reinterpret_cast<u64>(ptr) << std::dec << ") {\n";
delete static_cast<u32*>(ptr);
delete static_cast<ghost_obj*>(ptr);
std::cout << " delete 0x" << std::hex;
std::cout << reinterpret_cast<u64>(ptr) << std::dec << ";\n";
std::cout << "}\n";
}

void ghost_for_test_gc_marker(void* ptr, std::vector<var>* bfs_queue) {
std::cout << "ghost_for_test::mark (0x";
std::cout << std::hex << reinterpret_cast<u64>(ptr) << std::dec << ") {\n";
bfs_queue->push_back(static_cast<ghost_obj*>(ptr)->test_string);
std::cout << " mark 0x" << std::hex;
std::cout << reinterpret_cast<u64>(ptr) << std::dec << "->test_string;\n";
std::cout << "}\n";
}

var create_new_ghost(var* args, usize size, gc* ngc) {
var res = ngc->alloc(vm_obj);
res.ghost().set(ghost_for_test, ghost_for_test_destructor, new u32);
var res = ngc->alloc(vm_type::vm_ghost);
res.ghost().set(
ghost_for_test,
ghost_for_test_destructor,
ghost_for_test_gc_marker,
new ghost_obj
);
return res;
}

Expand All @@ -64,8 +86,10 @@ var set_new_ghost(var* args, usize size, gc* ngc) {
return nil;
}
f64 num = args[1].num();
*(reinterpret_cast<u32*>(res.ghost().pointer)) = static_cast<u32>(num);
std::cout << "set_new_ghost: successfully set ghost = " << num << "\n";
reinterpret_cast<ghost_obj*>(res.ghost().pointer)->number = static_cast<u32>(num);
std::cout << "set_new_ghost: successfully set ghost.number = " << num << "\n";
reinterpret_cast<ghost_obj*>(res.ghost().pointer)->test_string = ngc->newstr("just for test");
std::cout << "set_new_ghost: successfully set ghost.test_string = just for test\n";
return nil;
}

Expand All @@ -75,8 +99,11 @@ var print_new_ghost(var* args, usize size, gc* ngc) {
std::cout << "print_new_ghost: not ghost for test type.\n";
return nil;
}
std::cout << "print_new_ghost: " << res.ghost() << " result = "
<< *((u32*)res.ghost().pointer) << "\n";
std::cout << "print_new_ghost: " << res.ghost() << " number = "
<< reinterpret_cast<ghost_obj*>(res.ghost().pointer)->number
<< " test_string = "
<< reinterpret_cast<ghost_obj*>(res.ghost().pointer)->test_string
<< "\n";
return nil;
}

Expand All @@ -91,7 +118,7 @@ module_func_info func_tbl[] = {

}

extern "C" module_func_info* get() {
NASAL_EXTERN module_func_info* get() {
return fib_module::func_tbl;
}

Expand Down
6 changes: 5 additions & 1 deletion module/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <unistd.h>
#endif

#ifdef _MSC_VER
#pragma warning (disable:4996)
#endif

#ifdef _WIN32
#include <conio.h>
#else
Expand Down Expand Up @@ -106,7 +110,7 @@ module_func_info func_tbl[] = {
{nullptr, nullptr}
};

extern "C" module_func_info* get() {
NASAL_EXTERN module_func_info* get() {
return func_tbl;
}

Expand Down
16 changes: 10 additions & 6 deletions module/libfib.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std.dylib;
use std.os;

var _dl = dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so"));

Expand All @@ -18,37 +19,40 @@ var _call = dylib.limitcall(1);

var _test_call = dylib.limitcall(2);


var fib = func(x) {
return _call(_fib, x)
}


var qfib = func(x) {
return _call(_qfib, x)
}


var create_ghost = func() {
return _zero_call(_create_ghost)
}


var set_ghost = func(object, x) {
return _test_call(_set_ghost, object, x)
}


var print_ghost = func(object) {
return _call(_print_ghost, object)
}


var test_ghost = func() {
var ghost = create_ghost();
print_ghost(nil); # err
print("\n");
print_ghost(ghost); # random
print("\n");
set_ghost(nil, 114); # err
print("\n");
set_ghost(ghost, 114); # success
print("\n");
for(var i = 0; i<256; i+=1) {
var temp = []; # try to trigger gc
}
print("\n");
print_ghost(ghost); # 114
print("\n");
}
1 change: 1 addition & 0 deletions module/libkey.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std.dylib;
use std.os;

var (
kbhit,
Expand Down
1 change: 1 addition & 0 deletions module/libmat.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std.dylib;
use std.os;

var _dl = dylib.dlopen("libmat."~(os.platform()=="windows"?"dll":"so"));

Expand Down
1 change: 1 addition & 0 deletions module/libsock.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std.dylib;
use std.os;

var socket = func() {
var lib = dylib.dlopen("libnasock"~(os.platform()=="windows"? ".dll":".so"));
Expand Down
2 changes: 1 addition & 1 deletion module/makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY=clean all winall
.PHONY = clean all winall

dynamic_libs_so = libfib.so libkey.so libnasock.so libmat.so
dynamic_libs_dll = libfib.dll libkey.dll libnasock.dll libmat.dll
Expand Down
Loading

0 comments on commit a63bb6c

Please sign in to comment.