开启辅助访问 切换到窄版

登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 249|回复: 1

空灵-中文编程语言编译器--基础代码展示---讲解

[复制链接]

 成长值: 2820

6

主题

9

帖子

57

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
57
发表于 2025-11-29 20:03:54 | 显示全部楼层 |阅读模式
项目简介
这是一个用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 (内置输入) functions
Advanced 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 / 声明并初始化变量
  2. 让 年龄 = 25
  3. 让 姓名 = "张三"

  4. // Variable assignment / 变量赋值
  5. 年龄 = 26
复制代码
Basic Data Types / 基本数据类型
  1. // Numbers / 数字
  2. 让 数字 = 42
  3. 让 小数 = 3.14

  4. // Strings / 字符串
  5. 让 文本 = "你好,世界!"
  6. 让 带换行的文本 = "第一行\n第二行"

  7. // Boolean values / 布尔值
  8. 让 是真的 = true
  9. 让 是假的 = false
复制代码
Conditional Statements / 条件语句
  1. 让 分数 = 85

  2. 如果 分数 >= 90 {
  3.     打印("优秀")
  4. } 否则 如果 分数 >= 80 {
  5.     打印("良好")
  6. } 否则 如果 分数 >= 60 {
  7.     打印("及格")
  8. } 否则 {
  9.     打印("不及格")
  10. }
复制代码
Loop Statements / 循环语句
  1. // While loop / 当循环
  2. 让 计数器 = 0
  3. 当 计数器 < 5 {
  4.     打印(计数器)
  5.     计数器 = 计数器 + 1
  6. }

  7. // For loop / 对于循环
  8. 对于 i 在 1 到 5 {
  9.     打印(i)
  10. }

  11. // String traversal / 遍历字符串
  12. 让 文本 = "你好"
  13. 对于 字符 在 文本 {
  14.     打印("字符: " + 字符)
  15. }
复制代码
Function Definition and Calling / 函数定义和调用
  1. // Define function / 定义函数
  2. 函数 打印问候(姓名) {
  3.     打印("你好, " + 姓名 + "!")
  4. }

  5. // Call function / 调用函数
  6. 打印问候("张三")

  7. // Function with return value / 带返回值的函数
  8. function 计算阶乘(n) {
  9.     如果 n <= 1 {
  10.         返回 1
  11.     }
  12.     返回 n * 计算阶乘(n - 1)
  13. }

  14. 让 结果 = 计算阶乘(5)
  15. 打印("5的阶乘是: " + 结果)
复制代码
String Operations / 字符串操作
  1. // String concatenation / 字符串连接
  2. 让 名字 = "张"
  3. 让 姓氏 = "三"
  4. 让 全名 = 名字 + 姓氏
  5. 打印(全名)  // Output: 张三

  6. // String and number concatenation / 字符串和数字连接
  7. 让 年龄 = 25
  8. 打印("年龄是: " + 年龄)  // Output: 年龄是: 25
复制代码
Escape Character Support / 转义字符支持
  1. 打印("换行符测试\n第二行")
  2. 打印("制表符测试\t这里")
  3. 打印("引号测试"引号内"")
  4. 打印("反斜杠测试\")
复制代码
Comments / 注释
  1. // This is a single-line comment / 这是单行注释

  2. 让 变量 = 42  // End-of-line comment / 行尾注释

  3. /*
  4. Multi-line comments are not yet supported
  5. Only single-line comments are supported
  6. 多行注释暂不支持
  7. 仅支持单行注释
  8. */
复制代码
Installation and Usage / 安装和使用Build the Project / 编译项目
  1. cargo build
复制代码
Run the Program / 运行程序
  1. # Run after compilation / 编译后运行
  2. ./target/debug/cnlang 程序文件.cn

  3. # Or run directly / 或者直接运行
  4. 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/
  2. │   ├── main.rs          # Program entry point / 程序入口
  3. │   ├── lexer.rs         # Lexical analyzer / 词法分析器
  4. │   ├── parser.rs        # Syntax analyzer / 语法分析器
  5. │   ├── ast.rs           # Abstract syntax tree definition / 抽象语法树定义
  6. │   └── interpreter.rs   # Interpreter / 解释器
  7. ├── Cargo.toml           # Project configuration / 项目配置
  8. ├── README.md           # Project documentation / 项目说明
  9. └── 示例.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 / 计划中功能
回复

使用道具 举报

4

主题

8

帖子

47

积分

新手上路

Rank: 1

积分
47
发表于 2025-11-30 07:19:17 来自手机 | 显示全部楼层
支持啊,听说rust很厉害,空灵应该也是,毕竟继承了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

空灵语言

GMT+8, 2026-9-12 05:56 , Processed in 0.083163 second(s), 22 queries .

空灵语言 欢迎您! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表