Exemple #1
0
        public override void ValidateSemantic()
        {
            GeneralNode.ValidateSemantic();

            foreach (var variable in ListOfVariables)
            {
                var type = GeneralNode.DataType.ValidateTypeSemantic();

                var variables = new TypesTable.Variable();

                if (variable.Accessors != null)
                {
                    variables.Accessors.AddRange(variable.Accessors);
                }

                if (variable.PointerNodes != null)
                {
                    variables.Pointers.AddRange(variable.PointerNodes);
                }

                StackContext.Context.Stack.Peek().RegisterType(variable.Value, type, Position, variables);

                variable.ValidateSemantic();
            }
        }
        public override void ValidateSemantic()
        {
            //Name.ValidateSemantic();

            List <ElementStruct> elements = new List <ElementStruct>();

            foreach (var item in ListOfItems)
            {
                item.ValidateSemantic();

                elements.Add(new ElementStruct {
                    Element = item
                });

                StackContext.Context.Stack.Peek().Table.Remove(item.ItemDeclaration.NameOfVariable.Value);
                StackContext.Context.Stack.Peek().Variables.Remove(item.ItemDeclaration.NameOfVariable.Value);
            }

            var variable = new TypesTable.Variable
            {
                Accessors = Name.Accessors,
                Pointers  = new List <PointerNode>()
            };

            StackContext.Context.Stack.Peek().RegisterType(Name.Name, new StructType(elements), Position, variable);
            StackContext.Context.TableOfTypes.Add(Name.Name, new StructType(elements));
        }
        public override void ValidateSemantic()
        {
            BaseType type;

            if (NameOfVariable.StructValue != null)
            {
                type = StackContext.Context.Stack.Peek().GetVariable(NameOfVariable.StructValue, Position);
            }
            else
            {
                type = DataType.ValidateTypeSemantic();
            }

            var variable = new TypesTable.Variable();

            if (NameOfVariable.Accessors != null && NameOfVariable.Accessors.Count > 0)
            {
                variable.Accessors.AddRange(NameOfVariable.Accessors);
            }

            if (ListOfPointer != null && ListOfPointer.Count > 0)
            {
                variable.Pointers.AddRange(ListOfPointer);
            }

            StackContext.Context.Stack.Peek().RegisterType(NameOfVariable.Value, type, Position, variable);

            if (NameOfVariable.Assignation != null)
            {
                NameOfVariable.Assignation.LeftValue             = DataType;
                NameOfVariable.Assignation.LeftValue.StructValue = NameOfVariable.Value;

                NameOfVariable.Assignation.ValidateSemantic();
            }
        }
        public override void ValidateSemantic()
        {
            StackContext.Context.Stack.Push(new TypesTable());

            List <ParameterFunction> listParams = new List <ParameterFunction>();
            BaseType returnType = null;


            var variable = new TypesTable.Variable
            {
                Accessors = Identifier.NameOfVariable.Accessors,
                Pointers  = Identifier.ListOfPointer
            };

            StackContext.Context.Stack.Peek().RegisterType(Identifier.NameOfVariable.Value, new FunctionType(listParams, returnType), Identifier.Position, variable);
            foreach (var parameter in Parameters)
            {
                parameter.ValidateSemantic();

                listParams.Add(new ParameterFunction {
                    Parameter = parameter
                });
            }

            foreach (var sentence in Sentences)
            {
                if (sentence is ReturnStatementNode)
                {
                    var typeOfReturn = (sentence as ReturnStatementNode).ValidateSemanticAndGetType();

                    returnType = Identifier.DataType.ValidateTypeSemantic();

                    var canReturn = Validations.ValidateReturnTypesEquivalence(typeOfReturn, returnType);

                    if (!canReturn)
                    {
                        throw new SemanticException($"Return types don't match at function {Identifier.NameOfVariable.Value} at Row: {Position.Row} , Column {Position.Column}");
                    }
                }
                else
                {
                    sentence.ValidateSemantic();
                }
            }



            StackContext.Context.PastContexts.Add(CodeGuid, StackContext.Context.Stack.Pop());

            StackContext.Context.Stack.Peek().RegisterType(Identifier.NameOfVariable.Value, new FunctionType(listParams, returnType), Identifier.Position, variable);
            StackContext.Context.Stack.First().RegisterType(Identifier.NameOfVariable.Value + "ResponseForServer", new IntType(), Position, variable);
        }
