コード例 #1
0
        /// <summary>
        /// Inserts the specified token at the given depth.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <param name="depth">The depth.</param>
        public void Insert(Token token, int depth)
        {
            var index = (int)Hash(token.Lexeme) % PRIME_TABLE_SIZE;
            var newEntry = new LinkedListNode<Entry>
            {
                Value = new Entry
                {
                    Token = token,
                    Depth = depth
                }
            };

            // Collision
            if (this._Table[index] != null)
            {
                var possibleDuplicateEntry = this.Lookup(token.Lexeme);

                // possibleDuplicateEntry is the latest [Lexeme] inserted into the table
                if (possibleDuplicateEntry != null && possibleDuplicateEntry.Depth == depth)
                {
                    throw new DuplicateEntryException(token.Lexeme);
                }

                newEntry.Next = this._Table[index];
                this._Table[index].Previous = newEntry;
            }

            this._Table[index] = newEntry;
        }
コード例 #2
0
 /// <summary>
 /// Determines whether the specified item is boolean.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns></returns>
 private static bool IsBoolean(Token item)
 {
     return item.Type == TokenType.True || item.Type == TokenType.False;
 }
コード例 #3
0
 /// <summary>
 /// Determines whether [is number or variable] [the specified token].
 /// </summary>
 /// <param name="token">The token.</param>
 /// <returns></returns>
 private static bool IsNumberOrVariable(Token token)
 {
     return
         token.Type == TokenType.LiteralInteger ||
         token.Type == TokenType.LiteralReal ||
         token.Type == TokenType.Identifier;
 }
コード例 #4
0
 /// <summary>
 /// Pushes the specified token.
 /// </summary>
 /// <param name="token">The token.</param>
 public void Push(Token token)
 {
     _Tokens.Add(token);
 }
コード例 #5
0
        private void InsertClass(Token identifier)
        {
            CurrentClass = new ClassEntry
            {
                MethodNames = null,
                Fields = null,
                SizeOfLocal = 0
            };

            SymbolTable.Insert(identifier, Depth);

            var entry = SymbolTable.Lookup(identifier.Lexeme);
            entry.Type = EntryType.Class;
            entry.Content = CurrentClass;
        }
コード例 #6
0
        private void InsertMethod(TokenType returnType, Token identifier)
        {
            // Get type
            var _returnType = GetDataType(returnType);

            CurrentMethod = new MethodEntry
            {
                NumberOfParameters = 0,
                Parameters = null,
                ReturnType = _returnType,
                SizeOfLocal = 0
            };

            // Insert formal parameter
            SymbolTable.Insert(identifier, Depth);

            var entry = SymbolTable.Lookup(identifier.Lexeme);
            entry.Type = EntryType.Function;
            entry.Content = CurrentMethod;

            // Add method name to class
            var methodName = new Models.Table.LinkedListNode<string> { Value = identifier.Lexeme };
            methodName.Next = CurrentClass.MethodNames;
            CurrentClass.MethodNames = methodName;
        }
コード例 #7
0
        private void InsertVariable(TokenType dataType, Token identifier, bool isConstant = false, string value = "")
        {
            // Get type
            var _dataType = GetDataType(dataType);
            var _size = GetDataTypeSize(_dataType);
            var _offset = 0;
            var _scope = CurrentVariableScope;

            if (_scope == VariableScope.MethodParameter)
            {
                var paramType = new Models.Table.LinkedListNode<KeyValuePair<string, VariableType>>
                {
                    Value = new KeyValuePair<string, VariableType>(identifier.Lexeme, _dataType)
                };
                paramType.Next = CurrentMethod.Parameters;
                CurrentMethod.Parameters = paramType;

                CurrentMethod.NumberOfParameters++;
                CurrentMethod.ParameterSize += _size;
            }
            else if (_scope == VariableScope.ClassBody)
            {
                var field = new Models.Table.LinkedListNode<string> { Value = identifier.Lexeme };
                field.Next = CurrentClass.Fields;
                CurrentClass.Fields = field;

                CurrentClass.SizeOfLocal += _size;
                _offset = Offset;
                Offset += _size;
            }
            else if (_scope == VariableScope.MethodBody)
            {
                CurrentMethod.SizeOfLocal += _size;
                _offset = Offset;
                Offset += _size;
            }

            // Insert formal parameter
            SymbolTable.Insert(identifier, Depth);

            var entry = SymbolTable.Lookup(identifier.Lexeme);

            if (isConstant)
            {
                entry.Type = EntryType.Constant;
                entry.Content = new ConstantEntry
                {
                    DataType = _dataType,
                    Offset = _offset,
                    Value = value
                };
            }
            else
            {
                entry.Type = EntryType.Variable;
                entry.Content = new VariableEntry
                {
                    DataType = _dataType,
                    Offset = _offset,
                    Size = _size
                };
            }
        }