From 3ca4e2e4a7a2b52188801f309fba10184c79d882 Mon Sep 17 00:00:00 2001 From: neolithos Date: Sat, 29 Dec 2018 11:37:35 +0100 Subject: [PATCH] Chg: Lua constant script interface --- NeoLua.NuGet/common.targets | 2 +- NeoLua.Test/Parser.cs | 5 ++--- NeoLua/Lua.cs | 43 +++++++++++++++++++++++++++---------- NeoLua/LuaLexer.cs | 3 +-- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/NeoLua.NuGet/common.targets b/NeoLua.NuGet/common.targets index 1794538..daf6de6 100644 --- a/NeoLua.NuGet/common.targets +++ b/NeoLua.NuGet/common.targets @@ -12,7 +12,7 @@ git 5.3.0.0 - 1.3.4.0 + 1.3.5.0 beta ^(\d+)\.(\d+)\.(\d+) diff --git a/NeoLua.Test/Parser.cs b/NeoLua.Test/Parser.cs index 2b0029d..73e443a 100644 --- a/NeoLua.Test/Parser.cs +++ b/NeoLua.Test/Parser.cs @@ -340,9 +340,8 @@ public void TestPosition02() public void ParsePlainTest() { HtmlTokenTest(" < a% >", - T(LuaToken.KwReturn, "return"), - T(LuaToken.String, " < a% >"), - T(LuaToken.Semicolon, String.Empty) + T(LuaToken.Identifier, "print"), + T(LuaToken.String, " < a% >") ); } diff --git a/NeoLua/Lua.cs b/NeoLua/Lua.cs index 4f6e4a0..960fefa 100644 --- a/NeoLua/Lua.cs +++ b/NeoLua/Lua.cs @@ -238,26 +238,47 @@ public LuaChunk CompileChunk(string code, string name, LuaCompileOptions options return CompileChunkCore(lex, options, args); } // func CompileChunk + /// Create a code delegate without executing it. + /// Code of the delegate.. + /// Options for the compile process. + /// Arguments for the code block. + /// Compiled chunk. + public LuaChunk CompileChunk(ILuaLexer code, LuaCompileOptions options, params KeyValuePair[] args) + { + if (code == null) + throw new ArgumentNullException(nameof(code)); + return CompileChunkCore(code, options, args); + } // func CompileChunk + /// Creates a code delegate or returns a single return constant. /// /// /// /// - public object CompileOrReturnConstant(ILuaLexer code, LuaCompileOptions options, params KeyValuePair[] args) + public object CompileOrReturnPrint(ILuaLexer code, LuaCompileOptions options, params KeyValuePair[] args) { - code.Next(); // get first token - - if (code.Current.Typ == LuaToken.KwReturn // is first token a return - && (code.LookAhead.Typ == LuaToken.String || code.LookAhead.Typ == LuaToken.Number) // we expect a string or number - && (code.LookAhead2.Typ == LuaToken.Semicolon || code.LookAhead2.Typ == LuaToken.Eof)) // eof + if (IsConstantScript(code)) // eof { return code.LookAhead.Typ == LuaToken.String ? code.LookAhead.Value - : RtParseNumber(code.LookAhead.Value, FloatType == LuaFloatType.Double); + : ParseNumber(code.LookAhead.Value); } else return CompileChunkCore(code, options, args); - } // func CompileOrReturnConstant + } // func CompileOrReturnPrint + + /// Test for single print expression. + /// + /// + public static bool IsConstantScript(ILuaLexer code) + { + if (code.Current == null) + code.Next(); + + return (code.Current.Typ == LuaToken.Identifier && code.Current.Value == "print") // is first token is print + && (code.LookAhead.Typ == LuaToken.String || code.LookAhead.Typ == LuaToken.Number) // we expect a string or number + && (code.LookAhead2.Typ == LuaToken.Eof); + } // func IsConstantScript internal LuaChunk CompileChunkCore(ILuaLexer lex, LuaCompileOptions options, IEnumerable> args) { @@ -381,10 +402,10 @@ public object ParseNumber(string number) /// Parses a string to a lua number. /// String representation of the number. - /// Base fore the number + /// Base fore the number /// - public object ParseNumber(string number, int iBase) - => RtParseNumber(null, number, 0, iBase, FloatType == LuaFloatType.Double, false); + public object ParseNumber(string number, int toBase) + => RtParseNumber(null, number, 0, toBase, FloatType == LuaFloatType.Double, false); internal int NumberType => numberType; diff --git a/NeoLua/LuaLexer.cs b/NeoLua/LuaLexer.cs index 8b66b2b..a97d070 100644 --- a/NeoLua/LuaLexer.cs +++ b/NeoLua/LuaLexer.cs @@ -1602,9 +1602,8 @@ private static IEnumerable CreateHtmlTokenStream(LuaCharLexer chars, bool } else // no code emitted --> create a return statement { - yield return chars.CreateTokenAtStart(LuaToken.KwReturn); + yield return chars.CreateTokenAtStart(LuaToken.Identifier, "print"); yield return chars.CreateToken(LuaToken.String); - yield return chars.CreateTokenAtStart(LuaToken.Semicolon); } yield return chars.CreateToken(LuaToken.Eof); } // func CreateHtmlTokenStream