visitClassStmt method
void
visitClassStmt(
- Class stmt
)
override
Implementation
@override
void visitClassStmt(Stmt.Class stmt) {
Object? superclass;
if (stmt.superclass != null) {
superclass = evaluate(stmt.superclass!);
if (superclass is! LoxClass) {
throw RuntimeError(
stmt.superclass!.name, "Superclass must be a class.");
}
}
environment.define(stmt.name.lexeme, null);
if (stmt.superclass != null) {
environment = Environment(environment);
environment.define("super", superclass);
}
Map<String, LoxFunction> methods = {};
for (Stmt.Functional method in stmt.methods) {
LoxFunction function = LoxFunction(
method, environment, method.name.lexeme == stmt.name.lexeme); // init
for (var key in function.declaration.namedParams.keys) {
if (function.declaration.namedParams[key] != null) {
function.declaration.namedParams[key] =
evaluate(function.declaration.namedParams[key] as Expr.Expr);
}
}
methods[method.name.lexeme] = function;
}
LoxClass klass = LoxClass(stmt.name.lexeme,
superclass == null ? null : superclass as LoxClass, methods);
if (superclass != null) {
environment = environment.enclosing!;
}
environment.assign(stmt.name, klass);
return;
}