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.

XYScript

From Esolang
Jump to navigation Jump to search

XYScript is designed by PSTF. In timeline 284436, it was created by Fang Chengyi, Chen Xingqi, Zheng Xiang, Ouyang Kuan, and Pierrè Aveuçevre.

It was completely written in Chinese. XY stands for 轩辕.

This is still a work in progress. It may be changed in the future.

Overview

Data and Identifier

All the data in XYScript was stored in Variables.

This is how we define a variable.

令 变量名称@变量类型 为 值

You can omit one of value or type, but not both.

Identifier

All identifiers must follow the following rules:

  1. They must follow the following principle: start with an ID_First character, followed by any number of ID_Middle characters.
  2. Since this programming language is almost entirely written in Chinese, it's preferable to use Chinese for identifiers as well. However, this is not a strict requirement. If you insist on using English, the compiler won't be able to stop you.
  3. Special characters are not allowed, but characters from other languages are permitted.
  4. If two identifiers are equal after NFC normalization, then these two identifiers are considered equal. For example, '⿉' and '黍' are equivalent.
    • It should be noted that “ℛequⅇst” and “Request” are two different identifiers, because identifiers are considered equal only after NFC normalization, not NFKC normalization. So don't try to define your identifiers using Martian script.
  5. Do not use a single underscore as an identifier (although it is allowed) because it represents a 'wildcard' and can be very useful in loops. However, two consecutive underscores are allowed.

Types

Currently, we have these types:

  1. Integers (整数). An integer is a series of numbers without a decimal point. For example: 3, 5, and -7 are all integers.
  2. Real numbers (实数). Real numbers refer to numbers that can be represented using decimals (whether infinite decimals or finite decimals), including rational and irrational numbers. For example, 5/3(3分之5), 0.8, 91.78, 1.111111111111111111..., and pi(数学.圆周率) are all real numbers.
  3. Inscriptions (铭文). Inscriptions are enclosed in double quotes or double corner quotes (『』), or maybe single quotes or single corner quotes (「」), and can store any number of Unicode characters (including 0, but it must be a natural number). Unicode has now reached version 17. In the XYScript environment, Unicode encoding is always synchronized with the latest version. Such as, 『Hello, World!』 is equal to “Hello, World!”.
  4. List (列表). A list is enclosed in square brackets(【】), with elements separated by commas(、). It can store elements of any type and any length.
  5. Tuple (元组). It is roughly equivalent to an immutable list, but is enclosed in parentheses. If there is only one element, it must be represented as (a、). An empty tuple is represented as (、).
  6. Dictionary (字典). A dictionary is enclosed in turtle shell-shaped brackets (〔〕) and stores key-value pairs (in the format “key”: value).
  7. Yao (爻). Yao is equivalent to a Boolean value, with two values: 阳 and 阴, which correspond to true and false. Inspired by wenyan.
  8. NULL (空白). Just nothing.
  9. 任意. Can be any type.

In version 1.1, we added complex number (复数) which defined as what in mathematics.

Expressions

Conditional Jump

This is a simple example of conditional expression:

若 分数 大于等于 80 则
    输出(“良好”)
否则若 分数 大于等于 60 则
    输出(“及格”)
否则
    输出(“不及格”)
结束

You can omit the 'else if' blocks (there can be multiple) or the 'else' block (there can only be one), but you cannot omit the 'if' block.

Conditional Loop

This is a simple example of conditional loop:

令 计数器@整数 为 0
当 计数器 小于 10 循环
    输出(“当前计数:” 加 转为铭文(计数器))
    计数器 为 计数器 加 1
结束循环

THere is also another type of conditional loop, like do-while in C++ but instead by do-until.

令 计数器@整数 为 0
循环
    输出(“当前计数:” 加 转为铭文(计数器))
    计数器 为 计数器 加 1
直到 计数器 大于等于 10

Iterative Loop

We may also write iterative loop, very useful when we CAN determine the times of looping.

For two examples,

对于 数 在 范围(0, 10) 循环
    输出(数)
结束循环

令 我的列表@列表 为 【1、2、3、4、5】
对于 元素 在 我的列表 循环
    输出(元素)
结束循环

Then we can have this:

令 我的列表(列表) 为 【(数 幂 2) 对于 数 在 范围(0, 10)】
输出(我的列表)

Note: Half-width symbols (especially parentheses) and full-width symbols are treated the same by the compiler, unless you put them into a inscription literal.

Functions

This language supports functions, including R&R.

For example, these two functions calculates x! and gcd(x, y).

函数 阶乘(数)
    若 数 小于等于 1 则
        返回 1
    否则
        返回 数 乘 阶乘(数 - 1)
    结束
结束函数

函数 最大公约数(甲, 乙)
    若 乙 等于 0 则
        返回 甲
    否则
        返回 最大公约数(乙, 甲 取模 乙)
    结束
结束函数

And even mapping. This example will map the first 10 natural numbers to perfect squares from 12 to 102 using a mapping function.

函数 映射(函数, 列表)
    令 结果 为 【】
    对于 元素 在 列表 循环
        令 新元素 为 函数(元素)
        添加 新元素 到 结果
    结束循环
    返回 结果
结束函数

函数 平方(数)
    返回 数 乘 数
结束函数

令 原列表 为 【1、2、3、4、5、6、7、8、9、10】
令 平方列表 为 映射(平方, 原列表)

By the way...

There are some synonyms in XYScript, for example, 'a 乘 b' and 'a × b', or 'a 加 b' and 'a + b', and so on, are the same.

Example

Project Euler

7

引入 数学
引入 内核

函数 质数序号(n) -> 整数:
    〔返回第 n 个质数。〕
    若 n 小于等于 0 则
        抛出(值错误,“传入的值必须是正整数哦。”)
    否则若 n 等于 1 则
        返回 2
    否则若 n 等于 2 则
        返回 3
    结束
    # 此处将会预估一个质数上限。对于大于等于 6 的 n ,
    # 安全的上限应该是 n(log(n)+log(log(n))+10 ,其中 log 意为自然对数。
    若 n 大于等于 6 则
        令 n的对数 为 数学.对数(n)
        令 n的双重对数 为 数学.对数(n的对数)
        令 上限 为 n 乘 (n的对数 加 n的双重对数)加 10
    否则
        令 上限 为 15
        # 对于小于等于 5 的 n 来说,15 已经足够了,毕竟第五个质数是 13。
    结束
    # 埃拉托色尼质数筛选法
    令 筛子 为 【阳】 乘 (上限 加 1)
    令 筛子【0:2】 为 阴 # 0 和 1 不是质数
    对于 馕 在 范围(2, 转为整数(上限 幂 0.5)加 1) 循环
        如果 筛子【馕】 则
            令 爪巴 为 馕
            令 起点 为 馕
            令【起点:上限 加 1:爪巴】 为 阴
        结束
    结束循环
    # 解压出质数列表然后返回
    令 计数器 为 0
    对于 劈 在 范围(2, 上限 加 1) 循环
        如果 筛子【劈】 则
            令 计数器 为 计数器 加 1
            如果 计数器 等于 n 则:
                返回 劈
            结束
        结束
    结束循环
    # 如果上限过低(在上面的假设中这个现象应该不会发生),则回滚到最
    # 原始的方法——因子检测法。如果这真的被调用到,那么就说明你已经有
    # 买彩票每次都中头奖的手气了。
    令 挑战者 为 上限 加 1
    当 阳 循环
        令 我是质数 为 阳
        对于 撅 在 范围(2, 转为整数(上限 幂 0.5)加 1) 循环
            如果 挑战者 取模 撅 等于 0 则
                令 我是质数 为 阴
                跳出循环
            结束
        结束循环
        如果 我是质数 则
            令 计数器 为 计数器 加 1
            如果 计数器 等于 n 则:
                返回 挑战者 # 你过关!
            结束
        结束
        令 挑战者 为 挑战者 加 1
    结束循环
结束函数

函数 主函数():
    令 叉 为 质数序号(10001)
    输出(叉)
结束函数

若 __当前环境__ 等于 “__主程序__” 则
    主函数()
结束

Guessing number

从 随机 引入 随机整数

函数 猜数字游戏()
    令 目标数 为 随机整数(1, 100)
    令 尝试次数 为 0
    令 已猜中 为 阴
    
    当 非 已猜中 循环
        令 猜测 为 转为整数(输入("猜一个1-100之间的数字:"))
        令 尝试次数 为 尝试次数 加 1
        
        若 猜测 < 目标数 则
            输出(“太小了!再试一次吧。”)
        否则若 猜测 > 目标数 则
            输出(“太大了!再试一次吧。”)
        否则
            输出(“恭喜!你用了” 加 转为铭文(尝试次数) 加 “次猜中!”)
            令 已猜中 为 阳
        结束
    结束循环
结束函数

若 __当前环境__ 等于 “__主程序__” 则
    猜数字游戏()
结束

ASCII Password generator

从 随机 引入 选择
从 字符串操作 引入 大写字母、小写字母、数字、ASCII符号

令 数字启用 为 阳 若 输入(“是否需要数字(Y/N):”) = “Y”否则 阴
令 小写字母启用 为 阳 若 输入(“是否需要小写字母(Y/N):”) = “Y”否则 阴
令 大写字母启用 为 阳 若 输入(“是否需要大写字母(Y/N):”) = “Y”否则 阴
令 特殊符号启用 为 阳 若 输入(“是否需要特殊符号(Y/N):”) = “Y”否则 阴
令 密码长度@整数 为 转为整数(输入(“请键入密码长度(必须大于0):”))
令 密码@铭文 为 “”
对于 数 在 范围(密码长度) 循环
     密码.添加末尾(选择((大写字母 若 大写字母启用 否则 虚空) 加 (
                            小写字母 若 小写字母启用 否则 虚空) 加 (
                            数字 若 数字启用 否则 虚空) 加 (
                            特殊符号 若 特殊符号启用 否则 虚空)))
输出(密码)

Categories