2020-11-10 22:06:48 +08:00
|
|
|
|
# fscript 脚本引擎
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
## 1. 介绍
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
fscript 是一个极简的脚本引擎,借鉴了函数语言中一些思路,主要用于低端嵌入式系统,让用户轻松扩展现有系统,而不需要重新编译和下载固件。
|
|
|
|
|
|
|
|
|
|
* 特色:
|
2020-11-13 17:57:06 +08:00
|
|
|
|
* 小内存。最低开销小于 400 字节。
|
2021-01-09 17:48:48 +08:00
|
|
|
|
* 小巧。核心代码 1000 行,基本扩展函数 800 行。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
* 灵活。支持多条语句、函数嵌套调用和变量定义。
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 简单。熟悉任何一种编程语言的人,5 分钟内即可学会。
|
2021-01-09 17:48:48 +08:00
|
|
|
|
* 强大。超过 60 个内置函数,支持类似 C 语言的条件语句、循环语句和注释方式,支持复杂的表达式,支持用 C 语言扩展函数。
|
2020-11-13 17:57:06 +08:00
|
|
|
|
|
|
|
|
|
> keil -O1 编译结果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Code (inc. data) RO Data RW Data ZI Data Debug Object Name
|
2021-01-09 23:19:05 +08:00
|
|
|
|
10380 1580 1909 4 0 86893 fscript.o
|
2020-11-13 17:57:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
* 限制:
|
2020-11-13 08:57:05 +08:00
|
|
|
|
* 不支持函数定义。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
## 2. 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
print("hello fscript")
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 在 PC 上测试运行:
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
如:
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
./bin/runFScript 'print("hello fscript")'
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 在 PC 上测试运行指定文件:
|
|
|
|
|
|
|
|
|
|
> 第一个命令行参数以 @ 开头表示它是一个脚本文件,读取其内容执行。
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
```
|
2021-01-10 09:58:41 +08:00
|
|
|
|
./bin/runFScript @tests/fscripts/demo_while1.fs
|
2021-01-02 12:36:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
## 3. 语法
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
### 数据类型
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
* 浮点数类型 (double)。
|
|
|
|
|
* 整数类型 (int32)。支持十进制、二进制和十六进制。
|
|
|
|
|
* 字符串类型 (string)。UTF-8 字符串,用英文双引号扩起来。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
* 布尔类型 (bool)。标准取值为:true 和 false,非 0 的数值视为 true。
|
2021-01-10 09:58:41 +08:00
|
|
|
|
* 可以通过扩展实现混合数据类型的数组、固定类型的数组和 map。
|
|
|
|
|
* 可以通过类型转换函数得到各种基本类型。如 int8/int16/int32/int64/uint8/uint16/uint32/uintg64/float/double。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-07 11:34:05 +08:00
|
|
|
|
### 注释
|
|
|
|
|
|
|
|
|
|
格式:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
//line comment
|
|
|
|
|
/*block comment*/
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
//this is line comment
|
|
|
|
|
/*this is block comment*/
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
### 函数调用
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(0xff)
|
|
|
|
|
print(100)
|
|
|
|
|
print(0b1010)
|
|
|
|
|
print("hello fscript")
|
|
|
|
|
print("hello", 123)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 函数之间可以用空格、换行和英文分号分隔。
|
|
|
|
|
|
|
|
|
|
### 定义变量
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 通过 set 函数设置
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
|
|
|
|
set(a, 123)
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 通过=赋值
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
a = 123
|
|
|
|
|
b = "abc"
|
|
|
|
|
c = true
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 变量名命名规则:
|
|
|
|
|
|
|
|
|
|
* 以字母开头,后面可用数字、英文下划线和英文点。
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
age=123
|
|
|
|
|
name="fscript"
|
|
|
|
|
file_name="test.txt"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 英文点"."可以用来访问对象的成员,如果对应的对象不存在则当作普通变量。
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
msg.payload = "hello"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
>在这里,如果对象 msg 存在,msg.payload 为 msg 对象的 payload 成员,否则 msg.payload 则是一个普通变量。
|
|
|
|
|
|
|
|
|
|
* a、b、c 和 d 为快速访问变量,具有更快的访问速度,建议在循环中优先使用。
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
a=0
|
|
|
|
|
b=0
|
|
|
|
|
while(a < 100) {
|
|
|
|
|
a=a+1
|
|
|
|
|
b=b+a
|
|
|
|
|
print(a, b)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
### 获取变量
|
|
|
|
|
|
|
|
|
|
```
|
2020-11-12 15:04:36 +08:00
|
|
|
|
a
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-06 11:41:52 +08:00
|
|
|
|
> 获取变量时,如果变量不存在,自动当成字符串处理。不希望当成字符串,可以加上$前缀。
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
如:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
./bin/runFScript 'print($abc)'
|
|
|
|
|
|
|
|
|
|
get var abc failed
|
|
|
|
|
(null)
|
|
|
|
|
result:true
|
|
|
|
|
cost: 112 us
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
./bin/runFScript 'print(abc)'
|
|
|
|
|
|
|
|
|
|
get var abc failed
|
|
|
|
|
abc
|
|
|
|
|
result:true
|
|
|
|
|
cost: 112 us
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
### 函数嵌套调用
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(join(",", 1, 2, 3))
|
2021-01-02 12:36:30 +08:00
|
|
|
|
print(join(",",1,2,3,4), join(";",5,6,7,8))
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-19 10:12:15 +08:00
|
|
|
|
### 提前返回(return)
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
var a = 1;
|
|
|
|
|
if(a == 1) {
|
|
|
|
|
print("return");
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
print("end");
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
### 条件执行
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 语句方式
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
2021-01-02 12:36:30 +08:00
|
|
|
|
a=random(1, 100)
|
|
|
|
|
b=random(1, 100)
|
|
|
|
|
|
|
|
|
|
print("a is ", a);
|
|
|
|
|
print("b is ", b);
|
|
|
|
|
|
|
|
|
|
if(a < b) {
|
|
|
|
|
print(a, "<", b)
|
|
|
|
|
} else {
|
|
|
|
|
print(a, ">=", b)
|
|
|
|
|
}
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-19 09:37:34 +08:00
|
|
|
|
else if
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
var b = "";
|
|
|
|
|
|
|
|
|
|
if(a == 1) {
|
|
|
|
|
b = "a"
|
|
|
|
|
} else if(a == 2) {
|
|
|
|
|
b = "b"
|
|
|
|
|
} else if(a == 3) {
|
|
|
|
|
b = "c"
|
|
|
|
|
} else if(a == 4) {
|
|
|
|
|
b = "d"
|
|
|
|
|
} else {
|
|
|
|
|
b = "other"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
> 代码块需要用 {} 扩起来,else 语句可以省略。
|
|
|
|
|
|
|
|
|
|
* 函数方式
|
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
|
|
|
|
if(true, print("a"), print("b"))
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-23 16:10:23 +08:00
|
|
|
|
### 循环执行
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 语句方式
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
a=0
|
|
|
|
|
b=0
|
|
|
|
|
|
|
|
|
|
while(a < 100) {
|
|
|
|
|
a=a+1
|
|
|
|
|
b=b+a
|
|
|
|
|
print(a, b)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 代码块需要用 {} 扩起来
|
|
|
|
|
|
|
|
|
|
* 函数方式
|
|
|
|
|
|
2020-11-23 16:10:23 +08:00
|
|
|
|
```
|
2021-01-02 12:36:30 +08:00
|
|
|
|
a=0
|
|
|
|
|
while(a < 10, print(a), a=a+1)
|
2020-11-23 16:10:23 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-29 10:20:50 +08:00
|
|
|
|
### 表达式
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
为了确保优先级正确,尽量用括号明确优先级。
|
|
|
|
|
|
2020-11-29 10:55:12 +08:00
|
|
|
|
#### 支持的操作符
|
|
|
|
|
|
|
|
|
|
* 四则运算:+ - * / %
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 逻辑运算:|| && !
|
2020-11-29 10:55:12 +08:00
|
|
|
|
* 位运算:| & ~
|
|
|
|
|
* 比较运算:< <= > >= ==
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 条件运算:(p) ? a : b
|
2020-11-29 10:55:12 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
> 对于加法运算 (+),参数中有字符串时,进行字符串连接。
|
2020-11-29 10:55:12 +08:00
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
2021-01-02 12:36:30 +08:00
|
|
|
|
a = 10
|
|
|
|
|
b = 20
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
a<b
|
|
|
|
|
1+2
|
|
|
|
|
1-2
|
|
|
|
|
1*2
|
|
|
|
|
1/2
|
|
|
|
|
3%2
|
|
|
|
|
a||b
|
|
|
|
|
a&&b
|
|
|
|
|
a|b
|
|
|
|
|
a&b
|
|
|
|
|
!a
|
|
|
|
|
~a
|
|
|
|
|
a<b
|
|
|
|
|
a<=b
|
|
|
|
|
a>b
|
|
|
|
|
a>=b
|
|
|
|
|
(1+2)*(2+3)
|
|
|
|
|
sin(a) + sin(b)
|
|
|
|
|
a<b ? print(a) : print(b)
|
|
|
|
|
set(a, a+1)
|
|
|
|
|
set(a, a+b)
|
|
|
|
|
"abc" + "123"
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-13 17:57:06 +08:00
|
|
|
|
## 4. 函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.1 基本函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-04 10:48:13 +08:00
|
|
|
|
#### assert
|
|
|
|
|
|
|
|
|
|
> 断言
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
assert(condition, crash_if_fail) => void
|
2021-01-04 10:48:13 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 14:40:14 +08:00
|
|
|
|
* crash\_if\_fail 可选。为 true 时,调用系统的 assert,否则只是打印警告。
|
2021-01-13 13:48:59 +08:00
|
|
|
|
|
2021-01-04 10:48:13 +08:00
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
assert(1<2)
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-12 11:38:33 +08:00
|
|
|
|
#### eval
|
|
|
|
|
|
|
|
|
|
> 执行另外一段脚本。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
eval(condition) => value
|
2021-01-12 11:38:33 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
eval("1+2")
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 18:34:09 +08:00
|
|
|
|
#### has_error
|
|
|
|
|
|
|
|
|
|
> 检查是否之前遇到错误
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
has_error() => bool
|
2021-01-02 18:34:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(has_error())
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### clear_error
|
|
|
|
|
|
|
|
|
|
> 清除之前遇到错误
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
clear_error() => void
|
2021-01-02 18:34:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
clear_error()
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
#### print
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 打印调试信息到控制台。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
print(str,...) => void
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print("hello fscript")
|
2020-11-11 15:25:37 +08:00
|
|
|
|
print("hello", "fscript")
|
|
|
|
|
print(1)
|
|
|
|
|
print(true)
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
2021-01-13 13:48:59 +08:00
|
|
|
|
a=100
|
|
|
|
|
b=200
|
2020-11-29 10:20:50 +08:00
|
|
|
|
print(a+b)
|
|
|
|
|
print(join(",", a, b))
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### set
|
|
|
|
|
|
2021-01-02 18:34:09 +08:00
|
|
|
|
> 设置变量的值。建议使用=赋值。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
set(var, value) => bool
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
set(a, 1)
|
2021-01-02 18:34:09 +08:00
|
|
|
|
a=1
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### unset
|
|
|
|
|
|
|
|
|
|
> 清除变量。
|
|
|
|
|
|
2021-01-13 13:48:59 +08:00
|
|
|
|
对于不再使用的变量,特别是对象和字符串,请调用本函数清除掉。
|
|
|
|
|
|
|
|
|
|
当一个变量重复赋值时,会自动清除之前的数据。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
unset(var) => void
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
unset(a)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### int
|
|
|
|
|
|
|
|
|
|
> 转换为整数类型。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
int(var) => int32_t
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
int("123")
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-22 12:23:46 +08:00
|
|
|
|
#### i8
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 int8_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i8(var) => int8_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
i8("123")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### i16
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 int16_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i16(var) => int16_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
i16("123")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### i32
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 int32_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i32(var) => int32_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
i32("123")
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-04 17:22:32 +08:00
|
|
|
|
#### i64
|
|
|
|
|
|
|
|
|
|
> 转换为 int64_t 类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i64(var) => int64_t
|
2021-01-04 17:22:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
i64("123")
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-22 12:23:46 +08:00
|
|
|
|
#### u8
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 unt8_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u8(var) => uint8_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
u8("123")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### u16
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 unt16_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u16(var) => value(uint16)
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
u16("123")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### u32
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 unt32_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u32(var) => uint32_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
u32("123")
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-04 17:22:32 +08:00
|
|
|
|
#### u64
|
|
|
|
|
|
|
|
|
|
> 转换为 unt64_t 类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u64(var) => uint64_t
|
2021-01-04 17:22:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
u64("123")
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-22 12:23:46 +08:00
|
|
|
|
#### f32
|
|
|
|
|
|
|
|
|
|
> 转换为单精度类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
f32(var) => float
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
f32("123")
|
|
|
|
|
```
|
2020-11-10 22:06:48 +08:00
|
|
|
|
#### float
|
|
|
|
|
|
|
|
|
|
> 转换为浮点数类型。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
float(var) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
float("123")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### str
|
|
|
|
|
|
|
|
|
|
> 转换为字符串类型。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
str(var [,force_pointer_as_str]) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
> force\_pointer\_as\_str 如果输入参数是 POINTER 类型,是否将强制转换成字符串。
|
2020-12-10 15:57:12 +08:00
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
str(int(123))
|
2020-12-10 15:57:12 +08:00
|
|
|
|
str(msg.payload, true)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### iformat
|
|
|
|
|
|
|
|
|
|
> 对整数进行格式化(生成的字符串长度不超过 63)
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
iformat(format, value) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
iformat("hello:%d", 123)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### fformat
|
|
|
|
|
|
|
|
|
|
> 对浮点数进行格式化(生成的字符串长度不超过 63)
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
fformat(format, value) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fformat("hello:%lf", 123)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### exec
|
|
|
|
|
|
|
|
|
|
> 执行 object 里的一个命令。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
exec(cmd, arg) => bool
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
exec("clear", "all")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 具体功能与 object 的实现有关。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.2 字符串函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
#### join
|
|
|
|
|
|
2021-01-10 09:58:41 +08:00
|
|
|
|
> 将多个变量用指定的分隔符拼接起来,形成一个字符串。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
join(seperator, s1, s2, s3...) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
join(",", 1, 2, 3, "abc")
|
|
|
|
|
```
|
|
|
|
|
|
2021-02-20 12:42:20 +08:00
|
|
|
|
#### one_of
|
|
|
|
|
|
|
|
|
|
> 从字符串数组中取出第N个字符串
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
one_of(str_array, index, sep) => str
|
|
|
|
|
```
|
|
|
|
|
> sep为分隔符,默认为英文分号(;)。
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
one_of("aa;bb;cc", 0) # => aa
|
|
|
|
|
one_of("aa;bb;cc", 1) # => bb
|
|
|
|
|
one_of("aa;bb;cc", 2) # => cc
|
|
|
|
|
|
|
|
|
|
one_of("aa.bb.cc", 0, ".") # ==> aa
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
#### len
|
|
|
|
|
|
|
|
|
|
> 取字符串的长度。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
len(str) => uint32_t
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
len("abc")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### toupper
|
|
|
|
|
|
|
|
|
|
> 将字符串转换成大写。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
toupper(str) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2020-11-29 10:55:12 +08:00
|
|
|
|
touppper("abc")
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### tolower
|
|
|
|
|
|
|
|
|
|
> 将字符串转换成小写。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
tolower(str) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
tolower("ABC")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### trim
|
|
|
|
|
|
2021-01-10 09:58:41 +08:00
|
|
|
|
> 去掉字符串两端的空白字符串。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
trim(str) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
trm(" abc ")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### substr
|
|
|
|
|
|
|
|
|
|
> 取子字符串。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
substr(str, from, len) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
substr("abcd", 1, 2)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### replace
|
|
|
|
|
|
|
|
|
|
> 替换子字符串。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
replace(str, old, new) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2020-11-11 09:33:33 +08:00
|
|
|
|
replace("ab cd", "ab", "hello")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### contains
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
> 检查是否包含指定的子字符串。
|
|
|
|
|
----------------------------
|
2020-11-11 09:33:33 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
contains(str, substr) => bool
|
2020-11-11 09:33:33 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
contains("ab cd", "ab")
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.3 运算函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
#### sum
|
|
|
|
|
|
|
|
|
|
> 对多个数值类型的参数求和。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
sum(n1,n2...)
|
|
|
|
|
+(n1,n2...)
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1+n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(sum(1, 2, 3))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### sub
|
|
|
|
|
|
|
|
|
|
> 计算两个数之差。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
sub(n1,n2)
|
|
|
|
|
或
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1-n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(sub(2, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(2 - 1)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### mul
|
|
|
|
|
|
|
|
|
|
> 计算两个数之积。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
mul(n1,n2)
|
|
|
|
|
或
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1*n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(mul(2, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(2 * 1)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### div
|
|
|
|
|
|
|
|
|
|
> 计算两个数之商。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
div(n1,n2)
|
|
|
|
|
或
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1/n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(div(2, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(2/1)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### %
|
|
|
|
|
|
|
|
|
|
> 计算两个数的模。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1%n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(23%7)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### and
|
|
|
|
|
|
|
|
|
|
> 逻辑与运算。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
and(n1,n2)
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1 && n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(true && false)
|
|
|
|
|
print(true && true)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### or
|
|
|
|
|
|
|
|
|
|
> 逻辑或运算。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
or(n1,n2)
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1 || n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(a || b)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### not
|
|
|
|
|
|
|
|
|
|
> 逻辑非运算。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
not(n1)
|
|
|
|
|
!(n1)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
print(not(true))
|
|
|
|
|
print(!(false))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 比较函数
|
2020-12-01 18:11:44 +08:00
|
|
|
|
|
|
|
|
|
> 支持字符串比较。
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
#### <
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 小于。
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
<(a,b)
|
|
|
|
|
less(a,b)
|
|
|
|
|
a<b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### <=
|
|
|
|
|
|
|
|
|
|
> 小于等于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
<=(a,b)
|
|
|
|
|
le(a,b)
|
|
|
|
|
a<=b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### >
|
|
|
|
|
|
|
|
|
|
> 大于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
>(a,b)
|
|
|
|
|
great(a,b)
|
|
|
|
|
a>b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### >=
|
|
|
|
|
|
|
|
|
|
> 大于等于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
>=(a,b)
|
|
|
|
|
ge(a,b)
|
|
|
|
|
a>=b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### ==
|
|
|
|
|
|
|
|
|
|
> 等于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
==(a,b)
|
|
|
|
|
eq(a,b)
|
|
|
|
|
a==b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-01 18:11:44 +08:00
|
|
|
|
#### !=
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-12-01 18:11:44 +08:00
|
|
|
|
> 不等于。
|
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-12-01 18:11:44 +08:00
|
|
|
|
##### 原型
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
!=(a,b)
|
|
|
|
|
a != b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.4 数学函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
#### random
|
|
|
|
|
|
|
|
|
|
> 产生随机数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
random() => int
|
|
|
|
|
random(min, max) => int
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-29 10:20:50 +08:00
|
|
|
|
#### round
|
|
|
|
|
|
|
|
|
|
> 四舍五入。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
round(v) => double
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
round(4.5)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### floor
|
|
|
|
|
|
|
|
|
|
> 返回小于等于指定数值的最大整数。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
floor(v) => double
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
floor(4.5)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### ceil
|
|
|
|
|
|
|
|
|
|
> 返回大于等于指定数值的最小整数。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
ceil(v) => double
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
ceil(4.5)
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
#### abs
|
|
|
|
|
|
|
|
|
|
> abs 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
abs(a) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
abs(1)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### min
|
|
|
|
|
|
|
|
|
|
> min 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
min(a, b) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### max
|
|
|
|
|
|
|
|
|
|
> max 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
max(a, b) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
max(1, 2)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
min(1, 2)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### clamp
|
|
|
|
|
|
|
|
|
|
> clamp 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
|
clamp(a, min, max) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
clamp(2, 1, 3)
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.5 时间函数
|
2021-01-13 15:42:09 +08:00
|
|
|
|
> 已经移动到扩展模块。
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
|
|
|
|
### 5. 自定义函数
|
|
|
|
|
|
2020-12-04 08:29:06 +08:00
|
|
|
|
#### 5.1 定义函数
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
static ret_t func_foo(object_t* obj, fscript_args_t* args, value_t* v) {
|
|
|
|
|
value_set_int(v, 123);
|
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-04 08:29:06 +08:00
|
|
|
|
#### 5.2 注册和使用私有函数
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
value_t v;
|
|
|
|
|
object_t* obj = object_default_create();
|
|
|
|
|
object_set_prop_pointer(obj, "function.foo", (void*)func_foo);
|
|
|
|
|
|
|
|
|
|
fscript_eval(obj, "foo()", &v);
|
|
|
|
|
value_reset(&v);
|
|
|
|
|
|
|
|
|
|
OBJECT_UNREF(obj);
|
|
|
|
|
```
|
2020-11-23 11:01:46 +08:00
|
|
|
|
|
2020-12-04 08:29:06 +08:00
|
|
|
|
#### 5.3 注册全局函数
|
|
|
|
|
|
|
|
|
|
* 初始化时调用
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fscript_register_func("foo", func_foo);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 5.1 定义私有函数
|
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
### 6. 性能测量与优化
|
|
|
|
|
|
|
|
|
|
runFScript 的第二个参数可以指定运行次数,方便测量某个函数的运行时间。
|
|
|
|
|
|
|
|
|
|
比如:
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
./bin/runFScript '123+234' 100000
|
2020-11-23 11:01:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
通常设置变量和获取变量相对来说比较费时,为此 fscript 提供了 4 个寄存器变量:a,b,c,d。如果需要临时保存计算结果,优先使用寄存器变量。
|
|
|
|
|
|
|
|
|
|
比如:
|
|
|
|
|
|
|
|
|
|
```
|
2020-12-01 18:11:44 +08:00
|
|
|
|
./bin/runFScript 'set(a, 2); set(b, 3); print(a+b)'
|
2020-11-29 10:55:12 +08:00
|
|
|
|
```
|
2021-01-09 17:30:54 +08:00
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
|
### 7. 扩展模块
|
|
|
|
|
|
|
|
|
|
* [位操作扩展模块](fscript_bits.md)
|
|
|
|
|
* [数学扩展模块](fscript_math.md)
|
|
|
|
|
* [CRC 扩展模块](fscript_crc.md)
|
|
|
|
|
* [value 扩展模块](fscript_value.md)
|
|
|
|
|
* [widget 扩展模块](fscript_widget.md)
|
|
|
|
|
* [object 扩展模块](fscript_object.md)
|
2021-01-13 13:48:59 +08:00
|
|
|
|
* [文件系统扩展模块](fscript_fs.md)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
* [app\_conf 扩展模块](fscript_app_conf.md)
|
|
|
|
|
* [日期和时间扩展模块](fscript_date_time.md)
|
|
|
|
|
* [JSON 扩展模块](fscript_json.md)
|
|
|
|
|
* [大端小端扩展模块](fscript_endian.md)
|
|
|
|
|
* [istream 扩展模块](fscript_istream.md)
|
|
|
|
|
* [ostream 扩展模块](fscript_ostream.md)
|
|
|
|
|
* [iostream 扩展模块](fscript_iostream.md)
|
|
|
|
|
* [TCP/UDP 流扩展模块](fscript_iostream_inet.md)
|
|
|
|
|
* [文件流扩展模块](fscript_iostream_file.md)
|
|
|
|
|
* [串口流扩展模块](fscript_iostream_serial.md)
|
2021-01-14 10:00:45 +08:00
|
|
|
|
* [动态数组扩展模块](fscript_typed_array.md)
|
|
|
|
|
* [单一类型的动态数组扩展模块](fscript_typed_array.md)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
* [写缓冲区扩展模块](fscript_wbuffer.md)
|
|
|
|
|
* [读缓冲区扩展模块](fscript_rbuffer.md)
|
|
|
|
|
|
|
|
|
|
### 8. 更多示例
|
|
|
|
|
|
|
|
|
|
请参考 tests/fscripts 目录。
|