Example #1
0
        Type EmitAssignment(CodeExpression Left, CodeExpression Right, bool ForceTypes)
        {
            Depth++;
            Debug("Emitting assignment expression");
            Type Generated = typeof(void);

            if (Left is CodeVariableReferenceExpression)
            {
                // local IL variables generated by parser
                var Reference = Left as CodeVariableReferenceExpression;

                LocalBuilder Var;
                if (Locals.ContainsKey(Reference.VariableName))
                {
                    Var = Locals[Reference.VariableName];
                }
                else
                {
                    Var = Generator.DeclareLocal(typeof(int));
                    Locals.Add(Reference.VariableName, Var);
                }

                EmitExpression(Right, ForceTypes);
                Generator.Emit(OpCodes.Stloc, Var);
                Generator.Emit(OpCodes.Pop);
            }
            else if (Left is CodeArrayIndexerExpression)
            {
                var index = (CodeArrayIndexerExpression)Left;

                Generator.Emit(OpCodes.Ldloc, VarsProperty);

                EmitExpression(index.Indices[0]);
                Type resultType = EmitExpression(Right, ForceTypes);
                if (resultType.IsValueType)
                {
                    Generator.Emit(OpCodes.Box, resultType);
                }

                Generator.Emit(OpCodes.Callvirt, SetVariable);

                Generated = typeof(object);
            }
            else if (Left is CodePropertyReferenceExpression)
            {
                var prop = (CodePropertyReferenceExpression)Left;

                // HACK: property set method target
                var info = typeof(Rusty.Core).GetProperty(prop.PropertyName);

                if (Mirror != null)
                {
                    info = Mirror.GrabProperty(info);
                }

                var set = info == null ? null : info.GetSetMethod();

                if (set == null)
                {
                    Generator.Emit(OpCodes.Ldnull);
                }
                else
                {
                    EmitExpression(Right);
                    Generator.Emit(OpCodes.Dup);
                    Generator.Emit(OpCodes.Call, set);
                }

                Generated = typeof(object);
            }
            else
            {
                throw new CompileException(Left, "Left hand is unassignable");
            }

            Depth--;

            return(Generated);
        }