-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol_table.py
98 lines (81 loc) · 3.24 KB
/
symbol_table.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Nougaro : a python-interpreted high-level programming language
# Copyright (C) 2021-2024 Jean Dubois (https://github.com/jd-develop) <jd-dev@laposte.net>
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# IMPORTS
# __future__ import (must be first)
from __future__ import annotations
# nougaro modules imports
from src.lexer.token_types import KEYWORDS
# built-in python imports
import pprint
import difflib
from typing import Self
# special typing import
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from src.runtime.values.basevalues.value import Value
# ##########
# SYMBOL TABLE
# ##########
class SymbolTable:
def __init__(self, parent: Self | None = None):
self.symbols = {}
self.parent = parent
def dict_(self):
return {'symbols': self.symbols,
'parent': self.parent}
def __repr__(self) -> str:
return pprint.pformat(self.dict_())
def get(self, name: str, get_in_parent: bool = True, get_in_grandparent: bool = True) -> Value | None:
value = self.symbols.get(name, None)
if get_in_parent and value is None and self.parent is not None:
return self.parent.get(name, get_in_grandparent)
return value
def getf(self, name: str) -> Value | None:
"""Like get, but with get_in_(grand)parent to False. For builtin functions and modules."""
return self.get(name, False, False)
def set(self, name: str, value: Value):
self.symbols[name] = value
def set_whole_table(self, new_table: dict[str, Value]):
self.symbols = new_table.copy()
def remove(self, name: str):
del self.symbols[name]
def exists(self, name: str, look_in_parent: bool = False) -> bool:
if not look_in_parent or self.parent is None:
return name in self.symbols
else:
return name in self.symbols or self.parent.exists(name, True)
def set_parent(self, parent: Self):
self.parent = parent
return self
def __eq__(self, other: object):
if not isinstance(other, SymbolTable):
return False
return self.symbols == other.symbols
def __ne__(self, other: object):
return not self == other
def best_match(self, name: str) -> str | None:
"""Return the name in the symbol table that is the closest to 'name'. Return None if there is no close match."""
if len(self.symbols) == 0 or name == "":
return None
min_best_match = 0.5 # the original value was 0.3. Tweaked in commit 568156d
best_match = min_best_match
best_match_name = ""
list_to_check = list(self.symbols.keys())
list_to_check.extend(KEYWORDS)
for key in list_to_check:
ratio = difflib.SequenceMatcher(None, name, key).ratio()
if ratio > best_match:
best_match = ratio
best_match_name = key
if best_match_name == "":
return None
return best_match_name
def copy(self):
new_symbol_table = SymbolTable(self.parent)
new_symbol_table.symbols = self.symbols.copy()
return new_symbol_table