From fde7e7ab7afd319675ebc68235ab7aaf7f937025 Mon Sep 17 00:00:00 2001 From: Ivan Mogilko Date: Thu, 31 Oct 2024 00:24:03 +0300 Subject: [PATCH 1/2] AGS.Native: added IScriptCompiler interface, and a standard impl IScriptCompiler is a compiler interface that lets to address any available compiler. Implementation AGS3ScriptCompiler, which basically wrap calls to the native compiler lib. NOTE: There's already IScriptCompiler in AGS.CScript.Compiler managed library. But it appears to be complicated to use at the moment, because it has number of ties with existing code, including separate message (error) types. I left big TODO comments about merging these interfaces, would someone be willing to improve this situation. This probably will have to be done anyway would Editor work with standalone compilers. --- Common/script/cc_common.h | 24 +- Compiler/script/cs_compiler.cpp | 13 +- Compiler/script/cs_compiler.h | 3 + .../AGS.CScript.Compiler/IScriptCompiler.cs | 25 +- Editor/AGS.Editor/AGSEditor.cs | 93 ++++-- Editor/AGS.Editor/NativeProxy.cs | 4 +- Editor/AGS.Native/CompiledScript.h | 123 ++++++++ Editor/AGS.Native/IScriptCompiler.h | 73 +++++ Editor/AGS.Native/NativeDLL.vcxproj | 3 +- Editor/AGS.Native/NativeMethods.h | 4 +- Editor/AGS.Native/ScriptCompiler.cpp | 264 ++++++++++-------- Editor/AGS.Native/Scripting.h | 119 -------- Editor/AGS.Native/agsnative.cpp | 2 +- 13 files changed, 483 insertions(+), 267 deletions(-) create mode 100644 Editor/AGS.Native/CompiledScript.h create mode 100644 Editor/AGS.Native/IScriptCompiler.h delete mode 100644 Editor/AGS.Native/Scripting.h diff --git a/Common/script/cc_common.h b/Common/script/cc_common.h index 703f21e4c2..407fa8914e 100644 --- a/Common/script/cc_common.h +++ b/Common/script/cc_common.h @@ -20,15 +20,21 @@ #include "util/string.h" -#define SCOPT_EXPORTALL 1 // export all functions automatically -#define SCOPT_SHOWWARNINGS 2 // printf warnings to console -#define SCOPT_LINENUMBERS 4 // include line numbers in compiled code -#define SCOPT_AUTOIMPORT 8 // when creating instance, export funcs to other scripts -#define SCOPT_DEBUGRUN 0x10 // write instructions as they are procssed to log file -#define SCOPT_NOIMPORTOVERRIDE 0x20 // do not allow an import to be re-declared -#define SCOPT_LEFTTORIGHT 0x40 // left-to-right operator precedance -#define SCOPT_OLDSTRINGS 0x80 // allow old-style strings -#define SCOPT_UTF8 0x100 // UTF-8 text mode +// FIXME: SCOPT_AUTOIMPORT and SCOPT_DEBUGRUN are runtime only options +// remove them from this list of flags, and use a different way of assigning, +// not using ccSetOption. + +#define SCOPT_EXPORTALL 0x0001 // export all functions automatically +// 0x0002 [UNUSED] +#define SCOPT_LINENUMBERS 0x0004 // include line numbers in compiled code +#define SCOPT_AUTOIMPORT 0x0008 // when creating instance, export funcs to other scripts +#define SCOPT_DEBUGRUN 0x0010 // write instructions as they are procssed to log file +// TODO: this flag might have to be removed as it makes inconsistent rules for distinct scripts +#define SCOPT_NOIMPORTOVERRIDE 0x0020 // do not allow an import to be re-declared +#define SCOPT_LEFTTORIGHT 0x0040 // left-to-right operator precedance +#define SCOPT_OLDSTRINGS 0x0080 // allow old-style strings +#define SCOPT_UTF8 0x0100 // UTF-8 text mode +#define SCOPT_HIGHEST SCOPT_UTF8 extern void ccSetOption(int, int); extern int ccGetOption(int); diff --git a/Compiler/script/cs_compiler.cpp b/Compiler/script/cs_compiler.cpp index 6952472a0c..c357713dd2 100644 --- a/Compiler/script/cs_compiler.cpp +++ b/Compiler/script/cs_compiler.cpp @@ -27,6 +27,12 @@ const char *ccCurScriptName = ""; std::vector defaultheaders; std::vector defaultHeaderNames; +void ccGetExtensions(std::vector &exts) +{ + // Add extensions here as necessary + return; +} + int ccAddDefaultHeader(const char* nhead, const char *nName) { defaultheaders.push_back(nhead); @@ -100,10 +106,9 @@ ccScript* ccCompileText(const char *texo, const char *scriptName) { (sym.get_type(t) != SYM_LOCALVAR)) continue; if (sym.entries[t].flags & SFLG_IMPORTED) continue; - if (ccGetOption(SCOPT_SHOWWARNINGS)==0) ; - else if ((sym.entries[t].flags & SFLG_ACCESSED)==0) { - printf("warning: variable '%s' is never used\n",sym.get_friendly_name(t).c_str()); - } + //if ((sym.entries[t].flags & SFLG_ACCESSED)==0) { + // printf("warning: variable '%s' is never used\n",sym.get_friendly_name(t).c_str()); + //} } if (ccGetOption(SCOPT_EXPORTALL)) { diff --git a/Compiler/script/cs_compiler.h b/Compiler/script/cs_compiler.h index 8a2939dcc4..a2510b5336 100644 --- a/Compiler/script/cs_compiler.h +++ b/Compiler/script/cs_compiler.h @@ -21,6 +21,9 @@ #include "script/cc_script.h" // ccScript // ********* SCRIPT COMPILATION FUNCTIONS ************** +// Get a list of compiler extensions. +extern void ccGetExtensions(std::vector &exts); + // add a script that will be compiled as a header into every compilation // 'name' is the name of the header, used in error reports // (only the pointer is stored so don't free the memory) diff --git a/Editor/AGS.CScript.Compiler/IScriptCompiler.cs b/Editor/AGS.CScript.Compiler/IScriptCompiler.cs index d199f4b3c8..084bf37e36 100644 --- a/Editor/AGS.CScript.Compiler/IScriptCompiler.cs +++ b/Editor/AGS.CScript.Compiler/IScriptCompiler.cs @@ -4,7 +4,30 @@ namespace AGS.CScript.Compiler { - public interface IScriptCompiler + // TODO: merge this with AGS.Native.IScriptCompiler! + // I wrote a new variant of IScriptCompiler inside AGS.Native lib, because + // this one has certain ties within CScript.Compiler classes. + // The only implementation of IScriptCompiler in here is not completed and + // was never used in AGS Editor (apparently, not sure if possible to test). + // + // Proposal: rework CompileResults, make it contain CompiledScript and + // CompileMessages collection. Merge or substitute CompilerMessage with + // AGS.Types.CompileMessage. But note that CompilerMessage also has + // Error Codes! Need to figure out how to make that shareable without strict + // bind to the Compiler lib. + // Re CompiledScript, see if possible to merge with AGS.Native.CompiledScript, + // which has data stored as native ccScript (?). Need to think this through. + // + // Important: IScriptCompiler implementation does not necessarily has actual + // compiler code inside. It may instead be a wrapper over a code that starts + // **EXTERNAL** compiler, i.e. runs a standalone executable, passing all + // parameters through command line. + // Need to think through how to receive results in that case. Maybe CompileResults + // could contain optionally an object that has compiled data in memory **AND** + // optionally a path to compiled file. And interface user would test which one + // is valid. That's just an idea for now. + + public interface IScriptCompiler { CompileResults CompileScript(string script); } diff --git a/Editor/AGS.Editor/AGSEditor.cs b/Editor/AGS.Editor/AGSEditor.cs index 542a76e642..5a015b8e34 100644 --- a/Editor/AGS.Editor/AGSEditor.cs +++ b/Editor/AGS.Editor/AGSEditor.cs @@ -17,6 +17,8 @@ namespace AGS.Editor { + using ScriptCompilerOptions = AGS.Native.ScriptCompilerOptions; + public class AGSEditor { public event GetScriptHeaderListHandler GetScriptHeaderList; @@ -858,15 +860,53 @@ private void DefineMacrosAccordingToGameSettings(IPreprocessor preprocessor) } } - /// - /// Preprocesses and then compiles the script using the supplied headers. - /// - public void CompileScript(Script script, List