Exemplo n.º 1
0
 /** Performs semantic analysis on the function's signature and return type, but not the body. */
 public void analyzeSignature(Log log, SymbolTable table, Subroutine owner, boolean inLoop) {
   SymbolTable tableForParameters;
   body.createTable(table);
   tableForParameters = body.getTable();
   for (Variable parameter : parameters) {
     tableForParameters.insert(parameter, log);
     parameter.analyze(log, tableForParameters, owner, inLoop);
   }
 }
Exemplo n.º 2
0
 private Symbol tryDeclareSymbol(Token ident) {
   assert (ident.is(Token.Kind.IDENTIFIER));
   String name = ident.lexeme();
   try {
     symbolTable.insert(name);
     return new Symbol(name);
   } catch (RedeclarationError re) {
     String message = reportDeclareSymbolError(name, ident.lineNumber(), ident.charPosition());
     return new ErrorSymbol(message);
   }
 }
Exemplo n.º 3
0
  @Override
  public void analyze(Log log, SymbolTable outer, Subroutine owner, boolean inLoop) {

    List<Subroutine> subroutines = getSubroutines();

    // Create the table if it hasn't already been created.  For blocks that are bodies of
    // functions or loops, the analyze() method of the function or loop will have created this
    // table already, since it is the table in which the parameters or loop indices belong.
    // For blocks that are entire scripts, the table will have already been created, too.
    // All other blocks will need their tables created here.
    if (table == null) {
      table = new SymbolTable(outer);
    }

    // Insert the routines into the table, but analyze ONLY the parameters and return types.
    // We can't analyze the function bodies until all the functions have been put into the
    // table (with analyzed parameters) because within any function body there can be a call
    // to any other function, and we have to be able to analyze the call.  Notice also that the
    // functions are going in before any variables are being looked at since variables can call
    // any function in their initializing expressions.
    for (Subroutine subroutine : subroutines) {
      subroutine.analyzeSignature(log, table, owner, inLoop);
      table.insert(subroutine, log);
    }

    // Now just go through all the items in order and analyze everything, making sure to
    // insert variables because they have not yet been inserted.
    for (Statement s : statements) {
      if (s instanceof DecStatement) {
        DecStatement d = (DecStatement) s;
        for (String t : d.getNames()) {
          table.insert(new Variable(t), log);
        }
      }
      s.analyze(log, table, owner, inLoop);
    }
  }