public override bool Visit(AstIdArrayExpression node)
 {
     ErrorIfIsNull(node.Index);
     return true;
 }
Example #2
0
        // #TYPE_DEFINITION INT LEFT_BRACKET INTEGER_VALUE_LIST RIGHT_BRACKET
        private void ConstructIntArrayTypeDefinition()
        {
            List<AstIntegerValueExpression> dimensions = new List<AstIntegerValueExpression>();
            AstIntegerValueExpression intVal;
            do
            {
                intVal = nodes.Pop() as AstIntegerValueExpression;
                dimensions.Add(intVal);
            }
            while((nodes.Peek() as AstIntegerValueExpression) != null);

            AstIntegerListExpression expr = new AstIntegerListExpression(dimensions.ToArray());

            var typeDef = new AstIdArrayExpression(BuiltInTypes.INT, expr);
            PushNode(typeDef);
        }
 public override bool Visit(AstIdArrayExpression node)
 {
     return true;
 }
Example #4
0
        // #ASSIGN_STATEMENT ID #ARRAY ASSIGNMENT #EXPRESSION
        private void ConstructAssignArrayStatement()
        {
            var newValue = nodes.Pop() as AstExpression;

            List<AstArrayIndex> indices = new List<AstArrayIndex>();

            while (nodes.Peek() is AstArrayIndex)
            {
                indices.Insert(0, nodes.Pop() as AstArrayIndex);
            }
            AstExpressionList index = new AstExpressionList(indices.ToArray());
            var id = nodes.Pop() as AstIdExpression;
            var idArrExpression = new AstIdArrayExpression(id.Id, index);
            idArrExpression.SetTextPosition( tokenCurrPosition );
            var node = new AstAssignStatement(idArrExpression, newValue);
            PushNode(node);
        }
Example #5
0
        // #SIMPLE_TERM ID #ARRAY
        private void ConstructArrayUseSimpleTerm()
        {
            var indices = new List<AstArrayIndex>();
            var curr = nodes.Peek() as AstNode;
            while (curr is AstArrayIndex)
            {
                nodes.Pop();
                indices.Insert(0, curr as AstArrayIndex);
                curr = nodes.Peek() as AstNode;
            }

            var index = new AstExpressionList(indices.ToArray());

            var id = nodes.Pop() as AstIdExpression;

            var idArr = new AstIdArrayExpression(id.Id, index);
            idArr.SetTextPosition( tokenCurrPosition );
            var node = new AstSimpleTermExpr(idArr);
            PushNode(node);
        }
        public override bool Visit(AstIdArrayExpression node)
        {
            node.Index.Accept(this);
            StringBuilder arrIndex = new StringBuilder();
            foreach (string s in m_lastIndex)
                arrIndex.AppendFormat(", {0} ", s);

            var tableSymbol = table.Lookup(node.Id);
            codeStream.WriteLine(CreateUnnamedVariable() + " = getelementptr " + GetArrayId(tableSymbol.Size as int[]) +
                "* @" + node.Id +
                 " , i32 0 " + arrIndex);
            if (!arrAssign)
            {
                codeStream.WriteLine(CreateUnnamedVariable() + " = getelementptr " + GetArrayId(tableSymbol.Size as int[]) +
                    "* @" + node.Id +
                    " , i32 0 " + arrIndex);
                string strLoad = " = load i32* " + GetCurrUnnamedVariable();
                codeStream.WriteLine(CreateUnnamedVariable() + strLoad);
                SaveArg("i32 " + GetCurrUnnamedVariable());
            }
            return false;
        }
        public override bool Visit(AstIdArrayExpression node)
        {
            var intValSize = node.Index as AstIntegerListExpression;
            if (intValSize != null)
            {
                try
                {
                    var size = intValSize.GetSize().Aggregate((s, t) => s * t);
                    if (size > BuiltInTypes.MAX_ARRAY_SIZE)
                    {
                        DispatchError(intValSize.TextPosition, "Max arrays size is " + BuiltInTypes.MAX_ARRAY_SIZE);
                        return false;
                    }

                    if (size < 1)
                    {
                        DispatchError(intValSize.TextPosition, "Invalid array size");
                        return false;
                    }
                }
                catch (OverflowException)
                {
                    DispatchError(intValSize.TextPosition, "Max arrays size is " + BuiltInTypes.MAX_ARRAY_SIZE);
                    return false;
                }
            }

            var indexType = resolver.Resolve(node.Index);
            if (indexType != BuiltInTypes.INT)
            {
                DispatchError(node.Index.TextPosition, "Only integer values available for array index");
            }

            if(node.Index is AstExpressionList)
            {
                var expr = node.Index as AstExpressionList;
                foreach (var item in expr.Expr)
                    if (!CheckIsNotNegative(item))
                        return false;
            }
            else if(node.Index is AstIntegerListExpression)
            {
                var expr = node.Index as AstIntegerListExpression;
                foreach (var item in expr.Expr)
                    if (!CheckIsNotNegative(item))
                        return false;
            }

            var sym = table.Lookup(node.Id);
            if (sym != null)
            {
                sym.Used = true;

                if (!Symbol.IsArray(sym))
                {
                    DispatchError(node.TextPosition, "Is not array");
                    return false;
                }

                int[] size = table.Lookup(node.Id).Size as int[];
                AstListExpression indices = null;

                if (node.Index is AstIntegerListExpression)
                    indices = node.Index as AstIntegerListExpression;
                else if (node.Index is AstExpressionList)
                    indices = node.Index as AstExpressionList;

                if (indices.Length != size.Length)
                {
                    DispatchError(node.TextPosition, "Wrong number of indices");
                    return false;
                }
                for (int i = 0; i < size.Length; i++)
                    CheckIndexInRange(size[i] - 1, indices[i]);
            }
            else if (!isClassFieldDef)
            {
                DispatchError(node.TextPosition, "Unknown identifier " + node.Id);
                return true;
            }

            return true;
        }
 public abstract bool Visit(AstIdArrayExpression node);