示例#1
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;
        }
        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;
            }
        }
示例#3
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;
        }