Skip to content

Commit 3ca4e2e

Browse files
committed
Chg: Lua constant script interface
1 parent 62f986c commit 3ca4e2e

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

NeoLua.NuGet/common.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryType>git</RepositoryType>
1313

1414
<AssemblyVersion>5.3.0.0</AssemblyVersion>
15-
<FileVersion>1.3.4.0</FileVersion>
15+
<FileVersion>1.3.5.0</FileVersion>
1616

1717
<VersionAdd>beta</VersionAdd>
1818
<SimpleVersionPattern>^(\d+)\.(\d+)\.(\d+)</SimpleVersionPattern>

NeoLua.Test/Parser.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,8 @@ public void TestPosition02()
340340
public void ParsePlainTest()
341341
{
342342
HtmlTokenTest("<html> < a% >",
343-
T(LuaToken.KwReturn, "return"),
344-
T(LuaToken.String, "<html> < a% >"),
345-
T(LuaToken.Semicolon, String.Empty)
343+
T(LuaToken.Identifier, "print"),
344+
T(LuaToken.String, "<html> < a% >")
346345
);
347346
}
348347

NeoLua/Lua.cs

+32-11
Original file line numberDiff line numberDiff line change
@@ -238,26 +238,47 @@ public LuaChunk CompileChunk(string code, string name, LuaCompileOptions options
238238
return CompileChunkCore(lex, options, args);
239239
} // func CompileChunk
240240

241+
/// <summary>Create a code delegate without executing it.</summary>
242+
/// <param name="code">Code of the delegate..</param>
243+
/// <param name="options">Options for the compile process.</param>
244+
/// <param name="args">Arguments for the code block.</param>
245+
/// <returns>Compiled chunk.</returns>
246+
public LuaChunk CompileChunk(ILuaLexer code, LuaCompileOptions options, params KeyValuePair<string, Type>[] args)
247+
{
248+
if (code == null)
249+
throw new ArgumentNullException(nameof(code));
250+
return CompileChunkCore(code, options, args);
251+
} // func CompileChunk
252+
241253
/// <summary>Creates a code delegate or returns a single return constant.</summary>
242254
/// <param name="code"></param>
243255
/// <param name="options"></param>
244256
/// <param name="args"></param>
245257
/// <returns></returns>
246-
public object CompileOrReturnConstant(ILuaLexer code, LuaCompileOptions options, params KeyValuePair<string, Type>[] args)
258+
public object CompileOrReturnPrint(ILuaLexer code, LuaCompileOptions options, params KeyValuePair<string, Type>[] args)
247259
{
248-
code.Next(); // get first token
249-
250-
if (code.Current.Typ == LuaToken.KwReturn // is first token a return
251-
&& (code.LookAhead.Typ == LuaToken.String || code.LookAhead.Typ == LuaToken.Number) // we expect a string or number
252-
&& (code.LookAhead2.Typ == LuaToken.Semicolon || code.LookAhead2.Typ == LuaToken.Eof)) // eof
260+
if (IsConstantScript(code)) // eof
253261
{
254262
return code.LookAhead.Typ == LuaToken.String
255263
? code.LookAhead.Value
256-
: RtParseNumber(code.LookAhead.Value, FloatType == LuaFloatType.Double);
264+
: ParseNumber(code.LookAhead.Value);
257265
}
258266
else
259267
return CompileChunkCore(code, options, args);
260-
} // func CompileOrReturnConstant
268+
} // func CompileOrReturnPrint
269+
270+
/// <summary>Test for single print expression.</summary>
271+
/// <param name="code"></param>
272+
/// <returns></returns>
273+
public static bool IsConstantScript(ILuaLexer code)
274+
{
275+
if (code.Current == null)
276+
code.Next();
277+
278+
return (code.Current.Typ == LuaToken.Identifier && code.Current.Value == "print") // is first token is print
279+
&& (code.LookAhead.Typ == LuaToken.String || code.LookAhead.Typ == LuaToken.Number) // we expect a string or number
280+
&& (code.LookAhead2.Typ == LuaToken.Eof);
281+
} // func IsConstantScript
261282

262283
internal LuaChunk CompileChunkCore(ILuaLexer lex, LuaCompileOptions options, IEnumerable<KeyValuePair<string, Type>> args)
263284
{
@@ -381,10 +402,10 @@ public object ParseNumber(string number)
381402

382403
/// <summary>Parses a string to a lua number.</summary>
383404
/// <param name="number">String representation of the number.</param>
384-
/// <param name="iBase">Base fore the number</param>
405+
/// <param name="toBase">Base fore the number</param>
385406
/// <returns></returns>
386-
public object ParseNumber(string number, int iBase)
387-
=> RtParseNumber(null, number, 0, iBase, FloatType == LuaFloatType.Double, false);
407+
public object ParseNumber(string number, int toBase)
408+
=> RtParseNumber(null, number, 0, toBase, FloatType == LuaFloatType.Double, false);
388409

389410
internal int NumberType => numberType;
390411

NeoLua/LuaLexer.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1602,9 +1602,8 @@ private static IEnumerable<Token> CreateHtmlTokenStream(LuaCharLexer chars, bool
16021602
}
16031603
else // no code emitted --> create a return statement
16041604
{
1605-
yield return chars.CreateTokenAtStart(LuaToken.KwReturn);
1605+
yield return chars.CreateTokenAtStart(LuaToken.Identifier, "print");
16061606
yield return chars.CreateToken(LuaToken.String);
1607-
yield return chars.CreateTokenAtStart(LuaToken.Semicolon);
16081607
}
16091608
yield return chars.CreateToken(LuaToken.Eof);
16101609
} // func CreateHtmlTokenStream

0 commit comments

Comments
 (0)