} // func T private void TokenTest(ILuaLexer lex, params KeyValuePair <LuaToken, string>[] expectedTokens) { using (lex) { lex.Next(); for (var i = 0; i < expectedTokens.Length; i++) { Debug.Write(String.Format("Test: {0} = {1} ==>", lex.Current.Typ, expectedTokens[i].Key)); if (lex.Current.Typ != expectedTokens[i].Key) { Debug.WriteLine("tokens FAILED"); Assert.Fail(); } else if (lex.Current.Value != expectedTokens[i].Value) { Debug.WriteLine("values '{0}' != '{1}' FAILED", lex.Current.Value, expectedTokens[i].Value); Assert.Fail(); } Debug.WriteLine("OK"); lex.Next(); } if (lex.Current.Typ != LuaToken.Eof) { Assert.Fail($"Invalid token {lex.Current.Typ} (Expected: Eof)"); } } } // func TokenTest
} // 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
} // 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
} // 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 CompileOrReturnPrint(ILuaLexer code, LuaCompileOptions options, params KeyValuePair <string, Type>[] args) { if (IsConstantScript(code)) // eof { return(code.LookAhead.Typ == LuaToken.String ? code.LookAhead.Value : ParseNumber(code.LookAhead.Value)); } else { return(CompileChunkCore(code, options, args)); } } // func CompileOrReturnPrint
} // func IsConstantScript internal LuaChunk CompileChunkCore(ILuaLexer lex, LuaCompileOptions options, IEnumerable <KeyValuePair <string, Type> > args) { if (options == null) { options = new LuaCompileOptions(); } var registerMethods = options.DebugEngine != null && (options.DebugEngine.Level & LuaDebugLevel.RegisterMethods) == LuaDebugLevel.RegisterMethods; if (registerMethods) { BeginCompile(); } try { var expr = Parser.ParseChunk(this, options, true, lex, null, typeof(LuaResult), args); if (printExpressionTree != null) { printExpressionTree.WriteLine(Parser.ExpressionToString(expr)); printExpressionTree.WriteLine(new string('=', 79)); } // compile the chunk return(options.DebugEngine == null ? new LuaChunk(this, expr.Name, expr.Compile()) : options.DebugEngine.CreateChunk(this, expr)); } finally { if (registerMethods) { EndCompile(); } } } // func CompileChunkCore