From 8e04b3fc014844b64d12f632381b97792933453a Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Wed, 30 May 2018 20:08:10 -0700 Subject: [PATCH] Add eval command to console. --- src/console.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/console.c b/src/console.c index 950508a..819c3d4 100644 --- a/src/console.c +++ b/src/console.c @@ -25,6 +25,7 @@ #include "config.h" #include "ext/gif.h" #include "ext/file_dialog.h" +#include "machine.h" #include #include @@ -2216,6 +2217,28 @@ static void onConsoleResumeCommand(Console* console, const char* param) resumeRunMode(); } +static void onConsoleEvalCommand(Console* console, const char* param) +{ + printLine(console); + + tic_machine* machine = (tic_machine*)console->tic; + lua_State* lua = machine->lua; + + // TODO: check for other languages/runtimes? + if(lua) + { + if(luaL_dostring(lua, param) != LUA_OK) + { + printError(console, lua_tostring(lua, -1)); + } + lua_settop(lua, 0); + } + else + printError(console, "Lua state uninitialized.\n"); + + commandDone(console); +} + static void onAddFile(const char* name, AddResult result, void* data) { Console* console = (Console*)data; @@ -2418,6 +2441,7 @@ static const struct {"save", NULL, "save cart", onConsoleSaveCommand}, {"run", NULL, "run loaded cart", onConsoleRunCommand}, {"resume", NULL, "resume run cart", onConsoleResumeCommand}, + {"eval", "=", "run code", onConsoleEvalCommand}, {"dir", "ls", "show list of files", onConsoleDirCommand}, {"cd", NULL, "change directory", onConsoleChangeDirectory}, {"mkdir", NULL, "make directory", onConsoleMakeDirectory}, @@ -3310,4 +3334,4 @@ void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config, #endif console->active = !console->embed.yes; -} \ No newline at end of file +}