-
Notifications
You must be signed in to change notification settings - Fork 2
/
FireFlyLang.cpp
87 lines (75 loc) · 2.41 KB
/
FireFlyLang.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "FireFlyLang.h"
#include <angelscript.h>
#include <scriptstdstring/scriptstdstring.h>
#include <scriptbuilder/scriptbuilder.h>
#include <cassert>
#include <string>
#include "Dialogs.h"
#include "FireFly.h"
const char FireFlyLang::KEYWORDS[] = ""; // whitespace separated
FireFlyLang::FireFlyLang(FireFly& interface) : interface(interface)
{
this->engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
RegisterStdString(this->engine);
this->register_functions();
}
FireFlyLang::~FireFlyLang()
{
this->engine->Release();
this->engine = NULL;
}
bool FireFlyLang::register_functions()
{
//this->engine->RegisterGlobalFunction("void print(const string& in)", asMETHOD(FireFlyLang, print), asCALL_THISCALL);
return true;
}
bool FireFlyLang::script_load(const std::string& script)
{
// The CScriptBuilder helper is an add-on that loads the file,
// performs a pre-processing pass if necessary, and then tells
// the engine to build a script module.
CScriptBuilder builder;
builder.StartNewModule(this->engine, "MyModule");
builder.AddSectionFromMemory(script.c_str());
builder.BuildModule();
return true;
}
bool FireFlyLang::script_compile()
{
return true;
}
bool FireFlyLang::script_run()
{
// Find the function that is to be called.
asIScriptModule *mod = this->engine->GetModule("MyModule");
int funcId = mod->GetFunctionIdByDecl("void main()");
if(funcId < 0)
{
// The function couldn't be found. Instruct the script writer
// to include the expected function in the script.
DlgQuestion::show(this->interface.parent, L"The script must have the function 'void main()'. Please add it and try again.", L"FireFly", DlgQuestion::tError);
return false;
}
// Create our context, prepare it, and then execute
asIScriptContext *ctx = this->engine->CreateContext();
ctx->Prepare(funcId);
int r = ctx->Execute();
switch(r)
{
case asEXECUTION_FINISHED:
DlgQuestion::show(this->interface.parent, L"Execution finished.", L"FireFly", DlgQuestion::tInfo);
break;
case asEXECUTION_EXCEPTION:
DlgQuestion::show(this->interface.parent, L"An exception occured.", L"FireFly", DlgQuestion::tError);
break;
default:
DlgQuestion::show(this->interface.parent, L"Unknown error.", L"FireFly", DlgQuestion::tError);
break;
}
return true;
}
void FireFlyLang::print(const string& in)
{
//this->interface.message(in);
DlgQuestion::show(this->interface.parent, L"message called", L"FireFly", DlgQuestion::tInfo);
}