Skip to content

Commit d78ebac

Browse files
authored
Merge pull request #76 from nature-lang/feature/release_examples
feat: add release examples
2 parents f3406e1 + e701dba commit d78ebac

File tree

196 files changed

+8043
-7361
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+8043
-7361
lines changed

.clang-format

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ AccessModifierOffset: -4
44
AlignAfterOpenBracket: Align
55
AlignConsecutiveAssignments: None
66
AlignOperands: Align
7+
AlignTrailingComments: false
78
AllowAllArgumentsOnNextLine: false
89
AllowAllConstructorInitializersOnNextLine: false
910
AllowAllParametersOfDeclarationOnNextLine: false
@@ -56,11 +57,11 @@ SpaceBeforeInheritanceColon: true
5657
SpaceBeforeParens: ControlStatements
5758
SpaceBeforeRangeBasedForLoopColon: false
5859
SpaceInEmptyParentheses: false
59-
SpacesBeforeTrailingComments: 0
60+
SpacesBeforeTrailingComments: 1
6061
SpacesInAngles: false
6162
SpacesInCStyleCastParentheses: false
6263
SpacesInContainerLiterals: false
6364
SpacesInParentheses: false
6465
SpacesInSquareBrackets: false
6566
TabWidth: 4
66-
UseTab: Never
67+
UseTab: Never

.gitmodules

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
[submodule "package"]
22
path = package
3-
url = git@github.com:nature-lang/package.git
3+
url = https://github.com:nature-lang/package.git
44
[submodule "nls"]
55
path = nls
6-
url = [email protected]:nature-lang/nls.git
6+
url = https://github.com:nature-lang/nls.git
7+
[submodule "tests/features/cases/20250405_02_compress"]
8+
path = tests/features/cases/20250405_02_compress
9+
url = https://github.com:weiwenhao/compress.git
10+
[submodule "tests/features/cases/20250324_00_llama"]
11+
path = tests/features/cases/20250324_00_llama
12+
url = https://github.com/weiwenhao/llama.n.git
13+
[submodule "tests/features/cases/20250404_00_tetris"]
14+
path = tests/features/cases/20250404_00_tetris
15+
url = https://github.com/weiwenhao/tetris.git
16+
[submodule "tests/features/cases/20250418_00_playground"]
17+
path = tests/features/cases/20250418_00_playground
18+
url = https://github.com/weiwenhao/playground.git
19+
[submodule "tests/features/cases/20250405_00_parker"]
20+
path = tests/features/cases/20250405_00_parker
21+
url = https://github.com/weiwenhao/parker.git

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (CMAKE_BUILD_TYPE MATCHES Debug)
5151
add_compile_definitions(DEBUG_LIR="${DEBUG_LIR}")
5252
endif()
5353
if(DEBUG_ASM)
54-
add_compile_definitions(DEBUG_ASM)
54+
add_compile_definitions(DEBUG_ASM="${DEBUG_ASM}")
5555
endif()
5656
if(DEBUG_PARSER)
5757
add_compile_definitions(DEBUG_PARSER)
@@ -91,7 +91,7 @@ set(GOBIN /usr/local/go/bin/go)
9191
set(PACKAGE_BINARY_PATH ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_BINARY_NAME})
9292

9393
add_custom_target(${PACKAGE_BINARY_NAME} ALL
94-
COMMAND GOOS=${CMAKE_SYSTEM_NAME} GOARCH=${CMAKE_SYSTEM_PROCESSOR} ${GOBIN} build -o ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_BINARY_NAME} ${PROJECT_SOURCE_DIR}/package/main.go
94+
COMMAND CGO_ENABLED=0 GOOS=${CMAKE_SYSTEM_NAME} GOARCH=${CMAKE_SYSTEM_PROCESSOR} ${GOBIN} build -o ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_BINARY_NAME} ${PROJECT_SOURCE_DIR}/package/main.go
9595
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/package
9696
COMMENT "package manager npkg"
9797
)

README.md

+39-27
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,58 @@
22

