From 61962fe70abd22a665446cc09fcef163bf0e3862 Mon Sep 17 00:00:00 2001 From: Lukas Werling Date: Fri, 23 Dec 2016 17:05:59 +0100 Subject: [PATCH] Add --check/-c parameter to c4script for syntax checking --- include/c4script/c4script.h | 3 ++ src/script/C4ScriptMain.cpp | 47 +++++++++++++++++++++++++++---- src/script/C4ScriptStandalone.cpp | 35 ++++++++++++++++------- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/include/c4script/c4script.h b/include/c4script/c4script.h index 3eb88ee37..d007c5282 100644 --- a/include/c4script/c4script.h +++ b/include/c4script/c4script.h @@ -23,6 +23,9 @@ extern "C" { int c4s_runfile(const char *filename); int c4s_runstring(const char *script); +int c4s_checkfile(const char *filename); +int c4s_checkstring(const char *script); + #ifdef __cplusplus } #endif diff --git a/src/script/C4ScriptMain.cpp b/src/script/C4ScriptMain.cpp index ab39292ff..f900fb8f0 100644 --- a/src/script/C4ScriptMain.cpp +++ b/src/script/C4ScriptMain.cpp @@ -19,6 +19,7 @@ #include #include +#include int usage(const char *argv0) { @@ -26,21 +27,55 @@ int usage(const char *argv0) return 1; } -int main(int argc, const char * argv[]) +int main(int argc, char *const argv[]) { if (argc < 2) return usage(argv[0]); - if (strcmp(argv[1], "-e") == 0) + bool check = false; + char *runstring = nullptr; + + while (1) { - if (argc != 3) + static option long_options[] = + { + {"check", no_argument, 0, 'c'}, + {"execute", required_argument, 0, 'e'}, + {0, 0, 0, 0} + }; + + int option_index; + int c = getopt_long(argc, argv, "ce:", long_options, &option_index); + if (c == -1) break; + switch (c) + { + case 'c': + check = true; + break; + case 'e': + runstring = optarg; + break; + default: return usage(argv[0]); - return c4s_runstring(argv[2]); + } + } + + if (runstring) + { + if (argc - optind != 0) + return usage(argv[0]); + if (check) + return c4s_checkstring(runstring); + else + return c4s_runstring(runstring); } else { - if (argc != 2) + if (argc - optind != 1) return usage(argv[0]); - return c4s_runfile(argv[1]); + if (check) + return c4s_checkfile(argv[optind]); + else + return c4s_runfile(argv[optind]); } } diff --git a/src/script/C4ScriptStandalone.cpp b/src/script/C4ScriptStandalone.cpp index 361b2f74c..3ee70085b 100644 --- a/src/script/C4ScriptStandalone.cpp +++ b/src/script/C4ScriptStandalone.cpp @@ -31,7 +31,7 @@ void C4DefList::SortByPriority() {} void C4DefList::CallEveryDefinition() {} void C4DefList::ResetIncludeDependencies() {} -void InitializeC4Script() +static void InitializeC4Script() { InitCoreFunctionMap(&ScriptEngine); @@ -39,18 +39,22 @@ void InitializeC4Script() FixedRandom(time(nullptr)); } -C4Value RunLoadedC4Script() +static void ClearC4Script() +{ + GameScript.Clear(); + ScriptEngine.Clear(); +} + +static C4Value RunLoadedC4Script() { // Link script engine (resolve includes/appends, generate code) ScriptEngine.Link(nullptr); C4Value result = GameScript.Call("Main"); - GameScript.Clear(); - ScriptEngine.Clear(); return result; } -int c4s_runfile(const char * filename) +static int RunFile(const char * filename, bool checkOnly) { C4Group File; if (!File.Open(GetWorkingDirectory())) @@ -70,14 +74,25 @@ int c4s_runfile(const char * filename) InitializeC4Script(); GameScript.Load(File, fn.getData(), nullptr, nullptr); - RunLoadedC4Script(); - return 0; + if (!checkOnly) + RunLoadedC4Script(); + ClearC4Script(); + return ScriptEngine.errCnt; } -int c4s_runstring(const char *script) +static int RunString(const char *script, bool checkOnly) { InitializeC4Script(); GameScript.LoadData("", script, nullptr); - RunLoadedC4Script(); - return 0; + if (!checkOnly) + RunLoadedC4Script(); + ClearC4Script(); + return ScriptEngine.errCnt; } + + +int c4s_runfile(const char *filename) { return RunFile(filename, false); } +int c4s_runstring(const char *script) { return RunString(script, false); } + +int c4s_checkfile(const char *filename) { return RunFile(filename, true); } +int c4s_checkstring(const char *script) { return RunString(script, true); }