项目简介 这是一个用Rust开发的中文编程语言编译器/解释器,旨在让中文用户能够使用中文关键字和语法进行编程,降低编程学习门槛,提高代码可读性。 Rust优势 用Rust打造卓越性能 1、内存安全:Rust的所有权系统确保内存安全,无需垃圾回收,防止空指针解引用和缓冲区溢出等常见错误。 2、高性能: Rust提供零成本抽象,编译为本地机器码,提供与C和C++相当的性能,同时保持安全性。 3、 并发支持: Rust的无畏并发模型支持安全的并行编程,使其成为构建高效可靠编译器系统的理想选择。 4、跨平台:Rust可编译到多个平台(Windows、macOS、Linux),确保在不同操作系统上的一致行为。 5、现代工具链: Rust的包管理器(Cargo)和构建系统提供出色的依赖管理和开发体验。 灵创/空灵 语言集成 本编译器是灵创/空灵语言生态系统的重要组成部分。灵创/空灵语言是一个完整的编译器+集成开发环境(IDE)解决方案,专为中文编程设计。作为灵创/空灵语言平台持续发展的一部分,本编译器将继续获得更新和改进。 Features / 功能特性 Core Features / 核心功能- Chinese Keyword Support / 中文关键字支持:Use Chinese keywords like 如果 (if), 循环 (loop), 函数 (function), etc.
- Variable System / 变量系统:Support variable declaration and assignment (让 keyword)
- Data Types / 数据类型:Support basic data types like numbers, strings, boolean values
- Function System / 函数系统:Support function definition, parameter passing, and recursive calls
- Control Flow / 控制流:Support conditional statements (如果/否则), loop statements (循环, 当, 对于)
- String Operations / 字符串操作:Support string concatenation, escape character handling
- Built-in Functions / 内置函数:Provide printing (内置打印) and input (内置输入) functionsAdvanced Features / 高级特性- Scope Management / 作用域管理:Support local variables and function scope
- Recursion Support / 递归支持:Complete support for recursive function calls
- Error Handling / 错误处理:Provide friendly error messages
- Escape Characters / 转义字符:Support common escape sequences like \n, \t, \
- Comment Support / 注释支持:Support single-line comments (//)Syntax Guide / 语法说明 Variable Declaration and Assignment / 变量声明和赋值1. // Declare and initialize variables / 声明并初始化变量
- 让 年龄 = 25
- 让 姓名 = "张三"
- // Variable assignment / 变量赋值
- 年龄 = 26复制代码Basic Data Types / 基本数据类型1. // Numbers / 数字
- 让 数字 = 42
- 让 小数 = 3.14
- // Strings / 字符串
- 让 文本 = "你好,世界!"
- 让 带换行的文本 = "第一行\n第二行"
- // Boolean values / 布尔值
- 让 是真的 = true
- 让 是假的 = false复制代码Conditional Statements / 条件语句1. 让 分数 = 85
- 如果 分数 >= 90 {
- 打印("优秀")
- } 否则 如果 分数 >= 80 {
- 打印("良好")
- } 否则 如果 分数 >= 60 {
- 打印("及格")
- } 否则 {
- 打印("不及格")
- }复制代码Loop Statements / 循环语句1. // While loop / 当循环
- 让 计数器 = 0
- 当 计数器 < 5 {
- 打印(计数器)
- 计数器 = 计数器 + 1
- }
- // For loop / 对于循环
- 对于 i 在 1 到 5 {
- 打印(i)
- }
- // String traversal / 遍历字符串
- 让 文本 = "你好"
- 对于 字符 在 文本 {
- 打印("字符: " + 字符)
- }复制代码Function Definition and Calling / 函数定义和调用1. // Define function / 定义函数
- 函数 打印问候(姓名) {
- 打印("你好, " + 姓名 + "!")
- }
- // Call function / 调用函数
- 打印问候("张三")
- // Function with return value / 带返回值的函数
- function 计算阶乘(n) {
- 如果 n <= 1 {
- 返回 1
- }
- 返回 n * 计算阶乘(n - 1)
- }
- 让 结果 = 计算阶乘(5)
- 打印("5的阶乘是: " + 结果)复制代码String Operations / 字符串操作1. // String concatenation / 字符串连接
- 让 名字 = "张"
- 让 姓氏 = "三"
- 让 全名 = 名字 + 姓氏
- 打印(全名) // Output: 张三
- // String and number concatenation / 字符串和数字连接
- 让 年龄 = 25
- 打印("年龄是: " + 年龄) // Output: 年龄是: 25复制代码Escape Character Support / 转义字符支持1. 打印("换行符测试\n第二行")
- 打印("制表符测试\t这里")
- 打印("引号测试"引号内"")
- 打印("反斜杠测试")复制代码Comments / 注释1. // This is a single-line comment / 这是单行注释
- 让 变量 = 42 // End-of-line comment / 行尾注释
- /*
- Multi-line comments are not yet supported
- Only single-line comments are supported
- 多行注释暂不支持
- 仅支持单行注释
- /复制代码Installation and Usage / 安装和使用 Build the Project / 编译项目1. cargo build复制代码*Run the Program / 运行程序1. # Run after compilation / 编译后运行
- ./target/debug/cnlang 程序文件.cn
-
Or run directly / 或者直接运行
- cargo run -- 程序文件.cn复制代码Example Programs / 示例程序The project includes several example programs in the project root directory: 项目包含多个示例程序,位于项目根目录:- 示例.cn - Comprehensive example showing various features / 综合示例,展示各种功能
- test_basic.cn - Basic syntax test / 基础语法测试
- test_recursion.cn - Recursive function test / 递归函数测试
- test_escape_chars.cn - Escape character test / 转义字符测试Project Structure / 项目结构1. ├── src/
- │ ├── main.rs # Program entry point / 程序入口
- │ ├── lexer.rs # Lexical analyzer / 词法分析器
- │ ├── parser.rs # Syntax analyzer / 语法分析器
- │ ├── ast.rs # Abstract syntax tree definition / 抽象语法树定义
- │ └── interpreter.rs # Interpreter / 解释器
- ├── Cargo.toml # Project configuration / 项目配置
- ├── README.md # Project documentation / 项目说明
- └── 示例.cn # Example program / 示例程序复制代码Technical Architecture / 技术架构 Compilation Process / 编译流程- Lexical Analysis / 词法分析:Convert source code to token sequence / 将源代码转换为token序列
- Syntax Analysis / 语法分析:Build abstract syntax tree (AST) from token sequence / 将token序列构建为抽象语法树(AST)
- Semantic Analysis / 语义分析:Verify syntax correctness and type checking / 验证语法正确性和类型检查
- Interpretation / 解释执行:Traverse AST and execute corresponding operations / 遍历AST并执行相应操作Core Components / 核心组件 Lexer / 词法分析器- Convert source code strings to token sequences / 负责将源代码字符串转换为token序列
- Support Chinese identifiers and keyword recognition / 支持中文标识符和关键字识别
- Handle escape characters and comments / 处理转义字符和注释Parser / 语法分析器- Build abstract syntax tree from token sequences / 将token序列构建为抽象语法树
- Support expression, statement, function definition syntax structures / 支持表达式、语句、函数定义等语法结构
- Handle operator precedence and associativity / 处理运算符优先级和结合性Interpreter / 解释器- Execute abstract syntax tree / 执行抽象语法树
- Manage variable scope and environment / 管理变量作用域和环境
- Handle function calls and recursion / 处理函数调用和递归Development Roadmap / 开发计划 Completed Features / 已完成功能- ✅ Basic lexical analysis / 基本词法分析
- ✅ Syntax analysis and AST construction / 语法分析和AST构建
- ✅ Variable and scope management / 变量和作用域管理
- ✅ Function definition and calling / 函数定义和调用
- ✅ Recursion support / 递归支持
- ✅ String operations / 字符串操作
- ✅ Escape character handling / 转义字符处理
- ✅ Comment support / 注释支持
- ✅ Error handling / 错误处理Planned Features / 计划中功能- |