33
A general-purpose open-source programming language and compiler designed to provide developers with an **elegant and concise** development experience, enabling them to build secure and reliable cross-platform software **simply and efficiently**.
44

5+
56
## Features
67

7-
### Implemented Features
8+
- ✓ Simple, natural, and consistent syntax design
9+
- ✓ Independent compilation system, no dependency on llvm, supports cross-platform compilation
10+
- ✓ Well-formed type system with support for generics, null-value safety, and error handling
11+
- ✓ Automated GC, with very short STW
12+
- ✓ Modularity and package management system, npkg
13+
- ✓ Built-in shared-stack concatenation, millions of concatenation switches per second
14+
- ✓ Built-in libuv cooperates with the concatenation to handle IO event loops
15+
- ✓ Built-in implementations of common data structures and standard libraries, and easy interaction with C
16+
- ✓ Editor lsp support
17+
- ○ Asymptotic GC, capable of manual memory management
18+
- ○ Tests DSLs, efficient and stable use of AI coding
19+
- ○ macho cross-platform connector, currently lacks the macho linker darwin can't be cross-platform compilation.
20+
- ○ Improvement of collaborative scheduling system
21+
- ○ Cross-platform compilation support for wasm and risc64 platforms
22+
- ○ Compilation to readable golang programming language
823

9-
- Clean, natural, and consistent syntax design
10-
- Independent compilation system without LLVM dependency, supporting cross-platform compilation for linux/darwin on amd64/arm64
11-
- Comprehensive type system with support for generics, null safety, and error handling
12-
- Automatic garbage collection with very brief STW (Stop-The-World) pauses
13-
- Modular design with npkg package management system
14-
- Built-in shared-stack coroutines capable of millions of context switches per second
15-
- Built-in libuv integration with coroutine-based IO event loop
16-
- Built-in common data structures and standard library implementation with convenient C language interoperability
17-
- LSP (Language Server Protocol) support for editors
24+
## Project Status
1825

19-
### Planned Features
26+
The nature programming language has reached a usable version, and the syntax APIs are basically stable, there will not be any drastic changes before version 1.0, but there are some minor syntax changes.
2027

21-
- Progressive garbage collection with manual memory management capabilities
22-
- Test module with dedicated testing DSL, aimed at efficient and stable AI-assisted coding
23-
- Enhanced GC and non-preemptive coroutine scheduling system
24-
- Cross-platform compilation support for WASM and RISC64 platforms
25-
- Compilation to readable Go code
28+
The current version of nature source code supports compilation on the following target platforms
29+
- linux/amd64
30+
- linux/arm64
31+
- darwin/amd64
32+
- darwin/arm64
2633

27-
## Project Status
34+
nature includes a set of test cases and a standard library to test the usability of the syntax, but it has not been tested on a medium to large scale project, and so there are still many bugs.
2835

29-
The project is currently under development. While the current syntax and API are relatively stable, minor syntax changes may occur before version 1.0.
36+
Major work in the next versions of The main work is
37+
- Standard library refinement
38+
- Gathering user feedback
39+
- Project validation and bug fixes
40+
- Refinement of language features
41+
- Compilation into a readable golang programming language to increase the usability of the nature programming language
3042

