Skip to content

Commit

Permalink
Chg: Lua constant script interface
Browse files Browse the repository at this point in the history
  • Loading branch information
neolithos committed Dec 29, 2018
1 parent 62f986c commit 3ca4e2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion NeoLua.NuGet/common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryType>git</RepositoryType>

<AssemblyVersion>5.3.0.0</AssemblyVersion>
<FileVersion>1.3.4.0</FileVersion>
<FileVersion>1.3.5.0</FileVersion>

<VersionAdd>beta</VersionAdd>
<SimpleVersionPattern>^(\d+)\.(\d+)\.(\d+)</SimpleVersionPattern>
Expand Down
5 changes: 2 additions & 3 deletions NeoLua.Test/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,8 @@ public void TestPosition02()
public void ParsePlainTest()
{
HtmlTokenTest("<html> < a% >",
T(LuaToken.KwReturn, "return"),
T(LuaToken.String, "<html> < a% >"),
T(LuaToken.Semicolon, String.Empty)
T(LuaToken.Identifier, "print"),
T(LuaToken.String, "<html> < a% >")
);
}

Expand Down
43 changes: 32 additions & 11 deletions NeoLua/Lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,26 +238,47 @@ public LuaChunk CompileChunk(string code, string name, LuaCompileOptions options
return CompileChunkCore(lex, options, args);
} // func CompileChunk

/// <summary>Create a code delegate without executing it.</summary>
/// <param name="code">Code of the delegate..</param>
/// <param name="options">Options for the compile process.</param>
/// <param name="args">Arguments for the code block.</param>
/// <returns>Compiled chunk.</returns>
public LuaChunk CompileChunk(ILuaLexer code, LuaCompileOptions options, params KeyValuePair<string, Type>[] args)
{
if (code == null)
throw new ArgumentNullException(nameof(code));
return CompileChunkCore(code, options, args);
} // func CompileChunk

/// <summary>Creates a code delegate or returns a single return constant.</summary>
/// <param name="code"></param>
/// <param name="options"></param>
/// <param name="args"></param>
/// <returns></returns>
public object CompileOrReturnConstant(ILuaLexer code, LuaCompileOptions options, params KeyValuePair<string, Type>[] args)
public object CompileOrReturnPrint(ILuaLexer code, LuaCompileOptions options, params KeyValuePair<string, Type>[] 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

/// <summary>Test for single print expression.</summary>
/// <param name="code"></param>
/// <returns></returns>
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<KeyValuePair<string, Type>> args)
{
Expand Down Expand Up @@ -381,10 +402,10 @@ public object ParseNumber(string number)

/// <summary>Parses a string to a lua number.</summary>
/// <param name="number">String representation of the number.</param>
/// <param name="iBase">Base fore the number</param>
/// <param name="toBase">Base fore the number</param>
/// <returns></returns>
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;

Expand Down
3 changes: 1 addition & 2 deletions NeoLua/LuaLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,9 +1602,8 @@ private static IEnumerable<Token> 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
Expand Down

0 comments on commit 3ca4e2e

Please sign in to comment.