We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.
文枢
- You could also call the programming language WSCNPL.
- This is still a work in progress. It may be changed in the future.
文枢 is yet another Chinese programming language designed by PSTF.
Overview
文枢 (Wenshu) is a pure-Chinese programming language for educational use and pseudo-code. It is mostly inspired from 喵谕 Meaoiu.
Basics
Program Structure
- Indentation
- Use indentation (spaces or tabs) to denote code blocks, without the need for a "结束" or brace keyword.
- Note
- Anything starting with a pound sign (#) or asterisk (※) is a note.
# 这是注释
※ 这也是注释
定义 主函数():
输出("你好,文枢")
- Program Entry Point
- The program entry point defaults to the first line of the program.
Data Types
- 整数 (Integer)
- A sequence made up of digits is called an integer. Integers can have leading zeros or a plus or minus sign in front. Underscores in an integer are ignored. They have unlimited precision, which means a single integer can store Avogadro's number, the Earth's mass, or even 1000 factorial.
- 小数 (Float)
- A string of two number sequences separated by a decimal point is called a decimal or a floatig-point number. If any side of the decimal point is omitted, it means the value on that side is 0, but at least one side of the decimal point must have a number. Both integer and float belongs to numbers (数字).
- 文档 (String)
- A series of characters enclosed in double or single quotes is called a string. It supports escaping. You can create multi-line strings by using triple/sextuple quotes.
- 布尔值 (Boolean)
- A value that is either true (真) or false (假).
- 空值 (Void)
- Just a void literal.
- 列表 (List)
- A series of elements enclosed in square brackets and separated by commas. The elements in the list can be of any type.
- 元型 (Type)
- Data types themselves are a kind of type, which means types are first-class citizens. You can assign types to variables or pass them as parameters.
- 任意 (Any)
- Just any type.
- 函数 (Function)
- Functions themselves are also considered first-class citizens. They can be used for assignment and passing around, but one thing to especially keep in mind is that functions aren't standard data types.
Operator and Symbol Mapping
You can use Chinese operators or their fully equivalent standard math symbols:
| Meaning | Localized | Standard |
|---|---|---|
| Addition | 加 | + |
| Subtraction | 减 | - |
| Multiplication | 乘 | * or × |
| Division | 除 | / or ÷ |
| Modulo | 模 | % |
| Equality | 等于 | = |
| Inequality | 不等于 | != or ≠ |
| Superior | 大于 | > |
| Non-superior | 不大于 | ≯, ≤ or <= |
| Inferior | 小于 | < |
| Non-inferior | 不小于 | ≮, ≥ or >= |
| Logical AND | 且 | && or ∧ |
| Logical OR | 或 | || or ∨ |
| Logical NOT | 不 | ! or ¬ |
| Exponent | 幂 | ^ |
| Assignment | 是 | <- |
Control Flow
Conditional Branch
This is the structure and format of a conditional branch statement.
如果 条件:
代码1
否则如果 条件:
代码2
否则:
代码3
For example, we have this:
如果 x 大于 5:
输出(“太大了!”)
否则:
输出(“太小了!”)
While Loop
当 条件:
这段代码将在条件成立时一直执行
You can use 跳出 or 继续 to break out or continue the code.
Iterative Loop
对于 迭代对象 于 迭代容器:
阿巴阿巴阿巴
Function
Regular Functions
定义 函数名称(参数@类型、关键字参数@类型<-默认值)->返回类型:
函数体
# 返回 *** (如果需要返回则可以写)
For example, this is a doubling function.
定义 双倍(n@数字) -> 数字:
返回 n × 2
馕 是 双倍(5)
※ 馕此时的值应该是10
Anonymous Functions
The language supports anonymous functions, which are defined using a lowercase lambda (λ) or a backslash, like this.
λ(参数列表)->表达式 \(参数列表)->表达式
Nested Functions
You can define functions inside other functions, and the outer function's variables will be captured by the inner function and used when it runs, commonly known as a 'closure'. For example,
定义 构建增量(步长@整数):
定义 内部(值@整数) -> 整数:
返回 值 + 步长
返回 内部
加五 是 构建增量(5)
打印(加五(10)) # 输出 15 (步长5被闭包捕获)
Summary
※ 1. 递归(经典斐波那契)
定义 斐波那契(n 整数) -> 整数:
如果 n 小于 2:
返回 n
返回 斐波那契(n 减 1) 加 斐波那契(n 减 2)
打印("斐波那契数列的第七项是 " 加 转字(斐波那契(7)))
※ 2. 匿名函数(λ 符号)
平方 = λ(x) -> x 乘 x
立方 = \(x) -> x 乘 x 乘 x # 使用 \ 符号
打印("5² = " 加 转字(平方(5)))
打印("3³ = " 加 转字(立方(3)))
※ 3. 闭包(计数器工厂)
定义 创建计数器(初始值 整数):
计数 是 初始值
定义 内部():
计数 是 计数 加 1 # 注意:这里需要声明 nonlocal,此处为演示闭包捕获
返回 计数
返回 内部
※ 由于文枢支持 nonlocal(此处标记一下),闭包正确捕获变量
定义 创建计数器修正版(初始值 整数):
计数 是 初始值
定义 内部():
非局部 计数
计数 是 计数 加 1
返回 计数
返回 内部
我的计数器 = 创建计数器修正版(10)
打印(我的计数器()) # 11
打印(我的计数器()) # 12
Error Handling and Raising
试:
除数 是 0
如果 除数 等于 0:
抛出 "除数不能为零"
结果 是 10 除 除数
捕获 错误实例 为 错误信息:
打印("捕获到异常: " 加 转为文档(错误信息))
终:
打印("无论是否出错,都会执行此段")
If without error handling and raising, then the program will output these:
运行失败。 代码出错于第 3 行,第 6 列。 错误描述:0 不能做除数!
Of course this is the error message that will be implemented later. Currently only syntax errors and type errors will have their unique message.
Structures
A structure is defined like below.
结构 点:
x 小数
y 小数
原点 是 点(0.0, 0.0)
P 是 点(原点.x 加 5, 原点.y 加 3) # 复制
P撇 是 点(-P.x,P,y) # 还是复制
Just like in C/C++, structures are value-type, so it's still somewhat weaker than the class we'll introduce below.
OOP Programming
从 《数学》 导入 圆周率
# 类的定义
类 形状:
定义 初始化(本,参数列表):
# 未实现
定义 面积(本):
# 未实现
# 继承与多态
类 长方形:
属性 长 数字
属性 宽 数字
定义 初始化(本,长@数字,宽@数字):
本.长 是 长
本.宽 是 宽
定义 面积(本):
返回 本.长 × 本.宽
类 圆:
属性 半径 数字
定义 初始化(本,半径@数字):
本.半径 是 半径
定义 面积(本):
返回 圆周率 × 本.半径 ^ 2
# 实例的创建
甲 是 长方形(3, 5)
打印(甲.面积()) # 15
乙 是 圆(2)
打印(乙.面积()) # ≈12.56637061
Module Import and External API Calls
Importing modules
从 《库名》 导入 项目列表 为 别称列表 导入 《库名》 为 别称
If the project list has an asterisk (*), it means importing everything from a library (except protected or private parts of the library). These statements can import any native or third-party libraries written in the WenShu programming language.
API
We have 外联 as API to other programming language's API. Currently, we have PyShu(蛇枢) and CShu(词枢) API-module for Python and C/C++.
# 调用 Python 生态
外联 python:
import os
return os.listdir('.') # 返回当前目录文件列表
# 调用系统 C 库(示例)
外联 c:
#include <math.h>
double pow(double x, double y); # 声明C函数
return pow(2.0, 3.0); # 返回 8.0
Ability Test
# 完整示例:使用类、递归、闭包、列表推导、错误处理
结构 学生:
名字 字
成绩 浮
类 班级:
属性 成员 列表
定义 初始化(本, 成员 列表):
本.成员 = 成员
定义 平均分(本) -> 浮:
总和 = 0.0
对于 生 于 本.成员:
总和 = 总和 加 生.成绩
返回 总和 除 长度(本.成员)
定义 筛选优秀(学生列表 列表, 阈值 浮) -> 列表:
# 列表推导式(列表内的迭代)
返回 [生.名字 对于 生 于 学生列表 如果 生.成绩 >= 阈值]
定义 递归累加(n 整数) -> 整数:
如果 n <= 0: 返回 0
返回 n 加 递归累加(n 减 1)
试:
数据 = [学生("张三", 92.5), 学生("李四", 78.0), 学生("王五", 88.5)]
高二班 = 班级(数据)
平均 = 高二班.平均分()
打印("班级平均分: " 加 转字(平均))
# 闭包:构建一个基于平均分的及格过滤器
定义 构建过滤器(基准 浮):
返回 \(x 浮) -> x >= 基准
及格线 = 构建过滤器(平均)
打印("王五是否及格: " 加 转字(及格线(数据[2].成绩)))
优秀名单 = 筛选优秀(数据, 85.0)
打印("优秀学生: " 加 转字(优秀名单))
打印("递归累加(5): " 加 转字(递归累加(5)))
捕获 错误 为 异常:
打印("程序发生异常: " 加 转字(异常))
终:
打印("运行结束。")
Built-in Functions
The following list is built-in functions.
- This section is still a work in progress. It may be changed in the future.
打印(内容) 输入(提示) 等待(秒数)
Some Useless(?) Features
This programming language also offers some impractical features. For example, it has a standard library called 啊啊啊, which just stores some useless content. There's also a standard library called 编程之禅, which outputs some guidance for this programming language (I won't put it on this page). Additionally, there's a built-in function called 子曰, which works exactly the same as the corresponding function in wenyan.
Examples
Guessing Number
引入 《随机》
定义 猜数字游戏(目标 整数):
尝试次数 是 0
当 真: # 无限条件循环
试:
输入值 是 转为整数(输入("请输入一个整数: "))
尝试次数 是 尝试次数 加 1
如果 输入值 等 目标:
打印("恭喜!你用了 " 加 转为文档(尝试次数) 加 " 次猜中。")
跳出 # 跳出循环
否则 如果 输入值 大 目标:
打印("大了,再小一点")
否则:
打印("小了,再大一点")
捕获 错误 为 异常信息:
如果 转字(异常信息) 包含 "无效":
打印("请输入有效的数字!")
否则:
抛出 异常信息 # 重新抛出
终:
打印("本轮猜测结束") # 无论是否异常都执行
※ 调用函数(带递归风险,但这里用循环)
猜数字游戏(随机.随机整数(-50, 50))
Implementation
Will be figured out by the author in the future.