public static object GetMember(string value, Dictionary <string, Type> definedTypes, LocalLookup locals,
                                       Type currentType, AssemblyBuilder asmBuilder,
                                       BindingFlags modifiers  = BindingFlags.Public | BindingFlags.NonPublic,
                                       MemberTypes memberTypes = MemberTypes.Field | MemberTypes.Method)
        {
            if (value.StartsWith("\""))
            {
                return(typeof(string), value.Trim('"'));
            }
            if (int.TryParse(value, out var i))
            {
                return(typeof(int), i.ToString());
            }
            if (float.TryParse(value, out var f))
            {
                return(typeof(float), f.ToString());
            }
            if (double.TryParse(value, out var d))
            {
                return(typeof(double), d.ToString());
            }

            if (locals.Names.Contains(value))
            {
                return(locals[value]);
            }
            if (definedTypes.ContainsKey(value))
            {
                return(definedTypes[value]);
            }

            return(DynamicMembers.GetInstance().GetMembers(currentType.Name).First(member =>
                                                                                   member.Name == value &&
                                                                                   (member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Method)));
        }
 public static DynamicMembers GetInstance()
 {
     if (instance == null)
     {
         instance = new DynamicMembers();
     }
     return(instance);
 }
Exemple #3
0
        public override void Execute(IList <Token> tokens, ref int i)
        {
            if (tokens[i].TokenType == TokenType.Type)
            {
                Type = tokens[i++].Value;
            }
            else if (tokens[i].TokenType == TokenType.Identifier)
            {
                Name = tokens[i++].Value;
                var fieldAttributes = ModifierCollection.GetFieldAttributes(_modifs);
                var field           = ((TypeBuilder)TypeBuilder).DefineField(Name, DefinedTypes[Type], fieldAttributes);

                DynamicMembers.GetInstance().AddMember(TypeBuilder.Name, field);
                StateStack.Pop();
            }
            else
            {
                //todo UnexpectedToken
                ExceptionManager.ThrowCompiler(ErrorCode.UnexpectedToken, "", tokens[i].Line);
            }
        }