Beispiel #1
0
 //TODO:!!!:These two belong in Transpiler or something!..
 public void InjectParameters(Scope scope)
 {
     foreach (var param in Parameters)
     {
         scope.AddVar(param);
     }
 }
Beispiel #2
0
        public override void CheckSemantics(Scope scope, List<SemanticError> errors)
        {
            this.LowerValue.CheckSemantics(scope, errors);
            this.HighestValue.CheckSemantics(scope, errors);

            if (scope.ContainsType(IteratorName.Text, true))
                errors.Add(SemanticError.DefinedVariable(IteratorName.Text, this));

            if (this.LowerValue.ExpressionType.Type != TypesEnumeration.Integer)
                errors.Add(SemanticError.InvalidForExpression("lower", this));
            if (this.HighestValue.ExpressionType.Type != TypesEnumeration.Integer)
                errors.Add(SemanticError.InvalidForExpression("upper", this));

            var newScope = new Scope(scope);
            newScope.AddVar(this.IteratorName.Text, new VariableInfo(this.IteratorName.Text, "int"){ReadOnly =  true});
            this.Loop.CheckSemantics(newScope, errors);
            //loop may not return a value
            if (Loop.ExpressionType.Type != TypesEnumeration.Void)
                errors.Add(SemanticError.DontReturnExpression("For body", this));

            IteratorName.ILName = newScope.GetILVarNames(IteratorName.Text);
        }
Beispiel #3
0
 public override void ResolveNames(Scope scope, ErrorHandler errorHandler)
 {
     scope.AddVar(Token, this, errorHandler);
     Type.ResolveNames(scope, errorHandler);
 }
 protected Scope CreateFunctionScope(Scope scope, List<SemanticError> errors)
 {
     var newScope = new Scope(scope);
     if (Arguments != null)
     {
         Arguments.CheckSemantics(scope, errors);
         for (int i = 0; Arguments != null && i < Arguments.Count; i++)
         {
             newScope.AddVar(Arguments[i].Field.Text, new VariableInfo(Arguments[i].Field.Text, Arguments[i].TypeName.Text));
             Arguments[i].ILName = newScope.GetILVarNames(Arguments[i].Field.Text);
         }
     }
     return newScope;
 }
Beispiel #5
0
        public override bool VisitVarDeclaration(VarDeclarationAST expr)
        {
            expr.CurrentScope = _scope;

            ScopeLocation idLocation = _scope.HasVar(expr.Id);

            if (idLocation == ScopeLocation.DeclaredLocal)
            {
                _errorListener.Add(new AnalysisError(string.Format(AnalysisError.LoadMessage("VarDecl"), expr.Id), expr.Line, expr.Columns));
                expr.ReturnType = TigerType.GetType <ErrorType>();
                return(false);
            }
            if (idLocation != ScopeLocation.NotDeclared)
            {
                _errorListener.Add(AnalysisError.VariableHidesAnotherOne(expr));
            }

            //TODO: Rename ExpressionValue to NamedExpression
            expr.ExpressionValue.Accept(this);


            //se asume q no habran problemas de compatibilidad
            expr.ReturnType = TigerType.GetType <NoType>();

            // si se expecifico de forma explicita el tipo de la variable...
            if (!string.IsNullOrEmpty(expr.TypeId))
            {
                TigerType tt;
                //existe el tipo
                if (_scope.HasType(expr.TypeId, out tt) != ScopeLocation.NotDeclared)
                {
                    //el tipo de la variable no machea con el de la expression
                    if (!expr.ExpressionValue.ReturnType.CanConvertTo(tt))
                    {
                        _errorListener.Add(
                            new AnalysisError(
                                string.Format(AnalysisError.LoadMessage("Match"), expr.TypeId, expr.ExpressionValue.ReturnType.TypeID),
                                expr.Line, expr.Columns));
                        expr.ReturnType = TigerType.GetType <ErrorType>();
                        _scope.AddVar(expr.Id, TigerType.GetType <ErrorType>().TypeID);
                        return(false);
                    }
                    expr.ReturnType = expr.ExpressionValue.ReturnType;
                    //si me especifica el tipo explicitamente .
                    _scope.AddVar(expr.Id, tt.TypeID);
                    return(true);
                }
                // no existe el tipo de la variable
                _errorListener.Add(AnalysisError.TypeIsNotDefined(expr, expr.TypeId));
                expr.ReturnType = TigerType.GetType <ErrorType>();
                _scope.AddVar(expr.Id, TigerType.GetType <ErrorType>().TypeID);
                return(false);
            }
            if (!expr.ExpressionValue.ReturnType.IsLegalType)
            {
                _errorListener.Add(AnalysisError.TypeCannotBeInferred(expr, expr.Id));
                expr.ReturnType = TigerType.GetType <ErrorType>();
                _scope.AddVar(expr.Id, TigerType.GetType <ErrorType>().TypeID);
                return(false);
            }
            _scope.AddVar(expr.Id, expr.ExpressionValue.ReturnType.TypeID);
            return(true);
        }
Beispiel #6
0
 /// <summary>
 /// Añade una variable al ambito global
 /// /// </summary>
 /// <param name="node">Nodo de tipo VARIABLE</param>
 internal void AddVar(ParseTreeNode node)
 {
     Global.AddVar(node);
 }