コード例 #1
0
ファイル: Parser.cs プロジェクト: strogo/neolua
        private static void ParseConst(Scope scope, LuaLexer code)
        {
            // const ::= variable '=' ( expr | clr '.' Type )

            Token tVarName;
            Type typeVar;
            ParseIdentifierAndType(scope, code, out tVarName, out typeVar);

            if (code.Current.Typ == LuaToken.Identifier || code.Current.Value == "typeof")
            {
                code.Next();

                // Parse the type
                scope.RegisterConst(tVarName.Value, Expression.Constant(ParseType(scope, code, false)));
            }
            else
            {
                FetchToken(LuaToken.Assign, code);

                Expression exprConst = ParseExpression(scope, code, InvokeResult.Object, false); // No Debug-Emits
                if (typeVar != typeof(object))
                    exprConst = ConvertExpression(scope.Runtime, tVarName, exprConst, typeVar);

                // Try to eval the statement
                if (exprConst.Type == typeof(object) || exprConst.Type == typeof(LuaResult)) // dynamic calls, no constant possible
                    throw ParseError(tVarName, Properties.Resources.rsConstExpressionNeeded);
                else
                    try
                    {
                        object r = EvaluateExpression(exprConst);
                        if (r == null) // Eval via compile
                        {
                            Type typeFunc = Expression.GetFuncType(exprConst.Type);
                            LambdaExpression exprEval = Expression.Lambda(typeFunc, exprConst);
                            Delegate dlg = exprEval.Compile();
                            r = dlg.DynamicInvoke();
                        }
                        scope.RegisterConst(tVarName.Value, Expression.Constant(r, exprConst.Type));
                    }
                    catch (Exception e)
                    {
                        throw ParseError(tVarName, String.Format(Properties.Resources.rsConstExpressionEvalError, e.Message));
                    }
            }
        }