Skip to content

Commit

Permalink
lecture: fix resolving in classes (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
cagix authored Nov 23, 2023
1 parent bc28e7c commit d8e746c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions lecture/frontend/semantics/symboltables/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,26 @@ class Clazz(Struct):
# do we know "name" here?
if symbols[name]: return symbols[name]
# NEW: if not here, check any parent class ...
if parentClazz != None: return parentClazz.resolve(name)
# ... or enclosing scope if base class
try: return enclosingScope.resolve(name)
except: return None # not found
if parentClazz and parentClazz.resolve(name): return parentClazz.resolve(name)
else:
# ... or enclosing scope if base class
if enclosingScope: return enclosingScope.resolve(name)
else: return None # not found
def resolveMember(name):
if symbols[name]: return symbols[name]
# NEW: check parent class
try: return parentClazz.resolveMember(name)
except: return None
if parentClazz: return parentClazz.resolveMember(name)
else: return None
```

[Quelle: Eigene Implementierung nach einer Idee in [@Parr2010, p. 172]]{.origin}

::: notes
**Hinweis**: Die obige Implementierungsskizze soll vor allem das Prinzip demonstrieren - sie ist aus
Gründen der Lesbarkeit nicht besonders effizient: beispielsweise wird `parentClazz.resolve(name)`
mehrfach evaluiert ...

Beim Auflösen von Attributen oder Methoden muss zunächst in der Klasse selbst gesucht werden,
anschließend in der Elternklasse.

Expand Down
4 changes: 2 additions & 2 deletions lecture/frontend/semantics/symboltables/scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class Scope:
# do we know "name" here?
if symbols[name]: return symbols[name]
# if not here, check any enclosing scope
try: return enclosingScope.resolve(name)
except: return None # not found
if enclosingScope: return enclosingScope.resolve(name)
else: return None # not found

def bind(symbol):
symbols[symbol.name] = symbol
Expand Down

0 comments on commit d8e746c

Please sign in to comment.