コード例 #1
0
        private bool ParseConstant(ParseTreeNode node, Block block, out Variable res)
        {
            res = new Assemble.Variable();
            res.AssemblePosition = new Assemble.AssemblePosition(this.ParsingFilePath, node);
            res.DefinedBlock     = block;
            if (node.ChildNodes.Count == 5)
            { //.constant + name + = + content + ;
                //Name
                res.Name = node.ChildNodes[1].ChildNodes[0].Token.Text;

                //Content
                ValueBase initialValue;
                if (!ParseImmediateValue(node.ChildNodes[3].ChildNodes[0], block, out initialValue))
                {
                    return(false);
                }
                res.InitialValues = new ValueBase[1] {
                    initialValue
                };

                //Mark attribute
                res.IsConstant = true;
            }
            else if (node.ChildNodes.Count == 10)
            { //.constant + name + [ + number + ] + = + { + contents + } + ;
                //Name
                res.Name = node.ChildNodes[1].ChildNodes[0].Token.Text;

                //Content
                List <ValueBase> initialValues;
                if (!ParseConstantInitialValues(node.ChildNodes[7], block, out initialValues))
                {
                    return(false);
                }

                //Fill by zero
                int length = (int)node.ChildNodes[3].Token.Value;
                if (length <= 0)
                {
                    ReportError("Constant", "Constant array length is invalid.", node.ChildNodes[3]);
                    return(false);
                }
                if (length < initialValues.Count)
                {
                    ReportError("Constant", "Constant array length is too small for its initial values.", node.ChildNodes[3]);
                    return(false);
                }
                while (initialValues.Count < length)
                {
                    initialValues.Add(ValueBase.Zero);
                }
                res.InitialValues = initialValues.ToArray();

                //Mark attribute
                res.IsConstant = true;
            }
            return(true);
        }
コード例 #2
0
        public Variable Clone(Block parentBlock)
        {
            Variable res = new Assemble.Variable()
            {
                AssemblePosition     = this.AssemblePosition,
                Name                 = this.Name,
                PositionHint         = this.PositionHint,
                DefinedBlock         = parentBlock,
                IsConstant           = this.IsConstant,
                IsNeedInitialization = this.IsNeedInitialization,
            };

            //InitialValues
            res.InitialValues = new ValueBase[this.InitialValues.Length];
            for (int i = 0; i < res.InitialValues.Length; i++)
            {
                res.InitialValues[i] = this.InitialValues[i].Clone(parentBlock);
            }

            return(res);
        }
コード例 #3
0
        private bool ParseVariable(ParseTreeNode node, Block block, out Variable res)
        {
            res = new Assemble.Variable();
            res.AssemblePosition = new Assemble.AssemblePosition(this.ParsingFilePath, node);
            res.DefinedBlock     = block;
            if (node.ChildNodes.Count == 3 || node.ChildNodes.Count == 4)
            { //.variable + name + [variabledefPositionHint] + ;
                //Name
                res.Name = node.ChildNodes[1].ChildNodes[0].Token.Text;

                //No content
                res.InitialValues = new ValueBase[1] {
                    ValueBase.Zero
                };

                //PositionHint
                if (node.ChildNodes.Count == 4)
                {
                    res.PositionHint = (int)(node.ChildNodes[2].ChildNodes[2].Token.Value);
                }

                //Mark attribute
                res.IsConstant           = false;
                res.IsNeedInitialization = false;
            }
            else if (node.ChildNodes.Count == 5 || node.ChildNodes.Count == 6)
            { //.variable + name + [variabledefPositionHint] + = + content + ;
                //Name
                res.Name = node.ChildNodes[1].ChildNodes[0].Token.Text;

                //PositionHint
                if (node.ChildNodes.Count == 6)
                {
                    res.PositionHint = (int)(node.ChildNodes[2].ChildNodes[2].Token.Value);
                }

                //Content
                ParseTreeNode contentNode = node.ChildNodes[node.ChildNodes.Count - 2];
                ValueBase     initialValue;
                if (!ParseImmediateValue(contentNode.ChildNodes[0], block, out initialValue))
                {
                    return(false);
                }
                res.InitialValues = new ValueBase[1] {
                    initialValue
                };

                //Mark attribute
                res.IsConstant           = false;
                res.IsNeedInitialization = true;
            }
            else if (node.ChildNodes.Count == 10 || node.ChildNodes.Count == 11)
            { //.variable + name + [ + number + ] + [variabledefPositionHint] + = + { + contents + } + ;
                //Name
                res.Name = node.ChildNodes[1].ChildNodes[0].Token.Text;

                //PositionHint
                if (node.ChildNodes.Count == 11)
                {
                    res.PositionHint = (int)(node.ChildNodes[5].ChildNodes[2].Token.Value);
                }

                //Content
                ParseTreeNode    contentNode = node.ChildNodes[node.ChildNodes.Count - 3];
                List <ValueBase> initialValues;
                if (!ParseVariableInitialValues(contentNode, block, out initialValues))
                {
                    return(false);
                }

                //Fill by zero
                int length = (int)node.ChildNodes[3].Token.Value;
                if (length <= 0)
                {
                    ReportError("Variable", "Variable array length is invalid.", node.ChildNodes[3]);
                    return(false);
                }
                if (length < initialValues.Count)
                {
                    ReportError("Variable", "Variable array length is too small for its initial values.", node.ChildNodes[3]);
                    return(false);
                }
                while (initialValues.Count < length)
                {
                    initialValues.Add(ValueBase.Zero);
                }
                res.InitialValues = initialValues.ToArray();

                //Mark attribute
                res.IsConstant           = false;
                res.IsNeedInitialization = true; //配列に対するインデックスアクセス方法を提供しないので,要素が最適化されてもかまわない一時変数のために配列を使うことはないだろう
            }

            return(true);
        }