コード例 #1
0
ファイル: CallRoutineNode.cs プロジェクト: hansel0691/Tiger
        public override void CheckSemantics(Scope scope, List<SemanticError> errors)
        {
            if (Arguments != null)
                Arguments.CheckSemantics(scope, errors);
            var functionInfo = scope.GetRoutine(FunctionId.Text);
            //check if the function exist
            if (functionInfo == null)
            {
                errors.Add(SemanticError.FunctionDoesNotExist(FunctionId.Text, this));
                return;
            }

            if (functionInfo.ParameterCount != 0 && Arguments == null)
            {
                errors.Add(SemanticError.WrongParameterNumber("Function", FunctionId.Text, functionInfo.ParameterCount, 0, this));
                return;
            }
            if (Arguments != null && Arguments.Count != functionInfo.ParameterCount)
            {
                errors.Add(SemanticError.WrongParameterNumber("Function", FunctionId.Text, functionInfo.ParameterCount, this.Arguments.Count, this));
                return;
            }

            //var newScope = new Scope(scope);
            for (int i = 0; Arguments != null && i < Arguments.Count; i++)
            {
                //check the type of the parameters passed
                var expressionType = Arguments[i].ExpressionType;

                if (expressionType.Type == TypesEnumeration.Nil)
                {
                    if (!scope.GetType(functionInfo.ParametersType[i].Type).Nilable)
                        errors.Add(SemanticError.InvalidNilAssignation(functionInfo.ParametersType[i].Type, this));
                }
                else if (expressionType.Type == TypesEnumeration.Void ||  scope.GetType(expressionType.Name).Name != functionInfo.ParametersType[i].Type)
                    errors.Add(SemanticError.WrongType(functionInfo.ParametersType[i].Type, expressionType.Name, this));
            }
            ExpressionType = functionInfo.ReturnType != "void" ? scope.GetType(functionInfo.ReturnType) : TypeInfo.GenerateVoidInfo();
            ILName = scope.GetILRoutineName(FunctionId.Text);
        }