Esempio n. 1
0
        /* position <= 0 means we don't really care about it */
        public IdentifierInfo LookupIdentifierInfo(string identifier, int scopeId,
                                                   int position, bool isConstant = false)
        {
            IdentifierInfo identifierInfo;
            ScopeInfo      scopeInfo;

            var identLocation = new IdentifierLocation {
                identifierName = identifier, scopeId = scopeId
            };

            while (!IdentInfoDictionary.TryGetValue(identLocation, out identifierInfo) ||
                   (isConstant == true && identifierInfo.isConstant != isConstant) ||
                   (!identifierInfo.isFunctionType && position > 0 && identifierInfo.position > position))
            {
                var scopeExists = ScopeInfoDictionary.
                                  TryGetValue(identLocation.scopeId, out scopeInfo);

                if (!scopeExists || scopeInfo.parent == null)
                {
                    return(null);
                }

                identLocation.scopeId = scopeInfo.parent.id;
            }

            return(identifierInfo);
        }
Esempio n. 2
0
        public IdentifierInfo LookupFunctionInfo(string identifier, int scopeId, List <TypeAST> argsType)
        {
            IdentifierInfo identifierInfo;
            ScopeInfo      scopeInfo;

            var identLocation = new IdentifierLocation {
                identifierName = identifier, scopeId = scopeId
            };

            while (!IdentInfoDictionary.TryGetValue(identLocation, out identifierInfo) ||
                   identifierInfo.isFunctionType == false || InvalidArgs(identifierInfo, argsType))
            {
                var scopeExists = ScopeInfoDictionary.
                                  TryGetValue(identLocation.scopeId, out scopeInfo);

                if (!scopeExists || scopeInfo.parent == null)
                {
                    return(null);
                }

                identLocation.scopeId = scopeInfo.parent.id;
            }

            return(identifierInfo);
        }
Esempio n. 3
0
        public override void Visit(FunctionProtoAST functionProto)
        {
            var functionType = functionProto.GetFunctionType();

            var identInfo = new IdentifierInfo
            {
                name           = functionProto.Name,
                position       = _currentNodePosition,
                typeAST        = functionType,
                scopeId        = _currentScope.id,
                isFunctionType = true
            };

            var identLocation = new IdentifierLocation
            {
                identifierName = identInfo.name,
                scopeId        = identInfo.scopeId
            };

            if (_symTable.IdentInfoDictionary.ContainsKey(identLocation))
            {
                throw new Exception(string.Format("Identifier {0} already declared in file {1} line {2}",
                                                  identInfo.name, _symTable.FilePath, _currentNodePosition));
            }

            _symTable.IdentInfoDictionary.Add(identLocation, identInfo);

            if (_currentScope.id == 0)
            {
                GlobalIdentifiers.Add(functionProto.Name);
            }

            if (functionProto.Args == null)
            {
                return;
            }

            for (var i = 0; i < functionProto.Args.Count; i++)
            {
                var functionArgument = functionProto.Args[i];

                identInfo = new IdentifierInfo
                {
                    name           = functionArgument.Name,
                    typeAST        = functionArgument.Type,
                    position       = _currentNodePosition,
                    scopeId        = _scopeIdGen + 1,
                    isFunctionType = functionArgument.Type is FunctionTypeAST,
                    isFnParam      = true,
                    paramIndex     = i
                };

                AddIdentInfoToSymTable(identInfo);
            }
        }
Esempio n. 4
0
        private void AddIdentInfoToSymTable(IdentifierInfo identInfo)
        {
            var identLocation = new IdentifierLocation
            {
                identifierName = identInfo.name,
                scopeId        = identInfo.scopeId
            };

            if (_symTable.IdentInfoDictionary.ContainsKey(identLocation))
            {
                throw new Exception(string.Format("Identifier already declared {0} in file {1} line {2}",
                                                  identInfo.name, _symTable.FilePath, identInfo.scopeId));
            }

            _symTable.IdentInfoDictionary.Add(identLocation, identInfo);
        }