|
|
@ -32,21 +32,6 @@ void print_value(std::ostream& stream, const scripting::script_value& res, bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
struct identity : public scripting::function_impl { |
|
|
|
std::optional<scripting::script_value> apply(scripting::UserScript* self,std::vector<scripting::argument> args, std::optional<scripting::script_error>& errors) final { |
|
|
|
if(args.size() != 1) { |
|
|
|
errors = scripting::script_error{.message = "identity expects a single argument"}; |
|
|
|
} else { |
|
|
|
if(std::holds_alternative<scripting::script_value>(args.front())) { |
|
|
|
return std::get<scripting::script_value>(args.front()); |
|
|
|
} else { |
|
|
|
return self->resolve(std::get<scripting::script_variable>(args.front()).name); |
|
|
|
} |
|
|
|
} |
|
|
|
return scripting::script_value({}); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
struct print : public scripting::function_impl { |
|
|
|
std::ostream& stream; |
|
|
|
|
|
|
@ -67,39 +52,6 @@ struct print : public scripting::function_impl { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
struct set : public scripting::function_impl { |
|
|
|
std::optional<scripting::script_value> apply(scripting::UserScript* self,std::vector<scripting::argument> args, std::optional<scripting::script_error>& errors) final { |
|
|
|
|
|
|
|
if(args.size() != 2) { |
|
|
|
errors = scripting::script_error{ |
|
|
|
.message = "set expects 2 arguments" |
|
|
|
}; |
|
|
|
return scripting::script_value{}; |
|
|
|
} |
|
|
|
|
|
|
|
auto& var = args.back(); |
|
|
|
if(not holds_alternative<scripting::script_variable>(var)) { |
|
|
|
errors = scripting::script_error{ |
|
|
|
.message = "set expects the first argument to be a target variable" |
|
|
|
}; |
|
|
|
return scripting::script_value{}; |
|
|
|
} |
|
|
|
|
|
|
|
auto& arg = args.front(); |
|
|
|
|
|
|
|
if(std::holds_alternative<scripting::script_value>(arg)) { |
|
|
|
self->setValue(get<scripting::script_variable>(var).name, std::get<scripting::script_value>(arg)); |
|
|
|
} else { |
|
|
|
self->setValue(get<scripting::script_variable>(var).name, self->resolve(std::get<scripting::script_variable>(arg).name)); |
|
|
|
} |
|
|
|
if(auto v = self->getValue(get<scripting::script_variable>(var).name); v) { |
|
|
|
return v.value(); |
|
|
|
} else { |
|
|
|
return scripting::script_value{}; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
struct terminate : public scripting::function_impl { |
|
|
|
std::optional<scripting::script_value> apply(scripting::UserScript*,std::vector<scripting::argument>, std::optional<scripting::script_error>&) final { |
|
|
|
std::exit(1); |
|
|
@ -122,9 +74,14 @@ struct fn_exit : public scripting::function_impl { |
|
|
|
void process_bench(std::string target = "./tests/scripts/testfile.test") { |
|
|
|
auto engine = scripting::prepare_interpreter(std::string{}); |
|
|
|
|
|
|
|
engine->registerFunction("identity", std::make_unique<identity>()); |
|
|
|
|
|
|
|
constexpr scripting::UserScriptLibraryParameters params{}; |
|
|
|
|
|
|
|
engine = scripting::register_array_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_utils_lib(std::move(engine), params); |
|
|
|
|
|
|
|
engine->registerFunction("exit", std::make_unique<terminate>()); |
|
|
|
engine->registerFunction("set", std::make_unique<set>()); |
|
|
|
|
|
|
|
/***
|
|
|
|
* This is a half assed benchmark, |
|
|
@ -178,9 +135,14 @@ void process_bench(std::string target = "./tests/scripts/testfile.test") { |
|
|
|
void compile_bench(std::string target = "./tests/scripts/testfile.test") { |
|
|
|
auto engine = scripting::prepare_interpreter(std::string{}); |
|
|
|
|
|
|
|
engine->registerFunction("identity", std::make_unique<identity>()); |
|
|
|
|
|
|
|
constexpr scripting::UserScriptLibraryParameters params{}; |
|
|
|
|
|
|
|
engine = scripting::register_array_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_utils_lib(std::move(engine), params); |
|
|
|
|
|
|
|
engine->registerFunction("exit", std::make_unique<terminate>()); |
|
|
|
engine->registerFunction("set", std::make_unique<set>()); |
|
|
|
|
|
|
|
/***
|
|
|
|
* Same as above but for compilation times |
|
|
@ -220,15 +182,13 @@ void compile_bench(std::string target = "./tests/scripts/testfile.test") { |
|
|
|
void compare(std::string target, std::string expect) { |
|
|
|
auto engine = scripting::prepare_interpreter(std::string{}); |
|
|
|
|
|
|
|
engine->registerFunction("identity", std::make_unique<identity>()); |
|
|
|
engine->registerFunction("exit", std::make_unique<terminate>()); |
|
|
|
engine->registerFunction("set", std::make_unique<set>()); |
|
|
|
|
|
|
|
constexpr size_t array_limit = 4096; |
|
|
|
constexpr size_t string_limit = 4096; |
|
|
|
constexpr scripting::UserScriptLibraryParameters params{}; |
|
|
|
|
|
|
|
engine = scripting::register_array_lib(std::move(engine), true, array_limit); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), array_limit, string_limit); |
|
|
|
engine = scripting::register_array_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_utils_lib(std::move(engine), params); |
|
|
|
|
|
|
|
std::stringstream str; |
|
|
|
std::string_view filename_source = target; |
|
|
@ -274,15 +234,13 @@ void immediate_interactive() { |
|
|
|
bool should_exit = false; |
|
|
|
auto engine = scripting::prepare_interpreter(std::string{}); |
|
|
|
|
|
|
|
engine->registerFunction("identity", std::make_unique<identity>()); |
|
|
|
engine->registerFunction("exit", std::make_unique<fn_exit>(should_exit)); |
|
|
|
engine->registerFunction("set", std::make_unique<set>()); |
|
|
|
constexpr scripting::UserScriptLibraryParameters params{}; |
|
|
|
|
|
|
|
constexpr size_t array_limit = 4096; |
|
|
|
constexpr size_t string_limit = 4096; |
|
|
|
engine = scripting::register_array_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_utils_lib(std::move(engine), params); |
|
|
|
|
|
|
|
engine = scripting::register_array_lib(std::move(engine), true, array_limit); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), array_limit, string_limit); |
|
|
|
engine->registerFunction("exit", std::make_unique<fn_exit>(should_exit)); |
|
|
|
|
|
|
|
engine->registerFunction("print", std::make_unique<print>(std::cout)); |
|
|
|
while (not should_exit) { |
|
|
@ -306,38 +264,54 @@ void immediate_interactive() { |
|
|
|
} |
|
|
|
|
|
|
|
void exec(std::span<std::string_view> args) { |
|
|
|
std::vector<decltype(scripting::prepare_interpreter(std::string{}))> batch; |
|
|
|
//std::vector<decltype(scripting::prepare_interpreter(std::string{}))> batch;
|
|
|
|
|
|
|
|
|
|
|
|
std::ifstream src_str(std::string{args.front()}); |
|
|
|
std::stringstream code; |
|
|
|
code << src_str.rdbuf(); |
|
|
|
std::string code_val = code.str(); |
|
|
|
|
|
|
|
auto engine = scripting::prepare_interpreter(std::string{}); |
|
|
|
bool exit = false; |
|
|
|
|
|
|
|
engine->registerFunction("identity", std::make_unique<identity>()); |
|
|
|
engine->registerFunction("terminate", std::make_unique<terminate>()); |
|
|
|
engine->registerFunction("set", std::make_unique<set>()); |
|
|
|
constexpr scripting::UserScriptLibraryParameters params{}; |
|
|
|
|
|
|
|
constexpr size_t array_limit = 4096; |
|
|
|
constexpr size_t string_limit = 4096; |
|
|
|
engine = scripting::register_array_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), params); |
|
|
|
engine = scripting::register_utils_lib(std::move(engine), params); |
|
|
|
|
|
|
|
engine = scripting::register_array_lib(std::move(engine), true, array_limit); |
|
|
|
engine = scripting::register_crypto_lib(std::move(engine), array_limit, string_limit); |
|
|
|
engine->registerFunction("exit", std::make_unique<fn_exit>(exit)); |
|
|
|
|
|
|
|
engine->registerFunction("print", std::make_unique<print>(std::cout)); |
|
|
|
bool exit = false; |
|
|
|
|
|
|
|
auto errors = engine->prepare(code_val); |
|
|
|
|
|
|
|
if(not errors.empty()) { |
|
|
|
for (auto &line: errors) { |
|
|
|
std::cout << line.message << "\n at line "; |
|
|
|
if(line.location) { |
|
|
|
std::cout << line.location->line_number << ":" |
|
|
|
<< line.location->column_number << "\n"; |
|
|
|
std::cout << " " << *line.location->line_contents << "\n"; |
|
|
|
std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n"; |
|
|
|
} else std::cout << "UNKNOWN\n"; |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
while (not exit) { |
|
|
|
std::string code; |
|
|
|
std::getline(std::cin, code); |
|
|
|
auto res = engine->executeAtOnce(code); |
|
|
|
if (std::holds_alternative<scripting::script_value>(res)) { |
|
|
|
auto res = engine->stepOnce(); |
|
|
|
if (not res) { |
|
|
|
} else { |
|
|
|
auto &errors = std::get<std::vector<scripting::script_error>>(res); |
|
|
|
for (auto &line: errors) { |
|
|
|
std::cout << line.message << "\n at line "; |
|
|
|
if(line.location) { |
|
|
|
std::cout << line.location->line_number << ":" |
|
|
|
<< line.location->column_number << "\n"; |
|
|
|
std::cout << " " << *line.location->line_contents << "\n"; |
|
|
|
std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n"; |
|
|
|
} else std::cout << "UNKNOWN\n"; |
|
|
|
} |
|
|
|
auto line = res.value(); |
|
|
|
std::cout << line.message << "\n at line "; |
|
|
|
if(line.location) { |
|
|
|
std::cout << line.location->line_number << ":" |
|
|
|
<< line.location->column_number << "\n"; |
|
|
|
std::cout << " " << *line.location->line_contents << "\n"; |
|
|
|
std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n"; |
|
|
|
} else std::cout << "UNKNOWN\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -380,7 +354,7 @@ int cpp_main(std::span args) { |
|
|
|
if(args.empty()) compile_bench(); |
|
|
|
else compile_bench(std::string{args.front()}); |
|
|
|
} else if(args.front() == "exec") { |
|
|
|
c1">// exec(args.subspan(1));
|
|
|
|
n">exec(args.subspan(1)); |
|
|
|
} else { |
|
|
|
std::cerr << "Unknown option" << std::endl; |
|
|
|
} |
|
|
|