public DefinedVar(DeltinScriptParser.VardefineContext vardefine)
        {
            IsGlobal = vardefine.GLOBAL() != null;
            string name = vardefine.PART(0).GetText();

            if (IsVar(name))
            {
                throw new SyntaxErrorException($"The variable {name} was already defined.", vardefine.start);
            }

            Name = name;

            // Both can be null, or only one can have a value.
            string useVar    = vardefine.PART(1)?.GetText();
            var    useNumber = vardefine.number();

            // Auto assign
            if (useNumber == null && useVar == null)
            {
                Index = Var.Assign(IsGlobal);

                if (IsGlobal)
                {
                    Variable = Var.Global;
                }
                else
                {
                    Variable = Var.Player;
                }

                IsInArray = true;
            }
            else
            {
                if (useNumber != null)
                {
                    IsInArray = true;
                    string indexString = useNumber.GetText();
                    if (!int.TryParse(indexString, out int index))
                    {
                        throw new SyntaxErrorException("Expected number.", useNumber.start);
                    }
                    Index = index;
                }

                if (useVar != null)
                {
                    if (!Enum.TryParse(useVar, out Variable var))
                    {
                        throw new SyntaxErrorException("Expected variable.", vardefine.start);
                    }
                    Variable = var;
                }
            }

            VarCollection.Add(this);
        }
示例#2
0
        public override Node VisitVardefine(DeltinScriptParser.VardefineContext context)
        {
            string variableName = context.PART(0).GetText();
            bool   isGlobal     = context.GLOBAL() != null;

            Variable?useVar = null;

            if (Enum.TryParse <Variable>(context.PART().ElementAtOrDefault(1)?.GetText(), out Variable setUseVar))
            {
                useVar = setUseVar;
            }

            int?useIndex = null;

            if (int.TryParse(context.number()?.GetText(), out int setUseIndex))
            {
                useIndex = setUseIndex;
            }

            Node node = new DefinedNode(isGlobal, variableName, useVar, useIndex, Range.GetRange(context));

            CheckRange(node);
            return(node);
        }