"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hashmaps_1 = require("@nebtex/hashmaps");
class SymbolTable {
    constructor() {
        /**
         * 符号表的内部数据结构
         */
        this.scopeLevelMap = new hashmaps_1.HashMap();
        /**
         * 作用域的初始值
         */
        this.initLevel = "0";
        // public test(){
        //     let symbols = new SymbolTable();
        //     let currentLevel:string = "0";
        //     console.log(currentLevel);
        //     let table:HashMap<any,SymbolElement> = new HashMap<any,SymbolElement>();
        //     currentLevel = symbols.insertTable(currentLevel,table);
        //     console.log(currentLevel);
        //
        //     currentLevel = symbols.insertTable(currentLevel,table);
        //     console.log(currentLevel)
        //
        //     currentLevel = symbols.exitTable(currentLevel);
        //     console.log(currentLevel)
        //
        //
        // }
    }
    /**
     * 向符号表里添加一个域的描述,即进入一个作用域
     */
    insertTable(parentLevel, table) {
        let currentLevel = this.initLevel;
        if (this.initLevel == parentLevel) {
            let i = 0;
            while (true) {
                i++;
                let tempLevel = i.toString();
                if (!this.scopeLevelMap.has(tempLevel)) {
                    currentLevel = tempLevel;
                    break;
                }
            }
            this.scopeLevelMap.set(currentLevel, table);
        }
        else {
            let i = 0;
            while (true) {
                i++;
                let tempLevel = parentLevel + "::" + i;
                if (!this.scopeLevelMap.has(tempLevel)) {
                    currentLevel = tempLevel;
                    break;
                }
            }
            this.scopeLevelMap.set(currentLevel, table);
        }
        return currentLevel;
    }
    /**
     * 从当前作用域中退出
     * @param currentLevel
     */
    exitTable(currentLevel) {
        let regex = "::";
        let split = currentLevel.split(regex);
        if (split.length == 1) {
            return this.initLevel;
        }
        let end = split[split.length - 1];
        let endIndex = currentLevel.length - end.length - 2;
        let parentLevel = currentLevel.substring(0, endIndex);
        if (this.scopeLevelMap.has(parentLevel)) {
            return parentLevel;
        }
    }
    /**
     * 根据作用域找到该作用域下的所有变量表
     * @param level
     */
    getTable(level) {
        return this.scopeLevelMap.get(level);
    }
    getInitLevel() {
        return this.initLevel;
    }
    /**
     * 获取符号表内的作用作用域
     */
    getAllLevel() {
        return this.scopeLevelMap.keys();
    }
}
exports.SymbolTable = SymbolTable;
