示例#1
0
        private bool DetermineContinuousness()
        {
            HashSet <int> integersUsed = new HashSet <int>();
            bool          first        = true;
            int           min          = 0;
            int           max          = 0;

            foreach (Chunk chunk in this.chunks)
            {
                if (chunk.Cases.Length != 1)
                {
                    throw new Exception("This should have been filtered out by the largestSpane check earlier.");
                }
                IntegerConstant c = chunk.Cases[0] as IntegerConstant;
                if (c == null)
                {
                    throw new Exception("How did this happen?");
                }
                int num = c.Value;
                integersUsed.Add(num);
                if (first)
                {
                    first = false;
                    min   = num;
                    max   = num;
                }
                else
                {
                    if (num < min)
                    {
                        min = num;
                    }
                    if (num > max)
                    {
                        max = num;
                    }
                }
            }

            int expectedTotal = max - min + 1;

            return(expectedTotal == integersUsed.Count);
        }
示例#2
0
        public ListSlice(Expression root, List <Expression> items, Token bracketToken, TopLevelConstruct owner)
            : base(root.FirstToken, owner)
        {
            this.Root         = root;
            this.BracketToken = bracketToken;
            if (items.Count == 2)
            {
                items.Add(new IntegerConstant(null, 1, owner));
            }

            if (items.Count != 3)
            {
                throw new Exception("Slices must have 2 or 3 components before passed into the constructor.");
            }

            if (items[2] == null)
            {
                items[2] = new IntegerConstant(null, 1, owner);
            }

            this.Items = items.ToArray();
        }
示例#3
0
		public ListSlice(Expression root, List<Expression> items, Token bracketToken, Executable owner)
			: base(root.FirstToken, owner)
		{
			this.Root = root;
			this.BracketToken = bracketToken;
			if (items.Count == 2)
			{
				items.Add(new IntegerConstant(null, 1, owner));
			}

			if (items.Count != 3)
			{
				throw new Exception("Slices must have 2 or 3 components before passed into the constructor.");
			}

			if (items[2] == null)
			{
				items[2] = new IntegerConstant(null, 1, owner);
			}

			this.Items = items.ToArray();
		}
示例#4
0
        internal override IList <Executable> Resolve(Parser parser)
        {
            if (!parser.IsTranslateMode)
            {
                int resolutionState = parser.ConstantAndEnumResolutionState[this];
                if (resolutionState == 2)
                {
                    return(new Executable[0]);
                }
                if (resolutionState == 1)
                {
                    throw new ParserException(this.FirstToken, "The resolution of this enum creates a cycle.");
                }
                parser.ConstantAndEnumResolutionState[this] = 1;
            }
            HashSet <int> consumed = new HashSet <int>();

            for (int i = 0; i < this.Items.Length; ++i)
            {
                string itemName = this.Items[i].Value;

                if (itemName == "length")
                {
                    throw new ParserException(this.Items[i], "The name 'length' is not allowed as an enum value as it is a reserved field. In general, enum members should be in ALL CAPS anyway.");
                }

                if (this.IntValue.ContainsKey(itemName))
                {
                    throw new ParserException(this.Items[i], "Duplicate item in same enum. ");
                }

                this.IntValue[itemName] = -1;

                if (this.Values[i] != null)
                {
                    IntegerConstant ic = this.Values[i].Resolve(parser) as IntegerConstant;
                    if (ic == null)
                    {
                        throw new ParserException(this.Values[i].FirstToken, "Enum values must be integers or left blank.");
                    }
                    this.Values[i] = ic;
                    if (consumed.Contains(ic.Value))
                    {
                        throw new ParserException(this.Values[i].FirstToken, "This integer value has already been used in the same enum.");
                    }

                    consumed.Add(ic.Value);
                    this.IntValue[itemName] = ic.Value;
                }
            }
            parser.ConstantAndEnumResolutionState[this] = 2;

            int next = 0;

            for (int i = 0; i < this.Items.Length; ++i)
            {
                if (this.Values[i] == null)
                {
                    while (consumed.Contains(next))
                    {
                        ++next;
                    }

                    this.IntValue[this.Items[i].Value] = next;
                    consumed.Add(next);
                }
            }

            parser.AddEnumDefinition(this);

            return(Executable.EMPTY_ARRAY);
        }
示例#5
0
		protected override void TranslateIntegerConstant(List<string> output, IntegerConstant intConstant)
		{
			int value = intConstant.Value;
			if (value >= 0)
			{
				output.Add("" + value);
			}
			else
			{
				output.Add("(" + value + ")");
			}
		}
示例#6
0
        public SwitchStatementUnsafeBlotchy(SwitchStatement switchStatement, bool useExplicitMax, int explicitMax, Executable owner)
            : base(switchStatement.FirstToken, owner)
        {
            this.OriginalSwitchStatement = switchStatement;
            this.Condition = switchStatement.Condition;

            this.UsesStrings    = switchStatement.UsesStrings;
            this.UseExplicitMax = useExplicitMax;
            this.ExplicitMax    = explicitMax;
            if (this.UsesStrings)
            {
                this.StringUnsafeToSafeMapping = new Dictionary <string, int>();
            }
            else
            {
                this.IntegerUnsafeToSafeMapping = new Dictionary <int, int>();
            }
            this.codeMapping  = new Dictionary <int, Executable[]>();
            this.tokenMapping = new Dictionary <int, Token>();

            this.max = switchStatement.Chunks.Length - 1;

            for (int i = 0; i < switchStatement.Chunks.Length; ++i)
            {
                SwitchStatement.Chunk chunk = switchStatement.Chunks[i];
                this.codeMapping[i]  = chunk.Code;
                this.tokenMapping[i] = chunk.CaseOrDefaultToken;

                foreach (Expression expression in chunk.Cases)
                {
                    if (expression == null)
                    {
                        this.DefaultCaseId = i;
                    }
                    else
                    {
                        IntegerConstant ic = expression as IntegerConstant;
                        StringConstant  sc = expression as StringConstant;
                        if (ic == null && sc == null)
                        {
                            throw new Exception("This shouldn't happen.");
                        }
                        if (ic != null)
                        {
                            int c = ic.Value;
                            this.IntegerUnsafeToSafeMapping[c] = i;
                        }
                        else
                        {
                            string s = sc.Value;
                            this.StringUnsafeToSafeMapping[s] = i;
                        }
                    }
                }
            }

            if (useExplicitMax)
            {
                if (this.UsesStrings)
                {
                    throw new Exception("Cannot use explicit max on string switch statements.");
                }
            }
        }
示例#7
0
		private void CompileIntegerConstant(Parser parser, ByteBuffer buffer, IntegerConstant intConst, bool outputUsed)
		{
			if (!outputUsed) throw new ParserException(intConst.FirstToken, "This expression does nothing.");
			buffer.Add(intConst.FirstToken, OpCode.LITERAL, parser.GetIntConstant(intConst.Value));
		}
示例#8
0
		protected abstract void TranslateIntegerConstant(List<string> output, IntegerConstant intConstant);