Source: scope-class.js

/**
 * Universidad de La Laguna
 * Escuela Superior de Ingeniería y Tecnología
 * Grado en Ingeniería Informática
 * Procesadores de Lenguajes
 *
 * @author Juan Rodríguez Suárez
 * @since Mar 04 2024
 * @desc Contains a Scope class to analyze the scope of the program.
 */

const {
  buildIdentifier,
  buildVariableDeclaration,
  buildVariableDeclarator,
} = require('./ast-build');
const { difference } = require('./utils.js');

/**
 * @description Class to analyze the scope of the program
 */
class Scope {
  constructor(parent) {
    this.parent = parent;
    this.initialized = new Set();
    this.used = new Set();
    this.letDeclarations = [];
  }
  add(name) {
    this.initialized.add(name);
    this.letDeclarations.push(buildVariableDeclarator(buildIdentifier(name)));
  }
  setAsInitialized(name) {
    this.initialized.add(name);
  }
  setAsUsed(name) {
    this.used.add(name);
  }
  has(name) {
    return this.initialized.has(name);
  }
  buildDeclaration() {
    return buildVariableDeclaration(this.letDeclarations);
  }
  lookup(name) {
    if (this.has(name)) {
      return this;
    }
    if (this.parent) {
      return this.parent.lookup(name);
    }
    return null;
  }
  notDeclared() {
    let notDeclared = difference(this.used, this.initialized);
    for (let v of this.used) {
      let s = this.lookup(v);
      if (s) {
        notDeclared.delete(v);
      }
    }
    return notDeclared;
  }
  notDeclaredMessage() {
    let notDeclared = this.notDeclared();
    if (notDeclared.size > 0) {
      return Array.from(notDeclared).
        map(x => x.replace(/^[$]/, '')).
        map(x => `Variable \'${x}\' not declared`).join('\n');
    }
    return null;
  }
  
  get length() {
    return this.letDeclarations.length;
  }
}

module.exports = Scope;