Exemple #5
0
        public override void ValidateSemantic()
        {
            StackContext.Context.Stack.Push(new TypesTable());

            var type = DataType.ValidateTypeSemantic();

            var variable = new TypesTable.Variable
            {
                Accessors = Item.Accessors,
                Pointers  = Item.PointerNodes
            };

            StackContext.Context.Stack.Peek().RegisterType(Item.Value, type, Position, variable);
            StackContext.Context.PastContexts.Add(CodeGuid, StackContext.Context.Stack.Pop());
        }
Exemple #6
0
        public override void ValidateSemantic()
        {
            foreach (var expressionNode in RightValue)
            {
                var rTipo = expressionNode.ValidateSemantic();

                var variable = new TypesTable.Variable
                {
                    Accessors = LeftValue.Accessors,
                    Pointers  = LeftValue.PointerNodes
                };

                if (!StackContext.Context.Stack.Peek().VariableExist(LeftValue.Value))
                {
                    StackContext.Context.Stack.Peek().RegisterType(LeftValue.Value, rTipo, Position, variable);
                }
                else
                {
                    var lTipo = StackContext.Context.Stack.Peek().GetVariable(LeftValue.Value, Position);
                    if (lTipo.GetType() != rTipo.GetType())
                    {
                        throw new SemanticException($"You can't assign a {rTipo} to a {lTipo} at Row: {Position.Row} , Column {Position.Column}");
                    }
                }

                var accesorsOfArray =
                    StackContext.Context.Stack.Peek().GetVariableAccessorsAndPointers(ArrayIdentifier.Value);

                var dimension = 1;

                foreach (var accessor in accesorsOfArray.Accessors.OfType <ArrayAccessorNode>())
                {
                    var expressionUnaryNode = accessor.IndexExpression as ExpressionUnaryNode;
                    if (expressionUnaryNode != null)
                    {
                        dynamic val = expressionUnaryNode.Factor.Interpret();

                        dimension *= val.Value;
                    }
                }

                if (dimension != RightValue.Count)
                {
                    throw new SemanticException($"The index of the array is {dimension}, you're trying to assign {RightValue.Count} at " +
                                                $"Row: {Position.Row} , Column {Position.Column}");
                }
            }
        }
        public override void ValidateSemantic()
        {
            List <ElementEnum> items = new List <ElementEnum>();

            foreach (var item in EnumItems)
            {
                item.ValidateSemantic();

                items.Add(new ElementEnum {
                    Element = item as EnumItemNode
                });
            }

            var variable = new TypesTable.Variable
            {
                Accessors = Name.Accessors,
                Pointers  = Name.PointerNodes
            };

            StackContext.Context.Stack.Peek().RegisterType(Name.Value, new EnumType(items), Position, variable);
            StackContext.Context.TableOfTypes.Add(Name.Value, new EnumType(items));

            variable.Accessors = new List <AccessorNode>();
            variable.Pointers  = new List <PointerNode>();

            if (EnumDeclarations.Count > 0)
            {
                foreach (var declaration in EnumDeclarations)
                {
                    var type = StackContext.Context.GetGeneralType(Name.Value);

                    StackContext.Context.Stack.Peek().RegisterType(declaration, type, Position, variable);
                    //  StackContext.Context.Stack.Peek().Table.Add(declaration,type);
                }
            }

            foreach (var item in EnumItems)
            {
                var name = ((EnumItemNode)item).ItemName.Value;
                var type = StackContext.Context.GetGeneralType(Name.Value);

                StackContext.Context.Stack.Peek().RegisterType(name, type, Position, variable);
                StackContext.Context.Stack.Peek().SetVariableValue(name, new StringValue {
                    Value = name
                });
            }
        }
        public override void ValidateSemantic()
        {
            var type = TypeOfConst.ValidateTypeSemantic();

            if (Assignation != null)
            {
                Assignation.LeftValue = TypeOfConst;

                Assignation.ValidateSemantic();
            }

            var variable = new TypesTable.Variable
            {
                Accessors = ConstName.Accessors,
                Pointers  = PointersList
            };

            StackContext.Context.Stack.Peek().Table.Remove(ConstName.Value);

            StackContext.Context.Stack.Peek().RegisterType(ConstName.Value, new ConstType
            {
                Assignation = Assignation, Type = type
            }, Position, variable);
        }
