Esempio n. 1
0
        public TypeInferer(SymbolTableManager symTableManager)
        {
            _symTableManager         = symTableManager;
            _identifiersToBeInferred = symTableManager.IdentifiersToBeInferred;

            Func <IdentifierInfo, TypeAST> GetVariableTypeFunc = (IdentifierInfo identInfo) =>
            {
                var identToBeInfered = _identifiersToBeInferred.Find(x => x.identInfo == identInfo);

                if (identToBeInfered == null)
                {
                    throw new Exception("Undeclared identifier " + identInfo);
                }

                return(_typeVisitor.GetAstNodeType(identToBeInfered.file, identInfo.scopeId, identInfo.position, identToBeInfered.expr, identInfo.isConstant));
            };

            _typeVisitor = new ExprTypeVisitor(symTableManager, GetVariableTypeFunc);
        }
Esempio n. 2
0
        public override void Visit(VariableDecAssignAST variableDecAssign)
        {
            if (variableDecAssign.Type == null)
            {
                return;
            }

            var identInfo = _symTableManager.LookupIdentifierInfo(_currentFileName, variableDecAssign.Name,
                                                                  _currentScopeId, _currentNodePosition);

            var exprType = _exprTypeVisitor.GetAstNodeType(_currentFileName, identInfo.scopeId, identInfo.position, variableDecAssign.ExpressionValue);

            if (!IsSameTypeOrNullPtr(variableDecAssign.Type, exprType))
            {
                throw new Exception(string.Format("Type mismatch : variable '{0}' have type '{1}' but assigned '{2}' type",
                                                  variableDecAssign.Name, variableDecAssign.Type, exprType));
            }
        }