#include "pixelscript.h"
#include <string>
#include <iostream>
pxs_VarT println(pxs_VarT args) {
std::string msg;
for (int i = 0; i < pxs_listlen(args) - 1; i++) {
pxs_VarT res = pxs_tostring(pxs_listget(args, 0), pxs_listget(args, 1));
if (pxs_varis(res, pxs_VarType::pxs_Exception)) {
return res;
}
char* raw = pxs_getstring(res);
msg += std::string(raw);
pxs_freestr(raw);
}
std::cout << msg << std::endl;
return pxs_newnull();
}
pxs_VarT eval(pxs_VarT args) {
int runtime = pxs_getint(pxs_listget(args, 1));
char* script = pxs_getstring(pxs_listget(args, 2));
auto res = pxs_eval(script, static_cast<pxs_Runtime>(runtime));
pxs_freestr(script);
return res;
}
int main(int argc) {
pxs_Runtime runtime;
if (argc <= 1) {
runtime = pxs_Python;
} else {
runtime = pxs_Lua;
}
pxs_initialize();
pxs_Module* pxs_module = pxs_newmod("pxs");
pxs_addfunc(pxs_module, "println", println);
pxs_addfunc(pxs_module, "eval", eval);
pxs_addmod(pxs_module);
std::string full;
while (true) {
std::string input;
std::cout << ">> ";
if (!std::getline(std::cin, input) || input == "exit") {
break;
}
if (input.empty()) {
continue;
}
full += "\n" + input;
pxs_VarT res = pxs_exec(runtime, full.c_str(), "<test>");
if (!pxs_varis(res, pxs_VarType::pxs_Null)) {
char* msg = pxs_getstring(res);
std::cout << msg << std::endl;
pxs_freestr(msg);
}
}
pxs_finalize();
}