-
Notifications
You must be signed in to change notification settings - Fork 1
/
Environment.py
31 lines (22 loc) · 897 Bytes
/
Environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from __future__ import annotations
from dataclasses import *
from RuntimeError import *
@dataclass
class Environment:
enclosing: Environment | None = None
values: dict[str, any] = field(default_factory=dict)
def define(self, name: str, value: any):
self.values[name] = value
def get(self, name: Token):
if name.lexeme in self.values:
return self.values[name.lexeme]
if self.enclosing is not None:
return self.enclosing.get(name)
raise LoxRuntimeError(name, f"Undefined variable '{name.lexeme}'.")
def assign(self, name: Token, value: any):
if name.lexeme in self.values:
self.values[name.lexeme] = value
return
if self.enclosing is not None:
return self.enclosing.assign(name, value)
raise LoxRuntimeError(name, f"Undefined variable '{name.lexeme}'.")