Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

345 rindas
12 KiB

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <cmath>
  6. #include <chrono>
  7. #include <fstream>
  8. #include <span>
  9. #include <cstring>
  10. #include "UserScript.h"
  11. void print_value(std::ostream& stream, const scripting::script_value& res) {
  12. if(std::holds_alternative<scripting::array>(res)) {
  13. stream << "[";
  14. auto max = std::get<scripting::array>(res).value.size();
  15. auto no_comma = max - 1;
  16. for(size_t idx = 0; idx < max; ++idx) {
  17. print_value(stream, std::get<scripting::array>(res).value[idx]);
  18. stream << (idx != no_comma ? ", " : "");
  19. }
  20. stream << "]";
  21. } else if(std::holds_alternative<std::string>(res)) {
  22. stream << std::get<std::string>(res);
  23. } else if(std::holds_alternative<scripting::null>(res)) {
  24. stream << "null";
  25. } else {
  26. stream << std::get<int32_t>(res);
  27. }
  28. }
  29. struct identity : public scripting::function_impl {
  30. std::optional<scripting::script_value> apply(scripting::UserScript* self,std::vector<scripting::argument> args, std::optional<scripting::script_error>& errors) final {
  31. if(args.size() != 1) {
  32. errors = scripting::script_error{.message = "identity expects a single argument"};
  33. } else {
  34. if(std::holds_alternative<scripting::script_value>(args.front())) {
  35. return std::get<scripting::script_value>(args.front());
  36. } else {
  37. return self->resolve(std::get<scripting::script_variable>(args.front()).name);
  38. }
  39. }
  40. return scripting::script_value({});
  41. }
  42. };
  43. struct print : public scripting::function_impl {
  44. std::ostream& stream;
  45. print(std::ostream& _stream) : stream(_stream) {}
  46. std::optional<scripting::script_value> apply(scripting::UserScript* self,std::vector<scripting::argument> args, std::optional<scripting::script_error>& errors) final {
  47. while(not args.empty()) {
  48. auto& arg = args.back();
  49. if(std::holds_alternative<scripting::script_value>(arg)) {
  50. print_value(stream, std::get<scripting::script_value>(arg));
  51. } else {
  52. print_value(stream, self->resolve(std::get<scripting::script_variable>(arg).name));
  53. }
  54. args.pop_back();
  55. }
  56. return scripting::script_value({});
  57. }
  58. };
  59. struct set : public scripting::function_impl {
  60. std::optional<scripting::script_value> apply(scripting::UserScript* self,std::vector<scripting::argument> args, std::optional<scripting::script_error>& errors) final {
  61. if(args.size() != 2) {
  62. errors = scripting::script_error{
  63. .message = "set expects 2 arguments"
  64. };
  65. return scripting::script_value{};
  66. }
  67. auto& var = args.back();
  68. if(not holds_alternative<scripting::script_variable>(var)) {
  69. errors = scripting::script_error{
  70. .message = "set expects the first argument to be a target variable"
  71. };
  72. return scripting::script_value{};
  73. }
  74. auto& arg = args.front();
  75. if(std::holds_alternative<scripting::script_value>(arg)) {
  76. self->setValue(get<scripting::script_variable>(var).name, std::get<scripting::script_value>(arg));
  77. } else {
  78. self->setValue(get<scripting::script_variable>(var).name, self->resolve(std::get<scripting::script_variable>(arg).name));
  79. }
  80. if(auto v = self->getValue(get<scripting::script_variable>(var).name); v) {
  81. return v.value();
  82. } else {
  83. return scripting::script_value{};
  84. }
  85. }
  86. };
  87. struct terminate : public scripting::function_impl {
  88. std::optional<scripting::script_value> apply(scripting::UserScript*,std::vector<scripting::argument>, std::optional<scripting::script_error>&) final {
  89. std::exit(1);
  90. // PLEASE DO NOT ACTUALLY EXIT YOU FUCKING IDIOT
  91. return scripting::script_value({});
  92. }
  93. };
  94. void process_bench(std::string target = "./tests/scripts/testfile.test") {
  95. auto engine = scripting::prepare_interpreter(std::string{});
  96. engine->registerFunction("identity", std::make_unique<identity>());
  97. engine->registerFunction("exit", std::make_unique<terminate>());
  98. engine->registerFunction("set", std::make_unique<set>());
  99. /***
  100. * This is a half assed benchmark,
  101. * Document results here to keep the thingy in check performance wise (release mode only)
  102. *
  103. * 2023-07-04 Archivist -> 2618ns - 308ns - 49ns (clang+libstdc++)
  104. * 2023-07-07 Archivist -> 2481ns - 291ns - 46ns (clang+libc++)
  105. * 2023-07-07 Archivist -> 106ns - 12ns - 2ns (clang+march=native+libc++)
  106. */
  107. engine->registerFunction("print", std::make_unique<print>(std::cout));
  108. std::ifstream src_str(target);
  109. std::stringstream code;
  110. code << src_str.rdbuf();
  111. int steps = 0;
  112. decltype(std::chrono::high_resolution_clock::now()-std::chrono::high_resolution_clock::now()) per_exec{}, per_step{}, per_op{};
  113. for(int runs = 0; runs < 5000; runs++) {
  114. auto res = engine->prepare(code.str());
  115. auto begin = std::chrono::high_resolution_clock::now();
  116. while (not engine->getValue("exit_ctr").has_value()) {
  117. engine->stepOnce();
  118. steps++;
  119. }
  120. auto end = std::chrono::high_resolution_clock::now();
  121. per_exec += (end - begin);
  122. per_step += (end - begin);
  123. per_op += (end - begin);
  124. }
  125. per_exec /= 5000;
  126. per_step /= steps;
  127. per_op = per_op / 5000 / 53;
  128. std::cout << "time per exec = " << std::chrono::duration_cast<std::chrono::nanoseconds>(per_exec).count() << "ns\n";
  129. std::cout << "time per step = " << std::chrono::duration_cast<std::chrono::nanoseconds>(per_step).count() << "ns\n";
  130. std::cout << "time per avg op = " << std::chrono::duration_cast<std::chrono::nanoseconds>(per_op).count() << "ns\n";
  131. }
  132. void compile_bench(std::string target = "./tests/scripts/testfile.test") {
  133. auto engine = scripting::prepare_interpreter(std::string{});
  134. engine->registerFunction("identity", std::make_unique<identity>());
  135. engine->registerFunction("exit", std::make_unique<terminate>());
  136. engine->registerFunction("set", std::make_unique<set>());
  137. /***
  138. * Same as above but for compilation times
  139. *
  140. * 2023-07-04 Archivist -> 386µs
  141. */
  142. engine->registerFunction("print", std::make_unique<print>(std::cout));
  143. std::ifstream src_str("./tests/scripts/testfile.test");
  144. std::stringstream code;
  145. code << src_str.rdbuf();
  146. auto begin = std::chrono::high_resolution_clock::now();
  147. [&]() __attribute__((optimize("O0"))) {
  148. auto res = engine->prepare(code.str());
  149. res = engine->prepare(code.str());
  150. res = engine->prepare(code.str());
  151. res = engine->prepare(code.str());
  152. res = engine->prepare(code.str());
  153. }();
  154. auto end = std::chrono::high_resolution_clock::now();
  155. auto per_exec = (end - begin)/5;
  156. std::cout << "time per exec = " << std::chrono::duration_cast<std::chrono::microseconds>(per_exec).count() << "µs\n";
  157. }
  158. void compare(std::string target, std::string expect) {
  159. auto engine = scripting::prepare_interpreter(std::string{});
  160. engine->registerFunction("identity", std::make_unique<identity>());
  161. engine->registerFunction("exit", std::make_unique<terminate>());
  162. engine->registerFunction("set", std::make_unique<set>());
  163. std::stringstream str;
  164. std::string_view filename_source = target;
  165. std::string_view filename_output = expect;
  166. engine->registerFunction("print", std::make_unique<print>(str));
  167. std::ifstream src_str(std::string{filename_source});
  168. std::stringstream code;
  169. code << src_str.rdbuf();
  170. std::ifstream out_str(std::string{filename_output});
  171. std::stringstream output;
  172. output << out_str.rdbuf();
  173. auto res = engine->executeAtOnce(code.str());
  174. if (std::holds_alternative<scripting::script_value>(res)) {
  175. } else {
  176. auto &errors = std::get<std::vector<scripting::script_error>>(res);
  177. for (auto &line: errors) {
  178. str << line.message << "\n at line " << line.location->line_number << ":"
  179. << line.location->column_number << "\n";
  180. str << " " << *line.location->line_contents << "\n";
  181. str << " " << std::string(line.location->column_number - 1, ' ') << "^\n";
  182. }
  183. }
  184. int status = 0;
  185. while(not output.eof()) {
  186. std::string expected, found;
  187. std::getline(output, expected);
  188. std::getline(str, found);
  189. bool ok = (expected != found);
  190. status+= ok ;
  191. (ok ? std::cerr : std::cout)
  192. << (not ok ? "\033[21;32m" : "\033[1;31m") << expected
  193. << std::string(std::max<size_t>(0, 40 - expected.size()), ' ')<< "| " << found << std::endl;
  194. }
  195. if(status) std::exit(status);
  196. }
  197. void immediate_interactive() {
  198. auto engine = scripting::prepare_interpreter(std::string{});
  199. engine->registerFunction("identity", std::make_unique<identity>());
  200. engine->registerFunction("exit", std::make_unique<terminate>());
  201. engine->registerFunction("set", std::make_unique<set>());
  202. engine->registerFunction("print", std::make_unique<print>(std::cout));
  203. bool exit = false;
  204. while (not exit) {
  205. std::string code;
  206. std::getline(std::cin, code);
  207. auto res = engine->executeAtOnce(code);
  208. if (std::holds_alternative<scripting::script_value>(res)) {
  209. } else {
  210. auto &errors = std::get<std::vector<scripting::script_error>>(res);
  211. for (auto &line: errors) {
  212. std::cout << line.message << "\n at line ";
  213. if(line.location) {
  214. std::cout << line.location->line_number << ":"
  215. << line.location->column_number << "\n";
  216. std::cout << " " << *line.location->line_contents << "\n";
  217. std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n";
  218. } else std::cout << "UNKNOWN\n";
  219. }
  220. }
  221. }
  222. }
  223. void exec(std::span<std::string_view> args) {
  224. std::vector<decltype(scripting::prepare_interpreter(std::string{}))> batch;
  225. auto engine = scripting::prepare_interpreter(std::string{});
  226. engine->registerFunction("identity", std::make_unique<identity>());
  227. engine->registerFunction("terminate", std::make_unique<terminate>());
  228. engine->registerFunction("set", std::make_unique<set>());
  229. engine->registerFunction("print", std::make_unique<print>(std::cout));
  230. bool exit = false;
  231. while (not exit) {
  232. std::string code;
  233. std::getline(std::cin, code);
  234. auto res = engine->executeAtOnce(code);
  235. if (std::holds_alternative<scripting::script_value>(res)) {
  236. } else {
  237. auto &errors = std::get<std::vector<scripting::script_error>>(res);
  238. for (auto &line: errors) {
  239. std::cout << line.message << "\n at line ";
  240. if(line.location) {
  241. std::cout << line.location->line_number << ":"
  242. << line.location->column_number << "\n";
  243. std::cout << " " << *line.location->line_contents << "\n";
  244. std::cout << " " << std::string(line.location->column_number - 1, ' ') << "^\n";
  245. } else std::cout << "UNKNOWN\n";
  246. }
  247. }
  248. }
  249. }
  250. #if defined(__linux__) or defined(WIN32)
  251. constexpr bool trim_first_argument = true;
  252. #else
  253. constexpr bool trim_first_argument = false;
  254. static_assert(false, "Undefined status of the first argument");
  255. #endif
  256. int cpp_main(std::span<std::string_view> args) {
  257. if constexpr (trim_first_argument) {
  258. args = args.subspan(1);
  259. }
  260. if(args.empty() || args.front() == "immediate") {
  261. immediate_interactive();
  262. std::exit(0);
  263. } else if(args.front() == "compare") {
  264. args = args.subspan(1);
  265. if(args.size() != 2) {
  266. std::cerr << "compare expects 2 files as arguments" << std::endl;
  267. std::terminate();
  268. }
  269. } else if(args.front() == "bench_exec") {
  270. args = args.subspan(1);
  271. if(args.size() > 1) {
  272. std::cerr << "bench_exec expects 0 or 1 file as arguments" << std::endl;
  273. std::terminate();
  274. }
  275. if(args.empty()) process_bench();
  276. else process_bench(std::string{args.front()});
  277. } else if(args.front() == "bench_compile") {
  278. args = args.subspan(1);
  279. if(args.size() > 1) {
  280. std::cerr << "bench_compile expects 0 or 1 file as arguments" << std::endl;
  281. std::terminate();
  282. }
  283. if(args.empty()) compile_bench();
  284. else compile_bench(std::string{args.front()});
  285. } else if(args.front() == "exec") {
  286. // exec(args.subspan(1));
  287. } else {
  288. std::cerr << "Unknown option" << std::endl;
  289. }
  290. return 0;
  291. }
  292. int main(int argc, char** argv) {
  293. std::vector<std::string_view> args;
  294. for(auto& arg : std::span(argv, argv+argc)) {
  295. args.emplace_back(arg, arg+strlen(arg));
  296. }
  297. return cpp_main(args);
  298. }