📄 script.cpp
字号:
// Script.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "script.h"
#include "buildin_functions.h"
#include "script_session.h"
#include "file_object.h"
#include "timecounter.h"
using namespace std;
using namespace script;
__MMDECL__
void name_printer(const string & name, script_reference * obj)
{
cout << name << " - " << obj->to_string() << endl;
}
string call_function(script_session &ss, const string & funcname, const string & arguments)
{
script_instance * result = 0;
unsigned start = GetTickCount();
if(ss.call(funcname, arguments, &result))
{
unsigned end = GetTickCount();
cout << endl << (end - start) << "ms" << endl;
}
string str = (result == 0 ? "" :result->to_string());
DEREF(result);
return str;
}
int process(const string &filename, const string &arguments)
{
script_file file;
time_counter tc;
if(!file.read_file(filename))
{
cout << "some errors occurred." << endl;
system("pause");
return -1;
}
unsigned parsing_time = tc.time();
script_session ss;
ss.create_runtime();
file.functions.for_each(script_function_inserter(ss));
function_print * print = new function_print;
function_print_line * println = new function_print_line;
function_get * get = new function_get;
function_get_line * get_line = new function_get_line;
function_exec * exec = new function_exec;
FUNCTION_AUTO_NAME(file_struct) * fileobj = new FUNCTION_AUTO_NAME(file_struct);
native_function *sr = create_cpp_function("srand", function_srand(), "function srand(seed)");
native_function * rnd = create_cpp_function("rand", function_rand(), "function rand([range]) or function rand(start, end)");
ss.add_native_function("print", print);
ss.add_native_function("println", println);
ss.add_native_function("get", get);
ss.add_native_function("getline", get_line);
ss.add_native_function("flush", get_line);
ss.add_native_function("exec", exec);
ss.add_native_function("srand", sr);
ss.add_native_function("rand", rnd);
ss.add_native_function("File", fileobj);
DEREF(print);
DEREF(println);
DEREF(exec);
DEREF(get);
DEREF(get_line);
DEREF(sr);
DEREF(rnd);
DEREF(fileobj);
// if the file ext is 'scpexe', run main function directly
// otherwise, wait for user's commands.
size_t pos = filename.find(".scpexe");
if( pos != -1 && pos == filename.length() - 7)
{
pos = filename.rfind("\\");
if(pos != -1) SetCurrentDirectory(filename.substr(0, pos).c_str());
string result = call_function(ss, "main", arguments);
return atoi(result.c_str());
}
else
{
cout << "parsing time: " << parsing_time << "ms" << endl;
if(!arguments.empty())
cout << "arguments : " << arguments << " was ignored." << endl;
cout << "type ? for help" << endl;
while(true)
{
string command;
cout << endl << "COMMAND:";
getline(cin, command);
//cin >> command;
if(command.empty())
{
continue;
}
else if(command == "?")
{
cout << "exit - exit program" << endl
<< "list - list all named object" << endl
<< "[function name] - call specific function without any argument " << endl;
}
else if(command == "exit")
break;
else if(command == "list")
ss.for_each(name_printer);
else
{
size_t pos = command.find(" ");
string func = command.substr(0, pos);
string args = (pos == -1 ? "" : command.substr(pos+1, command.length() - pos - 1));
string result = call_function(ss, func, args);
if(!result.empty())
{
cout << endl << "return result : " << result << endl;
}
}
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
string filename = "test.scp";
if(argc >= 2)
filename = argv[1];
int index = 2;
string arguments = "";
while(argc > index)
{
arguments.append("\"");
arguments += argv[index++];
arguments.append("\" ");
}
return process(filename, arguments);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -