Пример #1
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            LValueNode.CheckSemantics(scope, report);
            IndexExpressionNode.CheckSemantics(scope, report);

            if (!LValueNode.IsOK || !IndexExpressionNode.IsOK)
            {
                return;
            }

            //Check children types
            if (!(LValueNode.TigerType is ArrayType))
            {
                report.AddError(SemanticErrors.NonArrayType(LValueNode, LValueNode.TigerType));
                return;
            }
            if (!TigerType.Int.Assignable(IndexExpressionNode.TigerType))
            {
                report.AddError(SemanticErrors.ArrayIndexerInvalidType(IndexExpressionNode, IndexExpressionNode.TigerType));
                return;
            }

            TigerType = ((ArrayType)LValueNode.TigerType).ContentType;
        }
Пример #2
0
        public override void CheckBodySemantics(TigerScope scope, Report report)
        {
            //Check children
            IdNode.CheckSemantics(scope, report);
            FieldDeclarationNodes.ToList().ForEach(f => f.CheckSemantics(scope, report));

            var usedNames = new List <string>();

            foreach (var f in FieldDeclarationNodes)
            {
                if (usedNames.Contains(f.IdNode.Name))
                {
                    report.AddError(SemanticErrors.DuplicateFieldDeclaration(f, IdNode.Name, f.IdNode.Name));
                }
                usedNames.Add(f.IdNode.Name);
            }

            //If type was not added to scope (if CheckSemantics failed) return
            if (!IsOK || FieldDeclarationNodes.Any(f => !f.IsOK))
            {
                IsOK = false;
                return;
            }

            RecordTigerType = scope.FindType(IdNode.Name) as RecordType;

            IsOK = true;
        }
Пример #3
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);

            if (!IdNode.IsOK)
            {
                return;
            }

            //Check type name existence
            if (!scope.TypeNameAvailable(IdNode.Name))
            {
                report.AddError(SemanticErrors.TypeNameAlreadyInUse(IdNode, IdNode.Name));
            }
            else
            {
                RecordTigerType = new RecordType(IdNode.Name, ContainingScope.Name, null);
                scope.DefineIncompleteType(IdNode.Name, RecordTigerType);
            }

            IsOK = true;
        }
Пример #4
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            LeftOperandNode.CheckSemantics(scope, report);
            RightOperandNode.CheckSemantics(scope, report);
            if (!LeftOperandNode.IsOK || !RightOperandNode.IsOK)
            {
                return;
            }

            //Check children types
            if (TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int) && TigerType.AreCompatible(RightOperandNode.TigerType, TigerType.Int))
            {
                TigerType = TigerType.Int;
            }
            else if (!TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int))
            {
                report.AddError(SemanticErrors.LogicalOperandInvalidType(LeftOperandNode, LeftOperandNode.TigerType));
            }
            else
            {
                report.AddError(SemanticErrors.LogicalOperandInvalidType(RightOperandNode, RightOperandNode.TigerType));
            }
        }
Пример #5
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            Condition.CheckSemantics(scope, report);
            IfBlock.CheckSemantics(scope, report);

            if (!Condition.IsOK && !IfBlock.IsOK)
            {
                return;
            }

            TigerType = TigerType.Void;

            //Check condition type
            if (!TigerType.AreCompatible(TigerType.Int, Condition.TigerType))
            {
                report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType));
                TigerType = TigerType.Error;
            }
            if (!TigerType.AreCompatible(TigerType.Void, IfBlock.TigerType))
            {
                report.AddError(SemanticErrors.InvalidIfBodyType(IfBlock));
                TigerType = TigerType.Error;
            }
        }
Пример #6
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Checking children
            Condition.CheckSemantics(scope, report);
            IfBlock.CheckSemantics(scope, report);
            ElseBlock.CheckSemantics(scope, report);

            if (!Condition.IsOK || !IfBlock.IsOK || !ElseBlock.IsOK)
            {
                return;
            }

            TigerType = IfBlock.TigerType;

            //Checking children types
            if (!TigerType.AreCompatible(Condition.TigerType, TigerType.Int))
            {
                report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType));
            }
            if (!TigerType.AreCompatible(IfBlock.TigerType, ElseBlock.TigerType))
            {
                report.AddError(SemanticErrors.IncompatibleIfElseReturnType(ElseBlock, IfBlock.TigerType, ElseBlock.TigerType));
            }
        }
Пример #7
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Create new scope
            var childScope = new TigerScope(scope, "Let");

            //Check children
            DeclarationListNode.CheckSemantics(childScope, report);
            InstructionSequenceNode.CheckSemantics(childScope, report);

            if (!DeclarationListNode.IsOK || !InstructionSequenceNode.IsOK)
            {
                return;
            }

            TigerType = InstructionSequenceNode.TigerType;

            //Check return type

            if (!childScope.ValidReturnType(TigerType))
            {
                report.AddError(SemanticErrors.InvalidReturnType(this, TigerType));
            }
        }
Пример #8
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            Condition.CheckSemantics(scope, report);
            InstructionNode.CheckSemantics(scope, report);

            if (!Condition.IsOK || !InstructionNode.IsOK)
            {
                return;
            }

            IsOK = true;

            //Check children types
            if (!TigerType.AreCompatible(Condition.TigerType, TigerType.Int))
            {
                report.AddError(SemanticErrors.InvalidConditionType(Condition, Condition.TigerType));
                IsOK = false;
            }
            if (!TigerType.AreCompatible(InstructionNode.TigerType, TigerType.Void))
            {
                report.AddError(SemanticErrors.InvalidWhileBodyType(InstructionNode));
                IsOK = false;
            }
        }
Пример #9
0
 public void SolveTypes()
 {
     for (int i = 0; i < TypeDeclarationNodes.Length; i++)
     {
         if (Sets[i] == null)
         {
             continue;
         }
         var parentSet = DisjointSet.SetOf(Sets[i]);
         if (parentSet.IsInvalid)
         {
             Scope.RemoveType(Sets[i].Name);
             Report.AddError(SemanticErrors.CircularTypeDefinition(TypeDeclarationNodes[i], TypeDeclarationNodes[i].IdNode.Name));
         }
         else
         {
             DisjointSet.FirstArrayDependency(Sets[i]);
             if (Sets[i].TigerType is ArrayType)
             {
                 ((ArrayType)Sets[i].TigerType).ContentType = Sets[i].Parent.TigerType;
             }
             if (Sets[i].TigerType is SimpleType)
             {
                 ((SimpleType)Sets[i].TigerType).ActualType = Sets[i].Parent.TigerType;
             }
             if (!(TypeDeclarationNodes[i] is RecordTypeDeclarationNode))
             {
                 Scope.CompleteType(Sets[i].Name, Sets[i].TigerType);
             }
         }
     }
 }
Пример #10
0
        public void CheckBodySemantics(TigerScope scope, Report report)
        {
            //If CheckSemantics failed (FunctionInfo was not created) return
            if (!IsOK)
            {
                return;
            }

            IsOK = false;

            //Create function scope
            FunctionBodyNode.CheckSemantics(FunctionScope, report);

            if (!FunctionBodyNode.IsOK)
            {
                return;
            }

            IsOK = true;

            if (ReturnTypeNode != null && !ReturnTypeNode.TigerType.Assignable(FunctionBodyNode.TigerType))
            {
                report.AddError(SemanticErrors.IncompatibleFunctionReturnTypeBody(FunctionBodyNode, ReturnTypeNode.TigerType, FunctionBodyNode.TigerType));
            }
            else if (ReturnTypeNode == null && !TigerType.AreOfSameType(TigerType.Void, FunctionBodyNode.TigerType))
            {
                report.AddError(SemanticErrors.IncompatibleFunctionReturnTypeBody(FunctionBodyNode, TigerType.Void, FunctionBodyNode.TigerType));
            }
        }
Пример #11
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            LValueNode.CheckSemantics(scope, report);
            AttributeNode.CheckSemantics(scope, report);

            if (!LValueNode.IsOK || !AttributeNode.IsOK)
            {
                return;
            }

            //Check children types
            if (!(LValueNode.TigerType is RecordType))
            {
                report.AddError(SemanticErrors.NonRecordType(LValueNode, LValueNode.TigerType));
            }
            else
            {
                var field = ((RecordType)LValueNode.TigerType).RecordFields.FirstOrDefault(f => f.Name == AttributeNode.Name);
                if (field == null)
                {
                    report.AddError(SemanticErrors.InvalidField(AttributeNode, AttributeNode.Name, LValueNode.TigerType.Name));
                }
                else
                {
                    TigerType = field.TigerType;
                }
            }
        }
Пример #12
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            LValueNode.CheckSemantics(scope, report);
            ExpressionNode.CheckSemantics(scope, report);

            if (!LValueNode.IsOK || !ExpressionNode.IsOK)
            {
                return;
            }

            //Check children types
            if (!LValueNode.TigerType.Assignable(ExpressionNode.TigerType))
            {
                report.AddError(SemanticErrors.InvalidAssignType(ExpressionNode, ExpressionNode.TigerType, LValueNode.TigerType));
            }

            var variableAccess = LValueNode as VariableAccessNode;

            if (variableAccess != null && !variableAccess.VariableInfo.Assignable)
            {
                report.AddError(SemanticErrors.NonAssignableVariable(LValueNode, variableAccess.VariableInfo.Name));
            }

            IsOK = true;
        }
Пример #13
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);
            TypeNode.CheckSemantics(scope, report);
            ExpressionNode.CheckSemantics(scope, report);

            if (!IdNode.IsOK || !TypeNode.IsOK || !ExpressionNode.IsOK)
            {
                return;
            }

            //Check use of variable name
            if (!scope.VariableNameAvailable(IdNode.Name))
            {
                report.AddError(SemanticErrors.VariableNameAlreadyInUse(IdNode, IdNode.Name));
                return;
            }

            //Check children types
            if (!TypeNode.TigerType.Assignable(ExpressionNode.TigerType))
            {
                report.AddError(SemanticErrors.InvalidAssignType(ExpressionNode, TypeNode.TigerType, ExpressionNode.TigerType));
            }

            //Add variable to scope
            VariableInfo = scope.DefineVariable(IdNode.Name, TypeNode.TigerType, ContainingScope);

            IsOK = true;
        }
Пример #14
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            int val;

            if (!int.TryParse(Text, out val))
            {
                report.AddError(SemanticErrors.InvalidIntegerConstant(this, Text));
            }
            else
            {
                TigerType = TigerType.Int;
                Value     = val;
            }
        }
Пример #15
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check type existence
            var type = scope.FindType(IdNode.Name);

            //Checking existence of array
            if (type != null)
            {
                TigerType = type is SimpleType ? ((SimpleType)type).ActualType : type;
            }
            else
            {
                report.AddError(SemanticErrors.NonExistentTypeReference(IdNode, IdNode.Name));
            }
        }
Пример #16
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);
            FromExpression.CheckSemantics(scope, report);
            ToExpression.CheckSemantics(scope, report);

            if (!IdNode.IsOK || !FromExpression.IsOK || !ToExpression.IsOK)
            {
                return;
            }

            //Check loop bounds type
            if (!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int) || !TigerType.AreCompatible(ToExpression.TigerType, TigerType.Int))
            {
                report.AddError(!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int)
                    ? SemanticErrors.InvalidForBoundType(FromExpression, FromExpression.TigerType)
                    : SemanticErrors.InvalidForBoundType(ToExpression, ToExpression.TigerType));
                return;
            }

            IsOK = true;

            //Define new scope
            TigerScope childScope = new TigerScope(scope, "For");

            VariableInfo = childScope.DefineVariable(IdNode.Name, TigerType.Int, ContainingScope, false);

            DoInstruction.CheckSemantics(childScope, report);

            if (!DoInstruction.IsOK)
            {
                return;
            }

            if (!TigerType.AreCompatible(DoInstruction.TigerType, TigerType.Void))
            {
                report.AddError(SemanticErrors.InvalidForBodyType(DoInstruction));
                IsOK = false;
            }
        }
Пример #17
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Checking children
            IdNode.CheckSemantics(scope, report);
            Arguments.ToList().ForEach(e => e.CheckSemantics(scope, report));

            if (!IdNode.IsOK || Arguments.Any(e => !e.IsOK))
            {
                return;
            }

            //Checking existence of function
            FunctionInfo = scope.FindFunction(IdNode.Name);

            if (FunctionInfo == null)
            {
                report.AddError(SemanticErrors.NonExistentFunctionReference(IdNode, IdNode.Name));
            }
            else
            {
                TigerType = FunctionInfo.ReturnType;

                //Checking parameters
                if (Arguments.Length != FunctionInfo.ParameterNumber)
                {
                    report.AddError(SemanticErrors.WrongNumberOfParameters(IdNode, FunctionInfo.Name,
                                                                           FunctionInfo.ParameterNumber, Arguments.Length));
                }
                else
                {
                    for (int i = 0; i < Arguments.Length; i++)
                    {
                        if (!FunctionInfo.Parameters[i].Assignable(Arguments[i].TigerType))
                        {
                            report.AddError(SemanticErrors.InvalidArgumentType(Arguments[i], Arguments[i].TigerType, FunctionInfo.Parameters[i]));
                        }
                    }
                }
            }
        }
Пример #18
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            TypeNode.CheckSemantics(scope, report);
            RecordFieldInitNodes.ToList().ForEach(f => f.CheckSemantics(scope, report));

            if (!TypeNode.IsOK || RecordFieldInitNodes.Any(f => !f.IsOK))
            {
                return;
            }

            //Check children types
            if (!(TypeNode.TigerType is RecordType))
            {
                report.AddError(SemanticErrors.NonRecordType(TypeNode, TypeNode.TigerType));
            }
            else
            {
                TigerType = TypeNode.TigerType;

                var fields = ((RecordType)TypeNode.TigerType).RecordFields;
                //Check fields length
                if (fields.Length != RecordFieldInitNodes.Length)
                {
                    report.AddError(SemanticErrors.WrongNumberOfFields(TypeNode, TypeNode.TigerType.Name, fields.Length, RecordFieldInitNodes.Length));
                }
                //Check field names and types
                for (int i = 0; i < Math.Min(fields.Length, RecordFieldInitNodes.Length); i++)
                {
                    if (fields[i].Name != RecordFieldInitNodes[i].IdNode.Name)
                    {
                        report.AddError(SemanticErrors.WrongFieldPosition(RecordFieldInitNodes[i].IdNode, fields[i].Name, RecordFieldInitNodes[i].IdNode.Name));
                    }
                    if (!fields[i].TigerType.Assignable(RecordFieldInitNodes[i].TigerType))
                    {
                        report.AddError(SemanticErrors.InvalidFieldType(RecordFieldInitNodes[i].IdNode, fields[i].TigerType, RecordFieldInitNodes[i].TigerType));
                    }
                }
            }
        }
Пример #19
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            IsOK = true;

            //Check if break is in correct position
            var nodes = GetNodesToRoot().TakeWhile(x => !(x is MethodDeclarationNode)).ToList();

            Owner = (nodes.FirstOrDefault(x => x is IBreakableNode)) as IBreakableNode;

            if (Owner == null)
            {
                report.AddError(SemanticErrors.BreakInIncorrectPostion(this));
                return;
            }

            //Set containing InstructionSequenceNodes' types to void
            nodes.TakeWhile(x => !(x is IBreakableNode)).Where(n => n is InstructionSequenceNode).ToList()
            .ForEach(n => ((InstructionSequenceNode)n).TigerType = TigerType.Void);
        }
Пример #20
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);

            FunctionScope = new TigerScope(scope, IdNode.Name);

            ParameterDeclarationNodes.ToList().ForEach(p => p.CheckSemantics(FunctionScope, report));

            if (!IdNode.IsOK || ParameterDeclarationNodes.Any(p => !p.IsOK))
            {
                return;
            }

            //Check existence of function name
            if (!scope.FunctionNameAvailable(IdNode.Name))
            {
                report.AddError(SemanticErrors.FunctionNameAlreadyInUse(IdNode, IdNode.Name));
                return;
            }

            if (ReturnTypeNode != null)
            {
                ReturnTypeNode.CheckSemantics(scope, report);
                if (!ReturnTypeNode.IsOK)
                {
                    return;
                }
                FunctionInfo = scope.DefineFunction(IdNode.Name, ReturnTypeNode.TigerType,
                                                    ParameterDeclarationNodes.Select(p => p.TypeNode.TigerType).ToArray(), FunctionScope);
            }
            else
            {
                FunctionInfo = scope.DefineFunction(IdNode.Name, TigerType.Void,
                                                    ParameterDeclarationNodes.Select(p => p.TypeNode.TigerType).ToArray(), FunctionScope);
            }
            IsOK = true;
        }
Пример #21
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            LeftOperandNode.CheckSemantics(scope, report);
            RightOperandNode.CheckSemantics(scope, report);
            if (!LeftOperandNode.IsOK || !RightOperandNode.IsOK)
            {
                return;
            }

            TigerType = TigerType.Int;

            //Check children types
            if (!TigerType.AreCompatible(LeftOperandNode.TigerType, RightOperandNode.TigerType) ||
                (TigerType.AreOfSameType(LeftOperandNode.TigerType, TigerType.Nil) && TigerType.AreOfSameType(RightOperandNode.TigerType, TigerType.Nil)))
            {
                report.AddError(SemanticErrors.InvalidIdentityComparison(this, LeftOperandNode.TigerType,
                                                                         RightOperandNode.TigerType));
            }
        }
Пример #22
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check child
            ExpressionNode.CheckSemantics(scope, report);

            if (!ExpressionNode.IsOK)
            {
                return;
            }

            //Check child type
            if (TigerType.AreCompatible(TigerType.Int, ExpressionNode.TigerType))
            {
                TigerType = TigerType.Int;
            }
            else
            {
                report.AddError(SemanticErrors.ArithmeticOperandInvalidType(ExpressionNode, ExpressionNode.TigerType));
            }
        }
Пример #23
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Checking Children
            TypeNode.CheckSemantics(scope, report);
            LengthExpressionNode.CheckSemantics(scope, report);
            InitialValueExpressionNode.CheckSemantics(scope, report);

            if (!TypeNode.IsOK || !LengthExpressionNode.IsOK || !InitialValueExpressionNode.IsOK)
            {
                return;
            }

            var type = TypeNode.TigerType as ArrayType;

            if (type == null)
            {
                report.AddError(SemanticErrors.NonArrayType(TypeNode, TypeNode.TigerType));
                return;
            }

            TigerType = TypeNode.TigerType;

            //Checking LengthExpression type
            if (!TigerType.AreCompatible(LengthExpressionNode.TigerType, TigerType.Int))
            {
                report.AddError(SemanticErrors.NonIntegerArrayLength(LengthExpressionNode,
                                                                     LengthExpressionNode.TigerType));
            }

            //Checking InitialValueExpression
            if (!type.ContentType.Assignable(InitialValueExpressionNode.TigerType))
            {
                report.AddError(SemanticErrors.ArrayInitialValueInvalidType(InitialValueExpressionNode,
                                                                            InitialValueExpressionNode.TigerType, TypeNode.TigerType));
            }
        }
Пример #24
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);

            if (!IdNode.IsOK)
            {
                return;
            }

            //Check variable existence
            VariableInfo = scope.FindVariable(IdNode.Name);

            if (VariableInfo == null)
            {
                report.AddError(SemanticErrors.NonExistentVariableReference(IdNode, IdNode.Name));
            }
            else
            {
                TigerType = VariableInfo.TigerType;
            }
        }
Пример #25
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            LeftOperandNode.CheckSemantics(scope, report);
            RightOperandNode.CheckSemantics(scope, report);
            if (!LeftOperandNode.IsOK || !RightOperandNode.IsOK)
            {
                return;
            }

            //Check children types
            if ((TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.Int) && TigerType.AreCompatible(RightOperandNode.TigerType, TigerType.Int)) ||
                (TigerType.AreCompatible(LeftOperandNode.TigerType, TigerType.String) && TigerType.AreCompatible(RightOperandNode.TigerType, TigerType.String)))
            {
                TigerType = TigerType.Int;
            }
            else
            {
                report.AddError(SemanticErrors.InvalidOrderComparison(this, LeftOperandNode.TigerType,
                                                                      RightOperandNode.TigerType));
            }
        }
Пример #26
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);
            TypeNode.CheckSemantics(scope, report);

            if (!IdNode.IsOK || !TypeNode.IsOK)
            {
                return;
            }

            //Check use of variable name
            if (!scope.VariableNameAvailable(IdNode.Name))
            {
                report.AddError(SemanticErrors.RepeatedParameterName(IdNode, IdNode.Name));
                return;
            }

            //Add variable to scope
            VariableInfo = scope.DefineVariable(IdNode.Name, TypeNode.TigerType, scope);
            TigerType    = TypeNode.TigerType;
        }