visitClassStmt method

  1. @override
void visitClassStmt(
  1. Class stmt
)
override

Implementation

@override
void visitClassStmt(Stmt.Class stmt) {
  ClassType enclosingClassType = currentClassType;
  currentClassType = ClassType.CLASS;
  declare(stmt.name);
  define(stmt.name);

  if (stmt.superclass != null &&
      stmt.name.lexeme == stmt.superclass!.name.lexeme) {
    error1(stmt.superclass!.name, 'A class can not inherit from itself.');
  }

  if (stmt.superclass != null) {
    currentClassType = ClassType.SUBCLASS;
    resolveExpr(stmt.superclass as Expr.Expr);
  }
  if (stmt.superclass != null) {
    beginScope();
    scopes.peek()["super"] = true;
  }
  beginScope();
  scopes.peek()["this"] = true;
  for (Stmt.Functional method in stmt.methods) {
    FunctionType declaration = FunctionType.METHOD;
    if (method.name.lexeme == "init") {
      declaration = FunctionType.INITIALIZER;
    }
    resolveFunction(method, declaration);
  }
  endScope();
  if (stmt.superclass != null) endScope();
  currentClassType = enclosingClassType;
  return;
}