Permalink
Browse files

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

  • Loading branch information...
lluchs committed Dec 23, 2016
1 parent 3aaf216 commit 61962fe70abd22a665446cc09fcef163bf0e3862
Showing with 69 additions and 16 deletions.
  1. +3 −0 include/c4script/c4script.h
  2. +41 −6 src/script/C4ScriptMain.cpp
  3. +25 −10 src/script/C4ScriptStandalone.cpp
@@ -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
@@ -19,28 +19,63 @@
#include <stdio.h>
#include <string.h>
#include <getopt.h>
int usage(const char *argv0)
{
fprintf(stderr, "Usage:\n%s -e <script>\n%s <file>\n", argv0, 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)
{
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]);
}
}
if (runstring)
{
if (argc != 3)
if (argc - optind != 0)
return usage(argv[0]);
return c4s_runstring(argv[2]);
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]);
}
}
@@ -31,26 +31,30 @@ void C4DefList::SortByPriority() {}
void C4DefList::CallEveryDefinition() {}
void C4DefList::ResetIncludeDependencies() {}
void InitializeC4Script()
static void InitializeC4Script()
{
InitCoreFunctionMap(&ScriptEngine);
// Seed PRNG
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); }

0 comments on commit 61962fe

Please sign in to comment.