Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 code improvement #34

Merged
merged 12 commits into from
Nov 5, 2023
525 changes: 296 additions & 229 deletions README.md

Large diffs are not rendered by default.

534 changes: 300 additions & 234 deletions doc/README_zh.md

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions doc/dev.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# __Development History__

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

## __Contents__

* [__Parser__](#parser)
Expand All @@ -22,6 +24,7 @@
* [__Release Notes__](#release-notes)
* [v8.0](#version-80-release)
* [v11.0](#version-110-release)
* [v11.1](#version-111-release)

## __Parser__

Expand Down Expand Up @@ -697,3 +700,11 @@ This bug is fixed in `v9.0`. So we suggest that do not use `v8.0`.
9. Add `CMakeLists.txt` for cmake user(including `Visual Studio`).

10. New ghost type register process.

### __version 11.1 release__

1. Bug fix: debugger in v11.0 is malfunctional.

2. Bug fix: symbol_finder does not check definition in foreach/forindex loop.

3. Change extension syntax `import.xx.xx` to `use xx.xx`.
11 changes: 11 additions & 0 deletions doc/dev_zh.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# __开发历史记录__

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

## __目录__

* [__语法分析__](#语法分析)
Expand All @@ -22,6 +24,7 @@
* [__发行日志__](#发行日志)
* [v8.0](#version-80-release)
* [v11.0](#version-110-release)
* [v11.1](#version-111-release)

## __语法分析__

Expand Down Expand Up @@ -630,3 +633,11 @@ in __`nasal_dbg.h:215`__: `auto canary=gc.stack+STACK_MAX_DEPTH-1;`
9. 添加`CMakeLists.txt` (可在`Visual Studio`中使用)。

10. 全新的自定义类型注册流程。

### __version 11.1 release__

1. Bug 修复: 修复 v11.0 的 debugger 无法启动的问题。

2. Bug 修复: symbol_finder 不检查 foreach/forindex 中的迭代变量声明的问题。

3. 扩展语法 `import.xx.xx` 改为 `use xx.xx`。
24 changes: 24 additions & 0 deletions doc/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,27 @@ var example_module = func {
};
}();
```

## Import a module

Here is a module named `std/example_module.nas`:

```nasal
var a = 1;
```

Then there's a script file named `test.nas`, import module in this file using this way:

```nasal
use std.example_module;

println(example_module.a); # 1
```

Or this way:

```nasal
import("std/example_module.nas");

println(example_module.a); # 1
```
8 changes: 5 additions & 3 deletions doc/nasal-http-test-web.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
</head>
<body>
<h1>&nbsp;Nasal | Not another scripting language!</h1>
<img src="/doc/pic/social.png" width="900" height="400" style="margin-left: 15px;"><br /></img>
<div class="badges">
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/github/languages/code-size/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github"></img></a>
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/github/v/release/ValKmjolnir/Nasal-Interpreter?style=flat-square&logo=github"></img></a>
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/badge/dev-v10.0-blue?style=flat-square&logo=github"></img></a>
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/badge/dev-v11.2-blue?style=flat-square&logo=github"></img></a>
<a href="https://github.com/ValKmjolnir/Nasal-Interpreter"><img src="https://img.shields.io/badge/license-GPLv2-green?style=flat-square&logo=github"><br/></img></a>
</div>
<h2>&nbsp;Introduction | 介绍</h2>
Expand Down Expand Up @@ -79,13 +80,14 @@ <h2>&nbsp;Benchmark | 执行效率</h2>
在8.0版本中这个解释器需要跑超过2200秒来绘制这张图。
</p>
<p>
The figure below is the feigenbaum-figure generated by ppm script written in nasal.
The figure below is the feigenbaum-figure and burningship-figure generated by ppm script written in nasal.
</p>
<p>
下方是使用 nasal 的 ppm 生成脚本生成的 feigenbaum 图形。
下方是使用 nasal 的 ppm 生成脚本生成的 feigenbaum 和 burningship 图形。
</p>
</text>
<img src="/doc/pic/feigenbaum.png" width="900" height="550" style="margin-left: 15px;"><br /></img>
<img src="/doc/pic/burningship.png" width="900" height="550" style="margin-left: 15px;"><br /></img>
<h2>&nbsp;Example | 样例代码</h2>
<form method="get">
<text style="margin-left: 15px;"> </text>
Expand Down
Binary file added doc/pic/burningship.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 module/fib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var set_new_ghost(var* args, usize size, gc* ngc) {
return nil;
}
f64 num = args[1].num();
*((u32*)res.ghost().pointer) = static_cast<u32>(num);
*(reinterpret_cast<u32*>(res.ghost().pointer)) = static_cast<u32>(num);
std::cout << "set_new_ghost: successfully set ghost = " << num << "\n";
return nil;
}
Expand Down
2 changes: 1 addition & 1 deletion module/libfib.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import.std.dylib;
use std.dylib;

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

Expand Down
2 changes: 1 addition & 1 deletion module/libkey.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import.std.dylib;
use std.dylib;

var (
kbhit,
Expand Down
2 changes: 1 addition & 1 deletion module/libmat.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import.std.dylib;
use std.dylib;

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

Expand Down
2 changes: 1 addition & 1 deletion module/libsock.nas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import.std.dylib;
use std.dylib;

var socket=func(){
var lib=dylib.dlopen("libnasock"~(os.platform()=="windows"?".dll":".so"));
Expand Down
26 changes: 17 additions & 9 deletions module/nasocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var nas_bind(var* args, usize size, gc* ngc) {
server.sin_port = htons(args[2].num());
return var::num(static_cast<double>(bind(
args[0].num(),
(sockaddr*)&server,
reinterpret_cast<sockaddr*>(&server),
sizeof(server)
)));
}
Expand Down Expand Up @@ -99,7 +99,7 @@ var nas_connect(var* args, usize size, gc* ngc) {
memcpy(&addr.sin_addr, entry->h_addr, entry->h_length);
return var::num(static_cast<double>(connect(
args[0].num(),
(sockaddr*)&addr,
reinterpret_cast<sockaddr*>(&addr),
sizeof(sockaddr_in)
)));
}
Expand All @@ -110,11 +110,19 @@ var nas_accept(var* args, usize size, gc* ngc) {
sockaddr_in client;
int socklen = sizeof(sockaddr_in);
#ifdef _WIN32
int client_sd = accept(args[0].num(), (sockaddr*)&client, &socklen);
int client_sd = accept(
args[0].num(),
reinterpret_cast<sockaddr*>(&client),
&socklen
);
#else
int client_sd = accept(args[0].num(), (sockaddr*)&client, (socklen_t*)&socklen);
int client_sd = accept(
args[0].num(),
reinterpret_cast<sockaddr*>(&client),
reinterpret_cast<socklen_t*>(&socklen)
);
#endif
var res=ngc->temp = ngc->alloc(vm_hash);
var res = ngc->temp = ngc->alloc(vm_hash);
auto& hash = res.hash().elems;
hash["sd"] = var::num(static_cast<double>(client_sd));
hash["ip"] = ngc->newstr(inet_ntoa(client.sin_addr));
Expand Down Expand Up @@ -159,7 +167,7 @@ var nas_sendto(var* args, usize size, gc* ngc) {
args[3].str().c_str(),
args[3].str().length(),
args[4].num(),
(sockaddr*)&addr,
reinterpret_cast<sockaddr*>(&addr),
sizeof(sockaddr_in)
)));
}
Expand Down Expand Up @@ -205,7 +213,7 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
buf,
args[1].num(),
args[2].num(),
(sockaddr*)&addr,
reinterpret_cast<sockaddr*>(&addr),
&socklen
);
#else
Expand All @@ -214,8 +222,8 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
buf,
args[1].num(),
args[2].num(),
(sockaddr*)&addr,
(socklen_t*)&socklen
reinterpret_cast<sockaddr*>(&addr),
reinterpret_cast<socklen_t*>(&socklen)
);
#endif
hash["size"] = var::num(static_cast<double>(recvsize));
Expand Down
14 changes: 14 additions & 0 deletions src/ast_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@

namespace nasal {

bool ast_dumper::visit_use_stmt(use_stmt* node) {
dump_indent();
std::cout << "use" << format_location(node->get_location());
push_indent();
for(auto i : node->get_path()) {
if (i==node->get_path().back()) {
set_last();
}
i->accept(this);
}
pop_indent();
return true;
}

bool ast_dumper::visit_null_expr(null_expr* node) {
dump_indent();
std::cout << "null" << format_location(node->get_location());
Expand Down
2 changes: 2 additions & 0 deletions src/ast_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>

namespace nasal {

Expand Down Expand Up @@ -41,6 +42,7 @@ class ast_dumper:public ast_visitor {
}

public:
bool visit_use_stmt(use_stmt*) override;
bool visit_null_expr(null_expr*) override;
bool visit_nil_expr(nil_expr*) override;
bool visit_number_literal(number_literal*) override;
Expand Down
7 changes: 7 additions & 0 deletions src/ast_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ bool ast_visitor::visit_expr(expr* node) {
return true;
}

bool ast_visitor::visit_use_stmt(use_stmt* node) {
for(auto i : node->get_path()) {
i->accept(this);
}
return true;
}

bool ast_visitor::visit_call(call* node) {
node->accept(this);
return true;
Expand Down
1 change: 1 addition & 0 deletions src/ast_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace nasal {
class ast_visitor {
public:
virtual bool visit_expr(expr*);
virtual bool visit_use_stmt(use_stmt*);
virtual bool visit_call(call*);
virtual bool visit_null_expr(null_expr*);
virtual bool visit_nil_expr(nil_expr*);
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var builtin_cocreate(context* ctx, gc* ngc) {
coroutine.ctx.top++;

// store old localr on stack
coroutine.ctx.top[0] = var::addr((var*)nullptr);
coroutine.ctx.top[0] = var::addr(nullptr);
coroutine.ctx.top++;

// store old pc on stack
Expand Down
4 changes: 2 additions & 2 deletions src/dylib_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ var builtin_dlopen(context* ctx, gc* ngc) {

// get "get" function, to get the register table
#ifdef _WIN32
void* register_table_get_function = (void*)GetProcAddress(
void* register_table_get_function = reinterpret_cast<void*>(GetProcAddress(
static_cast<HMODULE>(library_object.ghost().pointer), "get"
);
));
#else
void* register_table_get_function = dlsym(
library_object.ghost().pointer, "get"
Expand Down
25 changes: 22 additions & 3 deletions src/io_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ namespace nasal {
const auto file_type_name = "file";

void filehandle_destructor(void* ptr) {
if (static_cast<FILE*>(ptr)==stdin) {
return;
}
fclose(static_cast<FILE*>(ptr));
}

Expand Down Expand Up @@ -207,6 +204,25 @@ var builtin_eof(context* ctx, gc* ngc) {
));
}

var builtin_stdin(context* ctx, gc* ngc) {
auto file_descriptor = ngc->alloc(vm_obj);
file_descriptor.ghost().set(file_type_name, nullptr, stdin);
return file_descriptor;
}

var builtin_stdout(context* ctx, gc* ngc) {
auto file_descriptor = ngc->alloc(vm_obj);
file_descriptor.ghost().set(file_type_name, nullptr, stdout);
return file_descriptor;
}

var builtin_stderr(context* ctx, gc* ngc) {
auto file_descriptor = ngc->alloc(vm_obj);
file_descriptor.ghost().set(file_type_name, nullptr, stderr);
return file_descriptor;
}


nasal_builtin_table io_lib_native[] = {
{"__readfile", builtin_readfile},
{"__fout", builtin_fout},
Expand All @@ -220,6 +236,9 @@ nasal_builtin_table io_lib_native[] = {
{"__readln", builtin_readln},
{"__stat", builtin_stat},
{"__eof", builtin_eof},
{"__stdin", builtin_stdin},
{"__stdout", builtin_stdout},
{"__stderr", builtin_stderr},
{nullptr, nullptr}
};

Expand Down
3 changes: 3 additions & 0 deletions src/io_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var builtin_tell(context*, gc*);
var builtin_readln(context*, gc*);
var builtin_stat(context*, gc*);
var builtin_eof(context*, gc*);
var builtin_stdin(context*, gc*);
var builtin_stdout(context*, gc*);
var builtin_stderr(context*, gc*);

extern nasal_builtin_table io_lib_native[];

Expand Down
10 changes: 10 additions & 0 deletions src/nasal_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ void expr::accept(ast_visitor* visitor) {
visitor->visit_expr(this);
}

use_stmt::~use_stmt() {
for(auto i : path) {
delete i;
}
}

void use_stmt::accept(ast_visitor* visitor) {
visitor->visit_use_stmt(this);
}

void call::accept(ast_visitor* visitor) {
visitor->visit_call(this);
}
Expand Down
Loading
Loading