Exemple #9
0
        public override void ValidateSemantic()
        {
            var type = StackContext.Context.Stack.Peek().GetVariable(LeftValue.Value, Position);

            if (type is EnumType)
            {
                return;
            }

            if (LeftValue.Assignation != null)
            {
                var leftIdentifierPointers =
                    StackContext.Context.Stack.Peek().GetVariableAccessorsAndPointers(LeftValue.Value);

                TypesTable.Variable rightIdentifierPointers = null;

                var unaryNode = RightValue as ExpressionUnaryNode;

                var hasDereference = unaryNode != null && (unaryNode.UnaryOperator != null && (unaryNode.UnaryOperator.GetType() == typeof(BitAndOperatorNode)));

                var identifierExpression = unaryNode?.Factor as IdentifierExpression;

                if (identifierExpression != null)
                {
                    rightIdentifierPointers = StackContext.Context.Stack.Peek().GetVariableAccessorsAndPointers(identifierExpression.Name);
                }

                if (leftIdentifierPointers.Pointers.Count > 0)
                {
                    if (rightIdentifierPointers != null && rightIdentifierPointers.Pointers.Count == leftIdentifierPointers.Pointers.Count)
                    {
                    }
                    else
                    {
                        int?arrayAccessors = null;
                        if (rightIdentifierPointers != null)
                        {
                            arrayAccessors = rightIdentifierPointers.Accessors.Count(a => a is ArrayAccessorNode);
                        }

                        if (!(leftIdentifierPointers.Pointers.Count > 0 && hasDereference) &&
                            !(leftIdentifierPointers.Pointers.Count > 0 && arrayAccessors > 0) && arrayAccessors != null)
                        {
                            //throw new SemanticException("You need a reference to assignt value to a pointer," +
                            //                            $" at Row: {LeftValue.Position.Row}, column : {LeftValue.Position.Column}");
                        }
                    }
                }
            }

            var rTipo = RightValue.ValidateSemantic();

            var variable = new TypesTable.Variable
            {
                Accessors = LeftValue.Accessors,
                Pointers  = LeftValue.PointerNodes
            };

            if (!StackContext.Context.Stack.Peek().VariableExist(LeftValue.Value))
            {
                StackContext.Context.Stack.Peek().RegisterType(LeftValue.Value, rTipo, Position, variable);
            }
            else
            {
                var lTipo = StackContext.Context.Stack.Peek().GetVariable(LeftValue.Value, Position);

                if (rTipo is FunctionType)
                {
                    var returnType = (rTipo as FunctionType).FunctValue;

                    if (!Validations.ValidateReturnTypesEquivalence(returnType, lTipo))
                    {
                        throw new SemanticException($"You can't assign a {returnType} to a {lTipo} at Row: {Position.Row}, column : {Position.Column}");
                    }
                }
                else
                {
                    if (!Validations.ValidateReturnTypesEquivalence(rTipo, lTipo))
                    {
                        if (lTipo is ConstType)
                        {
                            throw new SemanticException($"You can't assign a {rTipo} to a {lTipo} at Row: {LeftValue.Position.Row}, column : {LeftValue.Position.Column}");
                        }
                        var row    = Position.Row == 0 ? LeftValue.Position.Row:Position.Row;
                        var column = Position.Column == 0 ? LeftValue.Position.Column : Position.Column;

                        throw new SemanticException($"You can't assign a {rTipo} to a {lTipo} " +
                                                    $"at Row: {row} , column : {column}");
                    }
                }
            }
        }