- 安装以及环境配置
-
ChaiScript安装
官网下载似乎不能成功,1M的文件,下载60K的时候就说网络错误,于是翻墙下,很快。将其上传到百度云盘了,方便下载,windows 64位的。
下载地址:http://pan.baidu.com/s/1cLXk9S -
boost库
ChaiScript 5.0后的版本已经不需要安装boost了 -
IDE配置
Visual Studio中,在工程属性->C++->常规->附加包含目录。 将ChaiScript的include路径添加进去。
C:\Program Files\chaiscript 5.8.1\include
-
脚本基本语法
ChaiScript可以直接调用C++的对象方法,以及对象参数。
在C++中可以直接调用脚本中的函数以及变量,直接函数重载,部分STL容器。
这些内容将在后面逐一提到。
- 条件模块
var i = 2
if(i < 2){
print("less than 2")
}else if(i == 2){
print("equal to 2")
}else{
print("more than 2")
}```
- 循环语句
for(var i = 0;i<10;++i){
print("i:"+i.to_string())
}
//while
var i = 0
while(i<10){
print("i:"+i.to_string())
++i
}```
- 容器
var x = [1,2,3]
print(x[1])
//字典
var x = ["bob":1,"fred":2]
print(x["fred"])
//通过值的范围快速创建Vector容器
var x = [1..10]
print(x)```
- 函数
//静态函数
def add_elems(x,y){
x + y
}
//创建匿名函数
var add_elems = fun(x,y){x + y}
print(add_elems(5,10))```
- 返回值
def five(){
return 5
}
//或者默认将最后一个值返回
def five(){
5
}```
- ChaiScript不支持方法
5.to_string()
to_string(5)
//只是值在.左边作为第一个参数命名的函数```
- 派遣函数
def print_num(x): x == 0 || x > 1{
print(x.to_string() + "units")
}
def print_num(x): x == 1{
print(x.to_string() + "units")
}```
3. **ChaiScript用法**
- 首先添加下面这条语句到源程序中
`#include <chaiscript/chaiscript.hpp>`
- 然后实例化ChaiScript引擎,创建一个名为chai的新引擎
`chaiscript::ChaiScript chai`
- 一旦实例化完成,引擎就可以开始运行ChaiScript程序了
两种方法
- 一次一行的处理
`chai.eval(string)`
- 使用一个chai脚本文件
`chai.eval_file(filename)`
- 使C++函数对chai脚本可见,必须在脚本引擎中注册
`chai.add(chaiscript::fun($my_function),"my_function_name");`
注册后,函数就在脚本是可见的,名字是my_function_name
- 另外,如果想在代码中编译std库以加快运行速度
include <chaiscript/chaiscript_stdlib.hpp>
//然后在main()函数中这样
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library())```
- 一个简单的例子
#include <chaiscript/chaiscript.hpp>
double function(int i, double j){
return i * j
}
int main(){
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&function),"function");
double d = chai.eval<double>("function(3,4.75);");
}```
- **测试程序**
include <chaiscript/chaiscript.hpp>
include <math.h>
include <chaiscript/chaiscript_stdlib.hpp>
double Exp(double value)
{
return exp(value);
}
double function(int i, double j)
{
return i * j;
}
int main()
{
//实例化引擎
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
//添加函数function
chai.add(chaiscript::fun(&function), "function");
//添加函数Exp作为exp
chai.add(chaiscript::fun(&Exp), "exp");
//加载脚本文件
chai.eval_file("HelloWorld.chai");
double value = -2;
double out;
//将value注册,以便在脚本中使用
chai.add(chaiscript::var(value), "value");
chai.add(chaiscript::var(&out), "out");
//use & to allow the variable in charscript can be written and linked to the variable in c++
using namespace chaiscript;
//也可以从chai的环境中提取而定义C++函数
std::function<double()> TestChai = chai.eval<std::function<double()> >("TestChai");
double d = TestChai(); // 调用脚本函数
//这里在C++中调用chai脚本的内容
chai("print("hello")");
// "hello" can be printed in chaiscript
chai("
if (value>1)\n
{out = 1/exp(value)}\n
else\n
{out = exp(value)}\n
");
printf("%lf", out);
printf("%f", d);
system("pause");
}```
另外程序需要的ChaiScript的脚本文件,HelloWorld.chai
def TestChai()
{
print("hello world chaisprite!")
return function(3, 5.0)
}```