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 (『』), and can store any number of Unicode characters (including 0, but it must be an integer). 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.

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 则
        返回 甲
    否则
        返回 最大公约数(乙, 甲 mod 乙)
    结束
结束函数

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' are the same.

Example

Guessing number

从 随机 引入 随机整数

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

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

Categories