X-script
X-script is designed by PSTF.
Language Overview
X-Script is a Turing-complete, efficient, lightweight and concise programming language. It combines the simplicity of Python, the power of JavaScript, the "hacking" of assembly language, and the efficiency of C++.
Basic Syntax Overview
Programming Mode
X-Script supports both imperative and scripted programming.
Imperative
The imperative mode just like you do Python in cmd.
X-Script 1.0 Line-V enviroment::2025-Jan-17, on Win32 > print("a") a >
Scripted
X-Script scripted programming must program in X-IDE.
The extension name of X-Script is ".xsc".
Comment
# I'm a one-line comment! /** I am a multi-line comment! */ /** * Document comment * often used on * module/library document, * just like in C++. */ /** /** It's illegal. */ */
Identifier
X-Script uses every identifier that:
- Not started with number.
- Not include special symbols.
- Not keyword.
It is important to note that X-Script supports all kinds of Latin, Greek, Cyrillic, even Japanese, Chinese, Korean, and all left-to-right scripts, but not historical scripts, such as Rune, Gothic, cuneiform, or right-to-left scripts such as Arabic, Hebrew, Syriac, etc.
Thus, a, _i, my_name, PRYSIGNETOFRY, number_2, 我的名字, 私の名前, are legal identifier, and while, $money, 42dk are illegal identifier.
At the same time, we stipulate that if a variable starts with two underscores and is all uppercase, then the variable is protected by X-Script.
Variable
By default, all variables are global (unless you've declared them local) and can be accessed casually, even to a non-existent variable, without compilation errors or resulting in traceback output, but with a null value - None.
Of course, if you have declared its type, its value will be the default.
Use this code to define an variable:
var (var_type) var_name (= var_value)
Data types
Currently, we have these types:
- Numbers, such as 3, 7, 3.14, -2, 114514, 1e20, 3.683E-34, 3.130582375184619046479671967486, etc. Initial value is 0 for int, and 0.0 for float.
- Strings, such as 'Hello' (or "Hello"). Strings can be enclosed in double quotes or single quotes, just like work in Python. Initial value is "".
- Booleans. It can only be True, or False. Initial value is False.
- Tables. This is the most important data type in X-Script, it can express a list or a dictionary, such as {1, 2, 3}, or {key = value}. Initial value is {}. Also, if you define a table as a list, then you use brackets instead of braces.
- Tuples. It is just a "unchangable table", such as (5, 2), (4, 8, 3, 8, 9), but (1, ) instead of (1). Initial value is (,).
- Functions. They're 1st-class citizens, which can be passed and returned as arguments.
- Empty types. They has only a value, None. They converts to 0, but they can used to be true. When they converted to string, they becomes "啊?".
- User-defined types. They're defined by "data" command.
- Undefined behaviour. This often occurs when you trying to do something incorrect, such as 5/0. They converts to 0, but they can used to be false. When they converted to string, they becomes 99 bottles of beer.
- Thread. It can fork a process out to do another program.
Special Assignment
X-Script also can assign many values to many variables.
When encountering an assignment statement, X-Script will evaluate all the values on the right before performing the assignment operation, so we can swap the values of the variables like this:
x = 6 y = 9 x, y = y, x
Index
X-Script uses subscript to index. However, -1 returns the first-to-last element, -2 is the second-to-last element, -3 is the third-to-last element, and so on.
Inputs and outputs
By default, X-Script outputs content to stdout and reads content from the stdin. You can specify the input and output files, and you can restore the handle by changing the file to "CON" (supposedly the Satanic username for Windows 98), outputting content to stdout and reading content from the stdin.
Use print() for output, and input() for input.
Loop
X-Script supports four kinds of loops.
Loop type | Description |
---|---|
for | This is an iteration loop that executes a statement once for each iteration object in the iteration container (e.g., an element in a list, a number in a range array). |
while | It's a conditional loop that does the same thing over and over again when the condition is true. Before each execution, it checks whether the return value of the condition is true. |
do ... while | It's a conditional loop that does the same thing over and over again when the condition is true. After each execution, it checks whether the return value of the condition is true. |
forever | It's an endless loop where it does the same thing forever until it's broken. |
You can control the loop by:
Statement | Description |
---|---|
break | It will exit the loop immediately. |
continue | It will jump to the next round of the loop. |
Conditional structure
X-Script uses the conditional jump just like Python, uses if-elif-else structure.
Functions
In X-Script, functions are the primary method of abstracting statements and expressions. It can be used to handle some special work, and it can also be used to calculate some values.
X-Script provides a number of built-in functions that you can easily call in your program, such as the print() function, which prints the parameters passed in to the console.
There are two main uses for X-Script functions:
- Complete the specified task, in which case the function is used as a call statement;
- Evaluates and returns a value, in which case the function is used as an expression for the assignment statement.
The difinition format is shown as below:
optional_function_scope function return_value_type function_name(argument1, argument2, argument3..., argumentn) function_body (return result_params_comma_separated) end
If you want to return nothing, you can omit the function type.
String
String can compare and do addition like string type in C++.
And, you can also put string in triple quote or sextuple quote, no escape needed because it becames a document string.
Error Handling
X-Script handles errors like in Python, but there is no ZeroDivisionError because it will causes an undefined behaviour.
OOP
X-Script is an Object-oriented programming language.
The definition of class
// Define a class and create an object class Person: var name: string var age: int __init__(string name, int age): this.name = name this.age = age def greet() -> string: return f"Hello, my name is {this.name} and I am {this.age} years old." var person = Person("Alice", 30) // "Spawn" a person print(person.greet()) // Output: Hello, my name is Alice and I am 30 years old.
Inheritance and polymorphism
Let's say we've defined the person class.
class Employee: Person: var employeeId: string __init__(string name, int age, string employeeId): super(name, age) this.employeeId = employeeId override def greet() -> string: return super.greet() + " My employee ID is " + this.employeeId + "." // Adding two strings is equivalent to joining the following string to the tail of the previous string. var employee = Employee("Bob", 40, "E12345") print(employee.greet()) // Output: Hello, my name is Bob and I am 40 years old. My employee ID is E12345.
Garbage collection
X-Script will automatically collect garbages(dead objects, such as a variable that no longer to use).
Advanced Syntax Overview
Lambda Expressions
X-Script uses lambda to create an anonymous function.
The format of lambda expression is like this:
lambda *args: return_value
Here is an usage of lambda expression.
# A+B Problem f = lambda a, b: (a + b) int a = parseInt(input()) int b = parseInt(input()) print(f(a, b))
Regular expressions
There is an library called re for regular expressions, just like work in Python.
Standard Libraries
Currently, X-Script supports these libraries:
math random re os sys internet time calendar logo cipher matrix kitten4 latex
All of them are like works in Python/C++/R-Language/JS/Java/Haskell/…… , but except these changes:
- You can generate a negative-timestamp, or overflowed-timestamp, such as -639075600 means 1949 January 1st, 15:00:00.
- Logo includes exec_logo(script) function, directly uses FMSLogo syntax to program.
- Kitten4 can directly work by text, just when you open this page by English. Use double-brace({{...}}) for statement block.
- LaTeX library supports output as formatted SVG-file, and directly calculate value of expressions.
- Socket and Request are combined to Internet.
- The Cipher library supports a variety of encryption systems, including the most basic Caesar cipher, the Vigerene cipher, or the more advanced Base64 cipher, MD5, SHA, etc.