XLang - Lightweight programming language
Project description
X Lang
介绍
X Lang 是一种轻量级、动态类型的脚本语言,结合了函数式和面向对象编程范式的特点。它的设计目标是提供最简单抽象的语法去实现最复杂的功能。
语言特性
变量类型
- 变量类型是动态的,不需要显式声明变量类型
- 变量可以是任意类型
- 变量有 INT、FLOAT、BOOL、STRING、NoneType、TUPLE、KEYVALUE、Lambda、BUILTIN、NAMED 类型
注意:
- 元组在构建时会遍历所有元素,如果元素是Lambda类型,会将元组自身引用传递给Lambda的caller(self),这样Lambda就可以访问元组的成员
- xxx.'name' 语法是一个组合语法,如果xxx是元组,会遍历元组的所有元素并对元素执行键值比较,也就是说当且仅当元组元素是键值对且键等于'name'时才返回该元素的值。如果 name 被AST解析成变量,会强制替换成字符串以适配更简略的语法
基本语法
- 使用
:=进行变量定义,使用=进行变量赋值 - 语句使用分号
;分隔 - 支持单行注释
//和多行注释/* */
数据类型
- 基本类型:整数、浮点数、布尔值、字符串、空值(
null) - 复合类型:元组、键值对
函数和闭包
- 函数定义语法:
(参数) -> { 函数体 } - 严格要求命名参数:
'参数名': 默认值 - 支持闭包:函数可以捕获外部变量
- 函数和对象方法使用
return返回值
对象系统
- 使用元组和键值对实现类似对象的结构
- 方法中可以通过
self访问对象成员 - 使用点操作符访问对象的属性和方法
控制流
- 条件语句:
if 条件 语句 else 语句或if 条件 语句 - 循环语句:
while 条件 语句
也就是说,以下代码是合法的:
if (true) {
print("Hello, world!");
}
A := if true 1 else 0;
while (A < 10) {
A = A + 1;
}
if (operator == "+") { // else if组合式语法
...
} else if (operator == "-") {
...
} else if (operator == "*") {
...
} else if (operator == "/") {
...
} else {
...
}
字符串
- 支持多种字符串表示形式:单引号、双引号、三引号
- 支持字符串转义序列
示例代码
简单函数
add := (a => 0, b => 0) -> {
return a + b;
};
print(add(5, 3)); // 输出: 8
对象和方法
person := (
'name': 'Alice',
'greet': (message => 'Hello') -> {
return message + ", " + self.name + "!";
}
);
print(person.greet()); // 输出: Hello, Alice!
method := person.greet;
print(method()); // 输出: Hello, Alice!
method := person.greet 是一个极其重要的特性,它允许我们将对象的方法赋值给一个变量,然后像普通函数一样调用,而且仍然可以访问对象的成员。
闭包和状态
createCounter := () -> {
count := 0;
return (count => count) -> { // 返回闭包
count = count + 1;
return count;
};
};
counter := createCounter();
print(counter()); // 输出: 1
print(counter()); // 输出: 2
引用和解引用
使用 ref 和 deref 关键字进行引用和解引用操作:
a := 1;
b := ref a;
print(deref b); // 输出: 1
内置函数
X Lang 提供了多种内置函数:
print- 打印到控制台input- 从控制台读取输入len- 获取元组或字符串的长度type- 获取对象的类型int,float,str,bool- 类型转换range- 创建数值范围sum,min,max- 聚合操作slice- 元组或字符串切片操作repr- 获取对象的字符串表示
函数式
// Z组合子的实现
// Z组合子允许我们创建匿名递归函数,不需要提前命名函数
Z := (f => (x => null) -> { return x(x); }) -> {
return f((x => null, f => f) -> {
return f(Z(f))(x);
});
};
// 使用Z组合子实现阶乘函数
factorial := Z((f => null) -> {
return (n => 0, f => f) -> {
if (n <= 1) {
return 1;
} else {
return n * f(n - 1);
};
};
});
print(factorial(5));
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
xlang_script-0.1.0.post2.tar.gz
(33.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xlang_script-0.1.0.post2.tar.gz.
File metadata
- Download URL: xlang_script-0.1.0.post2.tar.gz
- Upload date:
- Size: 33.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e5135eed84240c5acc473c4b556055228b84a2ad9dcf40e5e42e9107186e554
|
|
| MD5 |
c31b5d4a8d42258cb6c8b0e2d96fae06
|
|
| BLAKE2b-256 |
160b4accdd5afd951f694a4c6506decd58b713f95d3b1bd9bf2bb67421d7dbb2
|
File details
Details for the file xlang_script-0.1.0.post2-py3-none-any.whl.
File metadata
- Download URL: xlang_script-0.1.0.post2-py3-none-any.whl
- Upload date:
- Size: 33.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6c5d7d00e005e623b7f718f53e274df5fdb0ab7643ca9fca451d013da311a86
|
|
| MD5 |
5061bca96404da25b72d5b373e710de5
|
|
| BLAKE2b-256 |
0b8ca4e82ffca0ce76ff9873d47136aa25fdb0a4157c6924a1d95399042a2885
|