31-
The project includes a test suite and standard library to validate syntax functionality, though it hasn't been tested in medium to large-scale projects yet.
43+
Official website: [https://nature-lang.org](https://nature-lang.org)
3244

33-
Given Nature's current features, it is suitable for game engine and game development, scientific computing and AI, operating systems and IoT, as well as web development.
45+
## Design Philosophy
3446

35-
Future development priorities include:
36-
- Standard library enhancement
37-
- Project validation and bug fixes
38-
- Implementation of planned features listed above
47+
golang is the programming language I work with. It has a simple syntax, a very good and high-performance cross-platform compiler and runtime implementation, an advanced concurrent design style and high-performance network I/O, as well as good support for standard libraries. However, there are some inconveniences.
3948

40-
nature is a project developed out of interest. golang is the programming language I use in my daily work, so nature is designed to be a continuation and optimization of the golang programming language, as described in the Features section.
49+
- Syntax is too simple, leading to lack of expressiveness.
50+
- Type system is not perfect, lack of nullable, enum parameters, generic (now available), etc.
51+
- Error handling is cumbersome.
52+
- GC and preemptive scheduling, although very good, but it also limits the scope of golang's applications in areas such as GUI, OS, game development, and AI.
4153

42-
golang has an excellent and high-performance cross-platform compiler and runtime implementation, and in future releases nature will be compiled into the readable golang language, further increasing the usability of the nature programming language.
54+
nature is designed to be a continuation and optimization of the golang programming language, and pursues certain differences, as described in the Features section.
4355

44-
Official website: [https://nature-lang.org](https://nature-lang.org/)
56+
Based on the existing features of the nature programming language, it is suitable for game engines and game development, scientific computing and AI, OS and IoT, and web development.
4557

4658
## Installation
4759

README_CN.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,46 @@ nature 编程语言
22

33
通用开源编程语言与编译器,期望为开发者提供**简洁优雅**的开发体验,能够**简单高效**构建出安全可靠的跨平台软件。
44

5-
## 特性
6-
7-
已实现的特性
8-
9-
- 简洁、自然、一致性的语法设计
10-
- 独立的编译系统,不依赖 llvm,支持跨平台编译到 linux/darwin + amd64/arm64
11-
- 完善的类型系统,支持泛型、空值安全、错误处理
12-
- 自动 GC,具有非常短暂的 STW
13-
- 模块化与包管理系统 npkg
14-
- 内置共享栈协程,每秒进行数百万的协程切换
15-
- 内置 libuv 与协程合作处理 IO 事件循环
16-
- 内置常用数据结构及标准库实现,并且方便的和 C 语言交互
17-
- 编辑器 lsp 支持
18-
19-
待实现的特性
20-
21-
- 渐进式 GC,能够进行手动内存管理
22-
- 测试模块以及专用测试 DSL,高效稳定利用 AI 编码
23-
- GC 与协程非抢占式调度系统完善
24-
- 跨平台编译支持 wasm、risc64 平台
25-
- 编译为可读的 golang 编程语言
265

27-
## 概况
28-
29-
当前项目正处于开发中,目前的语法 API 基本稳定,在 1.0 版本之前不会有大幅的变化,但是依旧会有小范围的语法改动。
30-
31-
该项目包含一组测试用例及标准库用来测试语法的可用性,但未经过中大型的项目测试。
6+
## 特性
327

33-
基于 nature 编程语言的现有特性,它适用于游戏引擎和游戏开发、科学计算和 AI、操作系统和物联网,以及 Web 开发等领域。
8+
- ✓ 简洁、自然、一致性的语法设计
9+
- ✓ 独立的编译系统,不依赖 llvm,支持跨平台编译
10+
- ✓ 完善的类型系统,支持泛型、空值安全、错误处理
11+
- ✓ 自动 GC,具有非常短暂的 STW
12+
- ✓ 模块化与包管理系统 npkg
13+
- ✓ 内置共享栈协程,每秒进行数百万的协程切换
14+
- ✓ 内置 libuv 与协程合作处理 IO 事件循环
15+
- ✓ 内置常用数据结构及标准库实现,并且方便的和 C 语言交互
16+
- ✓ 编辑器 lsp 支持
17+
- ○ 渐进式 GC,能够进行手动内存管理
18+
- ○ 测试 DSL,高效稳定利用 AI 编码
19+
- ○ macho 跨平台连接器,目前缺少 macho 链接器 darwin 无法跨平台编译。
20+
- ○ 协作式调度系统完善
21+
- ○ 跨平台编译支持 wasm、risc64 平台
22+
- ○ 编译为可读的 golang 编程语言
23+
24+
## 项目概况
25+
26+
nature 编程语言已经达到可用版本,语法 API 基本稳定,在 1.0 版本之前不会有大幅的变化,会有小范围的语法改动。
27+
28+
当前版本 nature 源码支持编译的目标平台有
29+
- linux/amd64
30+
- linux/arm64
31+
- darwin/amd64
32+
- darwin/arm64
33+
34+
nature 包含一组测试用例及标准库用来测试语法的可用性,但未经过中大型的项目测试,所以还有较多的 bug。
3435

3536
后续版本的主要工作有
3637
- 标准库完善
38+
- 收集用户反馈
3739
- 项目验证及 bug 修复
38-
- 上述待实现特性
39-
40-
nature 是兴趣使然而开发的项目,golang 是我在日常工作中使用的编程语言,所以 nature 在设计理念上是对 golang 编程语言的延续与优化,就如特性一栏所述一般。
40+
- 完善语言特性
41+
- 编译为可读 golang 编程语言以增加 nature 编程语言可用性
4142

42-
golang 拥有非常优秀且高性能的跨平台编译器和 runtime 实现,在后续的版本中 nature 将支持编译为可读的 golang 语言,从而进一步增加 nature 编程语言的可用性。
4343

44-
官网 [https://nature-lang.org](https://nature-lang.org/)
44+
官网: [https://nature-lang.org](https://nature-lang.org)
4545

4646
## 安装
4747

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.5.0-beta
1+
v0.5.0

cmake/cpack.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
55
set(CPACK_PACKAGE_FILE_NAME "nature-${PROJECT_VERSION}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
66
set(CPACK_GENERATOR "TGZ")
77

8-
98
message(STATUS "wille generators: ${CPACK_GENERATOR}")
109

1110
include(CPack)

cmd/root.h

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void cmd_entry(int argc, char **argv) {
2626
{"archive", no_argument, NULL, 0},
2727
{"output", required_argument, NULL, 'o'},
2828
{"target", required_argument, NULL, 1},
29+
{"ld", required_argument, NULL, 2},
30+
{"ldflags", required_argument, NULL, 3},
2931
{NULL, 0, NULL, 0}};
3032

3133
int option_index = 0;
@@ -96,6 +98,16 @@ void cmd_entry(int argc, char **argv) {
9698
is_archive = true;
9799
break;
98100
}
101+
case 2: {
102+
// 处理 --ld 参数
103+
strcpy(USE_LD, optarg);
104+
break;
105+
}
106+
case 3: {
107+
// 处理 --ldflags 参数
108+
strcpy(LDFLAGS, optarg);
109+
break;
110+
}
99111
default:
100112
break;
101113
}

lib/linux_amd64/crt1.o

-32 Bytes
Binary file not shown.

lib/linux_arm64/crt1.o

-8 Bytes
Binary file not shown.

main.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ void print_help() {
2020
printf("Build Flags:\n");
2121
printf(" -o <name> Specify output filename (default: main)\n");
2222
printf(" --archive Generate static library (output: lib<name>.a)\n");
23-
printf(" --target Specify target platform for cross-compilation\n\n");
24-
23+
printf(" --target Specify target platform for cross-compilation\n");
24+
printf(" --ld <path> Specify the path to the linker\n");
25+
printf(" --ldflags <flags> Specify linker flags\n\n");
26+
2527
printf("Cross Compilation:\n");
2628
printf(" nature build --target <platform> <source_file>\n\n");
2729

@@ -35,8 +37,9 @@ void print_help() {
3537
printf(" nature build main.n # Basic build\n");
3638
printf(" nature build -o test main.n # Custom output name\n");
3739
printf(" nature build --archive main.n # Generate static library\n");
38-
printf(" nature build --target linux_arm64 main.n # Cross-compile for Linux ARM64\n\n");
39-
40+
printf(" nature build --target linux_arm64 main.n # Cross-compile for Linux ARM64\n");
41+
printf(" nature build --ld /usr/bin/ld main.n # Custom linker and flags\n\n");
42+
4043
printf("Global Flags:\n");
4144
printf(" --help, -h Show help information\n");
4245
printf(" --version, -v Show version information\n");

runtime/aco/aco.c

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ aco_attr_no_asan void aco_resume(aco_t *resume_co) {
440440
pthread_mutex_unlock(&resume_co->share_stack->owner_lock);
441441
#endif
442442
}
443+
443444
uv_key_set(&aco_gtls_co, resume_co);
444445
acosw(resume_co->main_co, resume_co);
445446
uv_key_set(&aco_gtls_co, resume_co->main_co);

runtime/aco/aco.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ typedef struct {
5858

5959
typedef struct {
6060
void *ptr; // need gc mark
61-
size_t sz;
62-
size_t valid_sz;
61+
size_t sz; // save stack size
62+
size_t valid_sz; // 基于 sp 指针计算出来的 valid sz
6363
// max copy size in bytes
6464
size_t max_cpsz;
6565
// copy from share stack to this save stack

runtime/aco/acosw.S

+21-18
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,20 @@ acosw:
9595
#define SYM(x) x
9696
#endif
9797

98-
.globl async_preempt
99-
#if defined(__DARWIN)
98+
#ifdef __DARWIN
99+
.globl _assist_preempt_yield
100+
_assist_preempt_yield:
100101
#else
101-
.type async_preempt, @function
102+
.globl assist_preempt_yield
103+
.type assist_preempt_yield, @function
104+
assist_preempt_yield:
102105
#endif
103-
async_preempt:
104106
#ifdef __AMD64
105107
push rbp
106108
mov rbp, rsp
107109
pushfq
108-
sub rsp, 368
110+
sub rsp, 376 // 376 + ret_addr + rbp + pushfd = 400, 16 对齐
111+
109112
nop
110113
mov QWORD PTR [rsp], rax
111114
mov QWORD PTR [rsp + 8], rcx
@@ -138,13 +141,14 @@ async_preempt:
138141
movups [rsp + 320], xmm13
139142
movups [rsp + 336], xmm14
140143
movups [rsp + 352], xmm15
141-
call SYM(co_preempt_yield)
144+
145+
call SYM(co_preempt_yield) // save and yield
142146

143147

144148
// debug (多的不能确定,但是这里肯定是 0 了,毕竟ret 地址被覆盖了,可关键是,栈空间还是会被污染呢)
145-
mov rdi, rbp
146-
mov rsi, [rbp + 8] // +8 是跳过 prev rbp, 此时栈中存储的值就是 ret addr 的值
147-
call SYM(debug_ret)
149+
// mov rdi, rbp
150+
// mov rsi, [rbp + 8] // +8 是跳过 prev rbp, 此时栈中存储的值就是 ret addr 的值
151+
// call SYM(debug_ret)
148152

149153
movups xmm15, [rsp + 352]
150154
movups xmm14, [rsp + 336]
@@ -176,15 +180,14 @@ async_preempt:
176180
mov rdx, [rsp + 16]
177181
mov rcx, [rsp + 8]
178182
mov rax, [rsp]
179-
add rsp, 368
180-
popfq
181183

182-
pop rbp
184+
add rsp, 376
183185

184-
// thread_handle_sig 部分预留了一些栈空间,现在需要还原成与被抢占的函数一致
185-
// 但是此时栈空间还没有被污染,依旧可以从原空间中找到需要返回的 rip
186-
add rsp, 1032
187-
jmp [rsp - 1032]
186+
popfq // 从栈顶弹出一个值到 RFLAGS 寄存器中,与之前的 pushfq 对应
187+
188+
pop rbp // 从栈顶弹出一个值到rbp寄存器中, 与之前的 push rbp dvyk
189+
190+
ret
188191

189192
#elif defined(__ARM64)
190193
// 保存所有通用寄存器
@@ -268,8 +271,8 @@ async_preempt:
268271

269272
mov sp, x29
270273
ldp x29, x30, [sp], #16
271-
add sp, sp, #1032 // 恢复栈指针
272-
ret // 返回到被中断的位置
274+
// add sp, sp, #1032 // 恢复栈指针
275+
ret // 返回到被中断的位置, 基于 x30 中的值进行返回
273276

274277
#else
275278
#error "platform not support"

0 commit comments

Comments
 (0)