Add --check/-c parameter to c4script for syntax checking

ipv6
Lukas Werling 2016-12-23 17:05:59 +01:00
parent 3aaf216582
commit 61962fe70a
3 changed files with 69 additions and 16 deletions

View File

@ -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

View File

@ -19,6 +19,7 @@
#include <stdio.h>
#include <string.h>
#include <getopt.h>
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]);
}
}

View File

@ -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("<memory>", 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); }