12.5.1 Invalid uses of this
This commit is contained in:
parent
21a58ffb20
commit
e62d1a209e
1 changed files with 17 additions and 0 deletions
|
@ -18,6 +18,13 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||||
NONE, FUNCTION, METHOD
|
NONE, FUNCTION, METHOD
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum ClassType {
|
||||||
|
NONE,
|
||||||
|
CLASS
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClassType currentClass = ClassType.NONE;
|
||||||
|
|
||||||
public void resolve(List<Stmt> statements) {
|
public void resolve(List<Stmt> statements) {
|
||||||
for (Stmt statement : statements) {
|
for (Stmt statement : statements) {
|
||||||
resolve(statement);
|
resolve(statement);
|
||||||
|
@ -34,6 +41,9 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitClassStmt(Stmt.Class stmt) {
|
public Void visitClassStmt(Stmt.Class stmt) {
|
||||||
|
ClassType enclosingClass = currentClass;
|
||||||
|
currentClass = ClassType.CLASS;
|
||||||
|
|
||||||
declare(stmt.name);
|
declare(stmt.name);
|
||||||
define(stmt.name);
|
define(stmt.name);
|
||||||
|
|
||||||
|
@ -47,6 +57,7 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||||
|
|
||||||
endScope();
|
endScope();
|
||||||
|
|
||||||
|
currentClass = enclosingClass;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,6 +172,12 @@ class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitThisExpr(Expr.This expr) {
|
public Void visitThisExpr(Expr.This expr) {
|
||||||
|
if (currentClass == ClassType.NONE) {
|
||||||
|
Lox.error(expr.keyword,
|
||||||
|
"Can't use 'this' outside of a class.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
resolveLocal(expr, expr.keyword);
|
resolveLocal(expr, expr.keyword);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue