04.03 轻量级嵌入式脚本语言 FakeScript-Java

简介

fakescript是一款轻量级的嵌入式脚本语言,使用Java语言编写,语法吸取自lua、golang、erlang,基于jflex、bison生成语法树,编译成字节码解释执行。 C/C++版本fakescript

脚本特性

  • 语法类似lua
  • 全部为函数
  • 支持array,map,可以无限嵌套
  • 支持fake testfunc(param1)产生routine,实现假多线程效果
  • 支持Java静态函数和Java类成员函数的绑定
  • 自带解释器
  • 支持多返回值
  • 自带profile,可获取脚本各个函数运行时间
  • 支持热更新
  • 支持Int64
  • 支持const定义
  • 支持包
  • 支持struct
  • 支持字节码优化
  • 支持全局map

示例


-- 当前包名

package mypackage.test

-- 引入的文件
include "common.fk"

-- 结构体定义
struct teststruct
\tsample_a
\tsample_b
\tsample_c
end

-- 常量值
const hellostring = "hello"
const helloint = 1234
const hellomap = {1 : "a" 2 : "b" 3 : [1 2 3]}
-- func1 comment
func myfunc1(arg1, arg2)
\t
\t-- Java静态函数和类成员函数的调用
\targ3 := cfunc1(helloint) + arg2:memfunc1(arg1)
\t
\t-- 分支
\tif arg1 < arg2 then\t
\t\t-- 创建一个协程
\t\tfake myfunc2(arg1, arg2)
\telseif arg1 == arg2 then\t
\t\tprint("elseif")
\telse
\t\tprint("else")
\tend
\t
\t-- for循环
\tfor var i = 0, i < arg2, i++ then
\t\tprint("i = ", i)
\tend
\t
\t-- 数组
\tvar a = array()
\ta[1] = 3
\t
\t-- 集合
\tvar b = map()
\tb[a] = 1
\tb[1] = a
\t

\t-- Int64
\tvar uid = 1241515236123614u
\tlog("uid = ", uid)

\t-- 子函数调用
\tvar ret1, var ret2 = myfunc2()

\t-- 其他包的函数调用
\tret1 = otherpackage.test.myfunc1(arg1, arg2)
\t
\t-- 结构体
\tvar tt = teststruct()
\ttt->sample_a = 1
\ttt->sample_b = teststruct()
\ttt->sample_b->sample_a = 10

\t-- 分支
\tswitch arg1
\t\tcase 1 then
\t\t\tprint("1")
\t\tcase "a" then
\t\t\tprint("a")
\t\tdefault
\t\t\tprint("default")
\tend

\t-- 多返回值
\treturn arg1, arg3
\t
end

Java示例

// 创建一个实例
fake f = fk.newfake(null);
// 注册包里全部标记@fakescript的函数
fk.reg(f, "com.test");
// 解析fake脚本文件
fk.parse(f, "test.fk");
// 执行myfunc1函数,传入两个参数分别为1和2
double ret = (double)fk.run(f, "myfunc1", 1, 2);

使用

Maven

<dependency>
<groupid>com.github.esrrhs/<groupid>
<artifactid>fakescript-java/<artifactid>
<version>1.0.12/<version>
/<dependency>

调试环境

  • IDE
轻量级嵌入式脚本语言 FakeScript-Java

  • 命令行
轻量级嵌入式脚本语言 FakeScript-Java


分享到:


相關文章: