实时规则计算引擎
lexer is inspired by tdop
中文 | English
go get -u github.com/shockerli/spiker
- Execute
spiker.Execute(`1 + 2 * 3 / 4`)
- ExecuteWithScope
var scopes = spiker.NewScopeTable("demo", 1, nil)
scopes.Set("a", 3)
scopes.Set("b", 4)
spiker.ExecuteWithScope(`a * 2 + b`, )
- Format
spiker.Format(`a + b * 3`)
true/false/if/else/in/...
每个语句后用英文分号(;)结束指令
- 数字
整型/浮点型
123;
-123;
12.34;
-12.34;
- 字符串
"abc"
- 布尔
true;
false;
- 数组
[];
[1, 2, "a"];
[1, [], [2,], [3, 4,], 5];
- 字典
v = [9:99, 8:8.8, "hello":12.02];
v = ["name":"jioby", "age":18, "log":[1:2, 3:4]];
v = [1, 9:9.9, 3];
1 + 2 - 3 * 4 / 5 % 6;
-0.19 + 3.2983;
3 ** 2;
1 & 2;
1 | 2;
1 ^ 2;
1 >> 2;
1 << 2;
3 == 2;
3 != 2;
3 > 2;
3 >= 2;
3 < 2;
3 <= 2;
!2;
1 && 2;
1 || 2;
v = 2;
v += 2;
v -= 2;
v *= 2;
v /= 2;
v %= 2;
a = b = c = 100;
"john" in ["joy", "john"]; // true
100 in [100, 200, 300]; // true
9 in [9:"999", 8:"888"]; // true
9 in "123456789"; // true
- export
中断脚本,并返回表达式的值
export(v);
export(v[i]);
- exist
判断变量或索引是否存在
exist(v);
exist(v[i]);
- len
返回值的长度或数量
len("123");
len(v);
len(v[i]);
- del
删除一个或多个的变量或索引
del(a)
del(a, b)
del(a, b[1])
del(a[i], b[i])
- 单行函数
sum = (a, b) -> a + b;
export(sum(1, 2)); # 3
pow2 = x -> x ** 2;
export(pow2(5)); # 25
- 块函数
max = (a, b) -> {
if (a > b) {
return a;
} else {
return b;
}
};
export(max(1, 2)); # 2
- if/else
if (a > b && c != d) {
a = b;
} else if (c > b) {
a = c;
} else {
export(c + d);
}
- while
while (true) {
a = 0;
while (a < 10) {
print(a, "\n");
a += 1;
}
}
a = 101;
b = 102;
c = "103";
d = [1, 2, a, b, c+b, "abc"];
if (a > b) {
export(len(d));
} else if (a < b) {
if (c) {
d = -1000;
} else {
d = 0;
}
d += len(c);
export(d);
}
export(d[2]);
This project is licensed under the terms of the MIT license.