Initial commit: Oraset programming language with documentation

This commit is contained in:
JGZSunShineMod1.0.3
2026-04-24 21:54:58 +08:00
commit 2089d28529
131 changed files with 28968 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
// 测试类定义
class Calculator {
def add(a, b) {
return a + b;
}
def subtract(a, b) {
return a - b;
}
def multiply(a, b) {
return a * b;
}
def divide(a, b) {
return a / b;
}
}
// 测试函数功能
def add(a, b) {
return a + b;
}
def subtract(a, b) {
return a - b;
}
def multiply(a, b) {
return a * b;
}
def divide(a, b) {
return a / b;
}
def calculate() {
result = add(10, 20);
show(`10 + 20 = `, result);
result = subtract(30, 10);
show(`30 - 10 = `, result);
result = multiply(5, 6);
show(`5 * 6 = `, result);
result = divide(20, 4);
show(`20 / 4 = `, result);
}
// 调用测试函数
calculate();
// 测试递归函数
def factorial(n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
result = factorial(5);
show(`Factorial of 5 is `, result);
+49
View File
@@ -0,0 +1,49 @@
// 测试条件语句
x = 10;
y = 20;
if (x < y) {
show(`x is less than y`);
} else {
show(`x is not less than y`);
}
// 测试else if
score = 85;
if (score >= 90) {
show(`Grade: A`);
} else if (score >= 80) {
show(`Grade: B`);
} else if (score >= 70) {
show(`Grade: C`);
} else {
show(`Grade: F`);
}
// 测试循环语句
i = 0;
while (i < 5) {
show(`i = `, i);
i += 1;
}
// 测试逻辑操作符
a = 1;
b = 0;
if (a == 1 && b == 0) {
show(`a is 1 and b is 0`);
}
if (a == 1 || b == 1) {
show(`Either a or b is 1`);
}
// 测试复合赋值操作符
count = 0;
count += 5;
show(`count after += 5: `, count);
count *= 2;
show(`count after *= 2: `, count);
+1
View File
@@ -0,0 +1 @@
input username "test"
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env python3
import sys
sys.path.insert(0, 'f:/Oraset')
from oraset import Lexer
source = '''input username "test"'''
lexer = Lexer(source)
tokens = []
while True:
tok = lexer.get_next_token()
tokens.append(tok)
print(f"Token: type={tok.type}, value={repr(tok.value)}, line={tok.line}, col={tok.column}")
if tok.type == 'EOF':
break
+27
View File
@@ -0,0 +1,27 @@
// 测试转义符
// 测试换行符
show(`Line 1\nLine 2`);
// 测试制表符
show(`Column 1\tColumn 2`);
// 测试回车符
show(`Hello\rWorld`);
// 测试反斜杠
show(`Path: C:\\Users\\Name`);
// 测试反引号
show(`Backtick: \``);
// 测试未知转义符
show(`Unknown escape: \x`);
// 测试转义符在字符串中的位置
message = `This is a\nmulti-line\nstring`;
show(message);
// 测试转义符与内置函数的结合
length = len(`Hello\nWorld`);
show(`Length of string with newline: `, length);
+50
View File
@@ -0,0 +1,50 @@
// 测试函数定义和调用
def add(a, b) {
return a + b;
}
result = add(10, 20);
show(`10 + 20 = `, result);
// 测试函数返回值
def multiply(a, b) {
product = a * b;
return product;
}
result = multiply(5, 6);
show(`5 * 6 = `, result);
// 测试类定义
class Calculator {
def add(a, b) {
return a + b;
}
def subtract(a, b) {
return a - b;
}
def multiply(a, b) {
return a * b;
}
def divide(a, b) {
return a / b;
}
}
// 测试类的使用(简化版,直接调用类方法)
// 注意:完整的类实例化和方法调用需要更多的实现
// 测试递归函数
def factorial(n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
result = factorial(5);
show(`Factorial of 5 is `, result);
+12
View File
@@ -0,0 +1,12 @@
!! 简单的Hello World程序
show(`Hello, Oraset!`);
!! 变量定义和算术运算
x = 10;
y = 20;
sum = x + y;
show(`Sum: `, sum);
!! 字符串拼接
message = `Hello, ` + `Oraset!`;
show(message);
+1
View File
@@ -0,0 +1 @@
eJzT11d4vq7hae/U57NaPFJzcvIVwvOLclKer+h+uqufqzgjv1wjASyuo+BflFicWqKYoGnNxaWvr/C0f8bL9v6n62Y92dn5dFLP83XTn81Z/2L/BCCDq0LBVsHQwJqrEkgbAeni0lwgq0JBW6HSGmpocGmulUKCjgJQCmbg2unP1yx7smPTs+49z/qWcuWmFhcnpqcCNcJckAA0IAHmDKhBUFVAMwAGg1K+
+63
View File
@@ -0,0 +1,63 @@
// 测试内置库函数
// 数学函数测试
show(`=== 数学函数测试 ===`);
abs_value = abs(-10);
show(`abs(-10) = `, abs_value);
max_value = max(10, 20);
show(`max(10, 20) = `, max_value);
min_value = min(10, 20);
show(`min(10, 20) = `, min_value);
pow_value = pow(2, 3);
show(`pow(2, 3) = `, pow_value);
sqrt_value = sqrt(16);
show(`sqrt(16) = `, sqrt_value);
// 字符串函数测试
show(`\n=== 字符串函数测试 ===`);
message = `Hello, Oraset!`;
length = len(message);
show(`len("Hello, Oraset!") = `, length);
upper_case = upper(message);
show(`upper("Hello, Oraset!") = `, upper_case);
lower_case = lower(message);
show(`lower("Hello, Oraset!") = `, lower_case);
sub_str = substring(message, 0, 5);
show(`substring("Hello, Oraset!", 0, 5) = `, sub_str);
// 其他函数测试
show(`\n=== 其他函数测试 ===`);
// 注意:input函数会等待用户输入,这里注释掉
// user_input = input(`请输入一个值: `);
// show(`你输入的值是: `, user_input);
int_value = int(`123`);
show(`int("123") = `, int_value);
str_value = str(456);
show(`str(456) = `, str_value);
// 测试函数组合
show(`\n=== 函数组合测试 ===`);
combined = upper(substring(`Hello, World!`, 0, 5));
show(`upper(substring("Hello, World!", 0, 5)) = `, combined);
// 测试用户定义函数与内置函数的结合
def calculate_area(radius) {
return pow(radius, 2) * 3; // 简化的圆面积计算
}
area = calculate_area(5);
show(`calculate_area(5) = `, area);
+11
View File
@@ -0,0 +1,11 @@
// 测试网络请求功能
// 测试HTTP GET请求
response = http_get(`http://example.com`);
show(`HTTP GET response length: `, len(response));
show(`First 100 characters: `, substring(response, 0, 100));
// 测试HTTP POST请求
post_data = `{"name": "Oraset", "version": "1.0"}`;
post_response = http_post(`http://httpbin.org/post`, post_data);
show(`\nHTTP POST response: `, post_response);
+17
View File
@@ -0,0 +1,17 @@
// 测试.oraset文件
show(`Hello from .oraset file!`);
// 变量定义
x = 10;
y = 20;
sum = x + y;
show(`10 + 20 = `, sum);
// 函数定义
def add(a, b) {
return a + b;
}
// 函数调用
result = add(5, 6);
show(`5 + 6 = `, result);
View File
+3
View File
@@ -0,0 +1,3 @@
!! 简单测试arr
arr1 = arr(`apple`);
show(arr1);
+67
View File
@@ -0,0 +1,67 @@
!! 全面测试数组功能
!! 基本数组操作
show(`=== 基本数组操作 ===`);
!! 创建数组
fruits = arr(`apple`, `banana`, `cherry`);
show(`创建数组: `, fruits);
show(`数组长度: `, len(fruits));
!! 数组索引访问
show(`第一个元素: `, fruits[0]);
show(`第二个元素: `, fruits[1]);
show(`第三个元素: `, fruits[2]);
!! 数组修改
fruits = push(fruits, `date`);
show(`添加元素后: `, fruits);
fruits = pop(fruits);
show(`删除元素后: `, fruits);
!! 数组搜索
show(`banana的索引: `, indexOf(fruits, `banana`));
show(`orange的索引: `, indexOf(fruits, `orange`));
!! 高级数组操作
show(`\n=== 高级数组操作 ===`);
!! 数组反转
reversed_fruits = reverse(fruits);
show(`反转数组: `, reversed_fruits);
!! 数组合并
show(`数组合并成字符串: `, join(fruits, `, `));
!! 数组排序
numbers = arr(`3`, `1`, `4`, `1`, `5`, `9`, `2`);
show(`原数字数组: `, numbers);
sorted_numbers = sort(numbers);
show(`排序后: `, sorted_numbers);
!! 数组切片
show(`切片 [0:2]: `, slice(fruits, `0`, `2`));
show(`切片 [1:3]: `, slice(fruits, `1`, `3`));
!! 数组嵌套
show(`\n=== 数组嵌套 ===`);
nested = arr(`a`, arr(`b`, `c`), `d`);
show(`嵌套数组: `, nested);
show(`嵌套数组长度: `, len(nested));
!! 数组与数学运算
show(`\n=== 数组与数学运算 ===`);
nums = arr(`10`, `20`, `30`);
show(`原数组: `, nums);
show(`第一个元素的平方根: `, math.sqrt(nums[0]));
show(`第二个元素的平方: `, math.pow(nums[1], `2`));
!! 数组遍历模拟
show(`\n=== 数组遍历模拟 ===`);
i = 0;
while (i < len(fruits)) {
show(`元素 `, i, `: `, fruits[i]);
i = i + 1;
}
+40
View File
@@ -0,0 +1,40 @@
!! 测试数组功能
!! 基本数组操作
show(`=== 测试基本数组操作 ===`);
arr1 = arr(`apple`, `banana`, `cherry`);
show(`初始数组: `, arr1);
show(`数组长度: `, len(arr1));
!! 数组操作
arr1 = push(arr1, `date`);
show(`添加元素后: `, arr1);
arr1 = pop(arr1);
show(`删除元素后: `, arr1);
show(`banana的索引: `, indexOf(arr1, `banana`));
!! 新添加的数组功能
show(`\n=== 测试新数组功能 ===`);
!! 数组反转
reversed_arr = reverse(arr1);
show(`反转数组: `, reversed_arr);
!! 数组合并
arr2 = arr(`1`, `2`, `3`);
merged_arr = push(push(push(arr1, arr2[0]), arr2[1]), arr2[2]);
show(`合并数组: `, merged_arr);
!! 数组排序
sorted_arr = sort(merged_arr);
show(`排序后: `, sorted_arr);
!! 数组切片
sliced_arr = slice(arr1, `0`, `2`);
show(`切片 [0:2]: `, sliced_arr);
!! 数组合并成字符串
joined_str = join(arr1, `, `);
show(`数组合并成字符串: `, joined_str);
+56
View File
@@ -0,0 +1,56 @@
!! 测试新的 array 库
!! 导入 array 库
@array;
show(`=== 测试 array 库函数 ===`);
!! 创建数组
fruits = arr(`apple`, `banana`, `cherry`);
show(`原数组: `, fruits);
!! array.length - 获取数组长度
show(`\n数组长度: `, array.length(fruits));
!! array.push - 添加元素到末尾
fruits = array.push(fruits, `date`);
show(`\n添加 'date' 后: `, fruits);
!! array.pop - 删除末尾元素
fruits = array.pop(fruits);
show(`删除末尾元素后: `, fruits);
!! array.unshift - 添加元素到开头
fruits = array.unshift(fruits, `grape`);
show(`\n添加 'grape' 到开头后: `, fruits);
!! array.shift - 删除开头元素
fruits = array.shift(fruits);
show(`删除开头元素后: `, fruits);
!! array.indexOf - 查找元素位置
show(`\n'banana' 的位置: `, array.indexOf(fruits, `banana`));
show(`'orange' 的位置: `, array.indexOf(fruits, `orange`));
!! array.lastIndexOf - 查找元素最后位置
numbers = arr(`1`, `2`, `3`, `2`, `1`);
show(`\n数字数组: `, numbers);
show(`'2' 最后出现的位置: `, array.lastIndexOf(numbers, `2`));
!! array.includes - 检查元素是否存在
show(`\n是否包含 'cherry': `, array.includes(fruits, `cherry`));
show(`是否包含 'orange': `, array.includes(fruits, `orange`));
!! array.join - 数组合并为字符串
show(`\n数组合并为字符串: `, array.join(fruits, `, `));
!! array.reverse - 反转数组
reversed = array.reverse(fruits);
show(`\n反转后: `, reversed);
!! array.sort - 排序数组
show(`\n排序后: `, array.sort(fruits));
!! array.slice - 截取数组
show(`\n截取 [0:2]: `, array.slice(fruits, `0`, `2`));
show(`截取 [1:3]: `, array.slice(fruits, `1`, `3`));
+13
View File
@@ -0,0 +1,13 @@
!! 测试bflang库的bf函数
show(`=== Testing bflang library ===`)
!! 测试Hello World Brainfuck代码
hello_world = `++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.`
result = bf(hello_world)
show(`Brainfuck Hello World: `, result)
!! 测试简单的Brainfuck代码
simple_code = `++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.`
result = bf(simple_code)
show(`Brainfuck simple code: `, result)
+11
View File
@@ -0,0 +1,11 @@
!! 测试bflang模块的导入和使用
!! 导入bflang模块
import bflang
show(`=== Testing bflang module ===`)
!! 测试Hello World Brainfuck代码
hello_world = `++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.`
result = bf(hello_world)
show(`Brainfuck Hello World: `, result)
+50
View File
@@ -0,0 +1,50 @@
!! 测试新的内置库函数
!! 测试文件操作函数
show(`=== Testing File Operations ===`)
!! 写入文件
result = file_write(`test_file.txt`, `Hello, Oraset!\nThis is a test file.`)
show(`file_write result: `, result)
!! 检查文件是否存在
exists = file_exists(`test_file.txt`)
show(`file_exists result: `, exists)
!! 读取文件内容
content = file_read(`test_file.txt`)
show(`file_read result: `, content)
!! 测试时间函数
show(`\n=== Testing Time Functions ===`)
!! 获取当前时间
current_time = time_now()
show(`Current time: `, current_time)
!! 测试随机数函数
show(`\n=== Testing Random Functions ===`)
!! 生成随机整数
random_num = random_int(1, 100)
show(`Random integer (1-100): `, random_num)
!! 生成随机浮点数
random_float_num = random_float()
show(`Random float (0-1): `, random_float_num)
!! 测试系统函数
show(`\n=== Testing System Functions ===`)
!! 执行系统命令
os_name = get_env(`OS`)
if (os_name == `Windows_NT`) {
system_output = system(`dir`)
} else {
system_output = system(`ls -la`)
}
show(`System command output: `, system_output)
!! 获取环境变量
path = get_env(`PATH`)
show(`PATH environment variable: `, substring(path, 0, 100), `...`)
+12
View File
@@ -0,0 +1,12 @@
// 测试错误显示功能
// 语法错误:缺少分号
x = 10
// 运行时错误:调用不存在的函数
unknown_function();
// 语法错误:括号不匹配
if (x > 5 {
show(`x is greater than 5`);
}
+7
View File
@@ -0,0 +1,7 @@
// 测试错误显示功能
// 语法错误:缺少分号
x = 10
// 运行时错误:调用不存在的函数
unknown_function();
+8
View File
@@ -0,0 +1,8 @@
!! 测试错误显示功能
!! 语法错误:缺少分号
x = 10
!! 这行应该正常执行
y = 20;
show(y);
+10
View File
@@ -0,0 +1,10 @@
!! 测试import语句导入bflang模块
!! 导入bflang模块
import bflang
show(`=== Testing import bflang ===`);
!! 测试Hello World Brainfuck代码
hello_world = `++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.`;
bf(hello_world);
+4
View File
@@ -0,0 +1,4 @@
!! 测试不导入库直接使用
!! 不导入math库直接使用math函数(应该报错)
show(`测试不导入直接使用: `, math.abs(`-123`));
+17
View File
@@ -0,0 +1,17 @@
!! 测试import语句导入pylang模块
!! 导入pylang模块
import pylang
show(`=== Testing import pylang ===`);
!! 测试简单的Python代码
python(`print('Hello from Python!')`);
!! 测试带有变量的Python代码
python(`
x = 10
y = 20
result = x + y
print(f'Sum: {result}')
`);
+32
View File
@@ -0,0 +1,32 @@
!! 测试新的导入语法
!! 测试1: 不导入库直接使用(应该报错)
!! show(`测试不导入直接使用: `, math.abs(`-123`));
!! 测试2: 使用新的 @ 语法导入库
@math;
@time;
@crypto;
!! 测试3: 导入后使用库函数
show(`=== 测试导入后的库函数 ===`);
show(`绝对值: `, math.abs(`-123`));
show(`平方根: `, math.sqrt(`16`));
show(`π值: `, math.pi());
show(`\n当前时间: `, time.now());
show(`格式化时间: `, time.format(time.now(), `%Y-%m-%d`));
show(`\nMD5加密: `, crypto.md5(`Hello`));
!! 测试4: 测试HTTP模块导入
@http;
show(`\n=== 测试HTTP模块 ===`);
!! 注意:这会实际发送HTTP请求
!! show(`HTTP GET: `, http_get(`https://api.github.com/users/octocat`));
!! 测试5: 测试旧的import语法(应该仍然兼容)
import json;
show(`\n=== 测试JSON模块 ===`);
json_str = `{"name": "Oraset"}`;
show(`解析JSON: `, json.parse(json_str));
+20
View File
@@ -0,0 +1,20 @@
// 测试加载includes文件夹中的插件
// 包含自定义的数学工具插件
!include 'math_utils'
// 测试插件中的函数
result1 = add(10, 20);
show(`10 + 20 = `, result1);
result2 = subtract(50, 30);
show(`50 - 30 = `, result2);
result3 = multiply(5, 6);
show(`5 * 6 = `, result3);
result4 = divide(100, 2);
show(`100 / 2 = `, result4);
result5 = factorial(5);
show(`5! = `, result5);
+10
View File
@@ -0,0 +1,10 @@
!! 测试@include指令导入bflang模块
!! 导入bflang模块
@include bflang
show(`=== Testing @include bflang ===`);
!! 测试Hello World Brainfuck代码
hello_world = `++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.`;
bf(hello_world);
+17
View File
@@ -0,0 +1,17 @@
!! 测试@include指令导入pylang模块
!! 导入pylang模块
@include pylang
show(`=== Testing @include pylang ===`);
!! 测试简单的Python代码
python(`print('Hello from Python!')`);
!! 测试带有变量的Python代码
python(`
x = 10
y = 20
result = x + y
print(f'Sum: {result}')
`);
+22
View File
@@ -0,0 +1,22 @@
!! 测试input和arr语法
!! 测试arr数组语法
show(`=== Testing arr() ===`);
arr1 = arr(`apple`, `banana`, `cherry`);
show(`Created array: `, arr1);
show(`Array length: `, len(arr1));
!! 测试数组操作
arr1 = push(arr1, `date`);
show(`After push('date'): `, arr1);
arr1 = pop(arr1);
show(`After pop(): `, arr1);
show(`indexOf('banana'): `, indexOf(arr1, `banana`));
!! 测试input语法(带提示)
show(`\n=== Testing input() ===`);
show(`Please enter your name:`);
name = input(`Name: `);
show(`Hello, `, name, `!`);
+2
View File
@@ -0,0 +1,2 @@
input username "请输入用户名: "
show(username)
+7
View File
@@ -0,0 +1,7 @@
// 测试没有包含response模块时使用HTTP请求函数
// 不包含response模块
// 尝试使用HTTP GET请求,应该会抛出错误
response = http_get(`http://example.com`);
show(`Response length: `, len(response));
+13
View File
@@ -0,0 +1,13 @@
!! 测试没有分号的情况
!! 没有分号的赋值语句(应该正常执行)
x = 10
!! 没有分号的赋值语句(应该正常执行)
y = 20
!! 没有分号的函数调用(应该正常执行)
show(x)
!! 没有分号的函数调用(应该正常执行)
show(y)
+24
View File
@@ -0,0 +1,24 @@
!! 测试pylang库的python函数
show(`=== Testing pylang library ===`)
!! 测试简单的Python代码
result = python(`print('Hello from Python!')`)
show(`Python code result: `, result)
!! 测试带有变量的Python代码
result = python(`
x = 10
y = 20
result = x + y
print(f'Sum: {result}')
`)
show(`Python code with variables result: `, result)
!! 测试更复杂的Python代码
result = python(`
import math
result = math.sqrt(16)
print(f'Square root of 16: {result}')
`)
show(`Python code with math result: `, result)
+19
View File
@@ -0,0 +1,19 @@
!! 测试pylang模块的导入和使用
!! 导入pylang模块
import pylang
show(`=== Testing pylang module ===`)
!! 测试简单的Python代码
result = python(`print('Hello from Python!')`)
show(`Python code result: `, result)
!! 测试带有变量的Python代码
result = python(`
x = 10
y = 20
result = x + y
print(f'Sum: {result}')
`)
show(`Python code with variables result: `, result)
+35
View File
@@ -0,0 +1,35 @@
!! 测试无分号和新注释格式
!! 单行注释测试
x = 10 !! 无分号赋值
!* 多行注释测试
这是多行注释的第一行
这是多行注释的第二行
*!
y = 20; !! 有分号赋值
show(x) !! 无分号函数调用
show(y); !! 有分号函数调用
!! 测试条件语句
if (x > 5) {
show(`x is greater than 5`)
} else {
show(`x is less than or equal to 5`)
}
!! 测试循环语句
i = 0
while (i < 5) {
show(`i = `, i)
i = i + 1
}
!! 测试函数定义
def add(a, b) {
return a + b
}
result = add(10, 20)
show(`10 + 20 = `, result)
+13
View File
@@ -0,0 +1,13 @@
!! 测试分号正确的情况
!! 有分号的赋值语句(应该正常执行)
x = 10
!! 有分号的赋值语句(应该正常执行)
y = 20
!! 有分号的函数调用(应该正常执行)
show(x)
!! 有分号的函数调用(应该正常执行)
show(y)
+13
View File
@@ -0,0 +1,13 @@
!! 测试分号必需的情况
!! 没有分号的赋值语句(应该报错)
x = 10
!! 有分号的赋值语句(应该正常执行)
y = 20;
!! 没有分号的函数调用(应该报错)
show(y)
!! 有分号的函数调用(应该正常执行)
show(x);
+4
View File
@@ -0,0 +1,4 @@
!! 测试不导入 string 库直接使用 split 函数
!! 不导入库直接使用(应该报错)
show(`不导入直接使用: `, string.split(`test`, `t`));
+99
View File
@@ -0,0 +1,99 @@
!! 测试扩展后的 string 库
!! 导入 string 库
@string;
show(`=== 测试 string 库函数 ===`);
!! string.upper - 转大写
text = `hello world`;
show(`原字符串: `, text);
show(`转大写: `, string.upper(text));
!! string.lower - 转小写
text2 = `HELLO WORLD`;
show(`\n原字符串: `, text2);
show(`转小写: `, string.lower(text2));
!! string.trim - 去空格
text3 = ` hello world `;
show(`\n原字符串(带空格): '`, text3, `'`);
show(`去空格后: '`, string.trim(text3), `'`);
!! string.replace - 替换
text4 = `hello world`;
show(`\n原字符串: `, text4);
show(`替换 'world' 为 'oraset': `, string.replace(text4, `world`, `oraset`));
!! string.startsWith - 是否以某字符串开头
text5 = `hello world`;
show(`\n原字符串: `, text5);
show(`是否以 'hello' 开头: `, string.startsWith(text5, `hello`));
show(`是否以 'world' 开头: `, string.startsWith(text5, `world`));
!! string.endsWith - 是否以某字符串结尾
text6 = `hello world`;
show(`\n原字符串: `, text6);
show(`是否以 'world' 结尾: `, string.endsWith(text6, `world`));
show(`是否以 'hello' 结尾: `, string.endsWith(text6, `hello`));
!! string.substring - 截取子串
text7 = `hello world`;
show(`\n原字符串: `, text7);
show(`截取 [0:5]: `, string.substring(text7, `0`, `5`));
show(`截取 [6:11]: `, string.substring(text7, `6`, `11`));
!! string.indexOf - 查找子串位置
text8 = `hello world`;
show(`\n原字符串: `, text8);
show(`'world' 的位置: `, string.indexOf(text8, `world`));
show(`'test' 的位置: `, string.indexOf(text8, `test`));
!! string.length - 获取字符串长度
text9 = `hello world`;
show(`\n原字符串: `, text9);
show(`字符串长度: `, string.length(text9));
!! string.contains - 是否包含子串
text10 = `hello world`;
show(`\n原字符串: `, text10);
show(`是否包含 'world': `, string.contains(text10, `world`));
show(`是否包含 'test': `, string.contains(text10, `test`));
!! string.concat - 字符串连接
text11 = `hello`;
text12 = `world`;
show(`\n字符串1: `, text11);
show(`字符串2: `, text12);
show(`连接结果: `, string.concat(text11, text12));
!! string.repeat - 重复字符串
text13 = `ab`;
show(`\n原字符串: `, text13);
show(`重复3次: `, string.repeat(text13, `3`));
!! string.reverse - 反转字符串
text14 = `hello`;
show(`\n原字符串: `, text14);
show(`反转后: `, string.reverse(text14));
!! string.includes - 是否包含
text15 = `hello world`;
show(`\n原字符串: `, text15);
show(`是否包含 'world': `, string.includes(text15, `world`));
show(`是否包含 'test': `, string.includes(text15, `test`));
!! string.lastIndexOf - 最后一次出现的位置
text16 = `hello world hello`;
show(`\n原字符串: `, text16);
show(`'hello' 最后出现的位置: `, string.lastIndexOf(text16, `hello`));
!! string.padStart - 左侧填充
text17 = `5`;
show(`\n原字符串: `, text17);
show(`左侧填充到长度5(用'0'): `, string.padStart(text17, `5`, `0`));
!! string.padEnd - 右侧填充
text18 = `5`;
show(`\n原字符串: `, text18);
show(`右侧填充到长度5(用'*'): `, string.padEnd(text18, `5`, `*`));
+37
View File
@@ -0,0 +1,37 @@
!! 测试新添加的实用功能
!! 测试字符串函数
show(`=== Testing string functions ===`);
str = `Hello, Oraset!`;
show(`Original string: `, str);
show(`startsWith('Hello'): `, startsWith(str, `Hello`));
show(`endsWith('World'): `, endsWith(str, `World`));
show(`endsWith('Oraset!'): `, endsWith(str, `Oraset!`));
show(`replace('Oraset', 'World'): `, replace(str, `Oraset`, `World`));
show(`split(', '): `, split(str, `, `));
show(`trim(' Hello '): `, trim(` Hello `));
!! 测试数组函数
show(`\n=== Testing array functions ===`);
arr = array(`a`, `b`, `c`);
show(`Original array: `, arr);
arr = push(arr, `d`);
show(`After push('d'): `, arr);
arr = pop(arr);
show(`After pop(): `, arr);
show(`indexOf('b'): `, indexOf(arr, `b`));
show(`indexOf('x'): `, indexOf(arr, `x`));
!! 测试类型检查函数
show(`\n=== Testing type check functions ===`);
show(`is_number('123'): `, is_number(`123`));
show(`is_number('abc'): `, is_number(`abc`));
show(`is_number('123.45'): `, is_number(`123.45`));
show(`is_string('Hello'): `, is_string(`Hello`));
show(`is_string(123): `, is_string(`123`));
+49
View File
@@ -0,0 +1,49 @@
!! 测试实用库功能
!! 数学库测试
show(`=== 数学库测试 ===`);
show(`绝对值: `, math.abs(`-123`));
show(`平方根: `, math.sqrt(`16`));
show(`幂运算: `, math.pow(`2`, `3`));
show(`正弦值: `, math.sin(`90`));
show(`余弦值: `, math.cos(`0`));
show(`π值: `, math.pi());
show(`e值: `, math.e());
show(`最大值: `, math.max(`10`, `20`, `5`));
show(`最小值: `, math.min(`10`, `20`, `5`));
show(`随机数: `, math.random());
show(`随机整数: `, math.randint(`1`, `100`));
!! 日期时间库测试
show(`\n=== 日期时间库测试 ===`);
timestamp = time.now();
show(`当前时间戳: `, timestamp);
show(`格式化时间: `, time.format(timestamp, `%Y-%m-%d %H:%M:%S`));
!! JSON库测试
show(`\n=== JSON库测试 ===`);
json_str = `{"name": "Oraset", "version": "1.0"}`;
parsed = json.parse(json_str);
show(`解析JSON: `, parsed);
obj = `{name: "Test", value: 123}`;
stringified = json.stringify(obj);
show(`字符串化: `, stringified);
!! 加密库测试
show(`\n=== 加密库测试 ===`);
text = `Hello, Oraset!`;
show(`MD5加密: `, crypto.md5(text));
show(`SHA1加密: `, crypto.sha1(text));
show(`SHA256加密: `, crypto.sha256(text));
!! 数组功能测试
show(`\n=== 数组功能测试 ===`);
arr = arr(`a`, `b`, `c`);
show(`原数组: `, arr);
show(`数组长度: `, len(arr));
show(`数组反转: `, reverse(arr));
show(`数组合并: `, join(arr, `-`));
!! 系统功能测试
show(`\n=== 系统功能测试 ===`);
show(`当前目录: `, system(`pwd`));
+36
View File
@@ -0,0 +1,36 @@
!! 测试新的 util 库
!! 导入 util 库
@util;
show(`=== 测试 util 库函数 ===`);
!! 随机数函数
show(`\n随机数: `, util.random());
show(`随机整数 (1-100): `, util.randint(`1`, `100`));
!! 数学函数
show(`\n绝对值: `, util.abs(`-123`));
show(`四舍五入: `, util.round(`3.14159`, `2`));
show(`最小值: `, util.min(`10`, `5`, `20`));
show(`最大值: `, util.max(`10`, `5`, `20`));
show(`求和: `, util.sum(`1`, `2`, `3`, `4`, `5`));
show(`平均值: `, util.average(`10`, `20`, `30`));
!! 类型检查函数
show(`\n类型检查:`);
show(`是否是数字: `, util.isNumber(`123`));
show(`是否是数字: `, util.isNumber(`abc`));
show(`是否是字符串: `, util.isString(`hello`));
show(`是否是数组: `, util.isArray(`[1,2,3]`));
!! 类型转换函数
show(`\n类型转换:`);
show(`转为数字: `, util.toNumber(`123`));
show(`转为字符串: `, util.toString(`123`));
!! 类型判断函数
show(`\n类型判断:`);
show(`类型: `, util.typeof(`123`));
show(`类型: `, util.typeof(`hello`));
show(`类型: `, util.typeof(`[1,2,3]`));
+14
View File
@@ -0,0 +1,14 @@
// 测试包含response模块后使用HTTP请求函数
// 包含response模块
!include 'response'
// 测试HTTP GET请求
response = http_get(`http://example.com`);
show(`HTTP GET response length: `, len(response));
show(`First 100 characters: `, substring(response, 0, 100));
// 测试HTTP POST请求
post_data = `{"name": "Oraset", "version": "1.0"}`;
post_response = http_post(`http://httpbin.org/post`, post_data);
show(`\nHTTP POST response: `, post_response);