Пример #1
0
        public DefinedEnum(ParseInfo parseInfo, EnumContext enumContext) : base(enumContext.Identifier.Text)
        {
            CanBeExtended = false;
            CanBeDeleted  = false;
            Kind          = "enum";

            // Check if a type with the same name already exists.
            if (parseInfo.TranslateInfo.Types.IsCodeType(Name))
            {
                parseInfo.Script.Diagnostics.Error($"A type with the name '{Name}' already exists.", enumContext.Identifier.Range);
            }

            _translateInfo = parseInfo.TranslateInfo;
            Scope          = new Scope("enum " + Name);

            // Set location and symbol link.
            DefinedAt = new Location(parseInfo.Script.Uri, enumContext.Identifier.Range);
            _translateInfo.GetComponent <SymbolLinkComponent>().AddSymbolLink(this, DefinedAt, true);

            // Get the enum members.
            for (int i = 0; i < enumContext.Values.Count; i++)
            {
                if (enumContext.Values[i].Identifier)
                {
                    var expression = enumContext.Values[i].Value != null
                        ? new ExpressionOrWorkshopValue(parseInfo.GetExpression(Scope, enumContext.Values[i].Value))
                        : new ExpressionOrWorkshopValue(new V_Number(i));

                    var newMember = new DefinedEnumMember(parseInfo, this, enumContext.Values[i].Identifier.Text, new Location(parseInfo.Script.Uri, enumContext.Values[i].Identifier.Range), expression);
                    Scope.AddVariable(newMember, parseInfo.Script.Diagnostics, newMember.DefinedAt.range);
                }
            }
        }
Пример #2
0
        public DefinedEnum(ParseInfo parseInfo, EnumContext enumContext) : base(enumContext.Identifier.GetText())
        {
            Kind = TypeKind.Enum;

            _translateInfo = parseInfo.TranslateInfo;
            Scope          = new Scope("enum " + Name);

            if (enumContext.Identifier)
            {
                parseInfo.TranslateInfo.CheckConflict(parseInfo, new(Name), enumContext.Identifier.Range);
                // Set location and symbol link.
                DefinedAt = new Location(parseInfo.Script.Uri, enumContext.Identifier.Range);
                parseInfo.Script.Elements.AddDeclarationCall(this, new(enumContext.Identifier.Range, true));
            }

            // Get the enum members.
            for (int i = 0; i < enumContext.Values.Count; i++)
            {
                if (enumContext.Values[i].Identifier)
                {
                    var expression = enumContext.Values[i].Value != null
                        ? new ExpressionOrWorkshopValue(parseInfo.GetExpression(Scope, enumContext.Values[i].Value))
                        : new ExpressionOrWorkshopValue(Element.Num(i));

                    var newMember = new DefinedEnumMember(parseInfo, this, enumContext.Values[i].Identifier.Text, new Location(parseInfo.Script.Uri, enumContext.Values[i].Identifier.Range), expression);
                    Scope.AddVariable(newMember, parseInfo.Script.Diagnostics, newMember.DefinedAt.range);
                }
            }
        }