|
9 | 9 | # the GNU General License |
10 | 10 | # ---------------------------------------------------------------------- |
11 | 11 |
|
12 | | -from collections import OrderedDict |
13 | | - |
14 | | -from typing import Dict |
15 | | -from typing import List |
16 | | -from typing import Optional |
17 | | -from typing import Union |
18 | | - |
19 | | -from src.api import errmsg |
20 | | -import src.api.check as check |
21 | | - |
22 | | -from src.api.debug import __DEBUG__ |
23 | | -from src.api import global_ |
| 12 | +from typing import List, Dict, Optional, Union |
24 | 13 |
|
25 | 14 | from src import symbols |
| 15 | +from src.api import global_, check as check, errmsg |
| 16 | +from src.api.config import OPTIONS |
| 17 | +from src.api.constants import TYPE, DEPRECATED_SUFFIXES, SUFFIX_TYPE, CLASS, SCOPE |
| 18 | +from src.api.debug import __DEBUG__ |
| 19 | +from src.api.errmsg import ( |
| 20 | + error as syntax_error, |
| 21 | + warning_not_used, |
| 22 | + warning_implicit_type, |
| 23 | + syntax_error_not_array_nor_func, |
| 24 | + syntax_error_cannot_define_default_array_argument, |
| 25 | + syntax_error_func_type_mismatch, |
| 26 | +) |
| 27 | +from src.api.symboltable.scope import Scope |
| 28 | +from src.symbols.label import SymbolLABEL |
26 | 29 | from src.symbols.symbol_ import Symbol |
27 | 30 | from src.symbols.var import SymbolVAR |
28 | | -from src.symbols.label import SymbolLABEL |
29 | 31 | from src.symbols.vararray import SymbolVARARRAY |
30 | 32 |
|
31 | | -from .config import OPTIONS |
32 | | - |
33 | | -from .errmsg import error as syntax_error |
34 | | -from .errmsg import warning_implicit_type |
35 | | -from .errmsg import warning_not_used |
36 | | -from .errmsg import syntax_error_func_type_mismatch |
37 | | -from .errmsg import syntax_error_not_array_nor_func |
38 | | -from .errmsg import syntax_error_cannot_define_default_array_argument |
39 | | - |
40 | | -from .constants import DEPRECATED_SUFFIXES |
41 | | -from .constants import SUFFIX_TYPE |
42 | | -from .constants import SCOPE |
43 | | -from .constants import CLASS |
44 | | -from .constants import TYPE |
45 | | - |
46 | | - |
47 | | -class Scope: |
48 | | - """Implements an Scope in the SymbolTable |
49 | | -
|
50 | | - An Scope is just a dictionary. |
51 | | -
|
52 | | - To get a symbol, just access it by it's name. So scope['a'] will |
53 | | - return the 'a' symbol (e.g. a declared variable 'a') or None |
54 | | - if nothing is declared in that scope (no KeyError exception is raised |
55 | | - if the identifier is not defined in such scope). |
56 | | -
|
57 | | - The caseins dict stores the symbol names in lowercase only if |
58 | | - the global OPTION ignore case is enabled (True). This is because |
59 | | - most BASIC dialects are case insensitive. 'caseins' will be used |
60 | | - as a fallback if the symbol name does not exists. |
61 | | -
|
62 | | - On init() the parent mangle can be stored. The mangle is a prefix |
63 | | - added to every symbol to avoid name collision. |
64 | | -
|
65 | | - E.g. for a global var o function, the mangle will be '_'. So |
66 | | - 'a' will be output in asm as '_a'. For nested scopes, the mangled |
67 | | - is composed as _function-name_varname. So a local variable in function |
68 | | - myFunct will be output as _myFunct_a. |
69 | | - """ |
70 | | - |
71 | | - def __init__(self, namespace: str = "", parent_scope: Optional["Scope"] = None): |
72 | | - self.symbols: Dict[str, SymbolVAR] = OrderedDict() |
73 | | - self.caseins: Dict[str, SymbolVAR] = OrderedDict() |
74 | | - self.namespace: str = namespace |
75 | | - self.owner: Optional[SymbolVAR] = None # Function, Sub, etc. owning this scope |
76 | | - self.parent_scope: Optional["Scope"] = parent_scope |
77 | | - self.parent_namespace: Optional[str] = parent_scope.namespace if parent_scope is not None else None |
78 | | - |
79 | | - def __getitem__(self, key: str) -> Optional[SymbolVAR]: |
80 | | - return self.symbols.get(key, self.caseins.get(key.lower(), None)) |
81 | | - |
82 | | - def __setitem__(self, key: str, value: SymbolVAR): |
83 | | - assert isinstance(value, Symbol) |
84 | | - self.symbols[key] = value |
85 | | - if value.caseins: # Declared with case insensitive option? |
86 | | - self.caseins[key.lower()] = value |
87 | | - |
88 | | - def __delitem__(self, key: str): |
89 | | - self.symbols.pop(key, None) |
90 | | - self.caseins.pop(key, None) |
91 | | - |
92 | | - def values(self, filter_by_opt=True): |
93 | | - if filter_by_opt and OPTIONS.optimization_level > 1: |
94 | | - return [y for x, y in self.symbols.items() if y.accessed] |
95 | | - return [y for x, y in self.symbols.items()] |
96 | | - |
97 | | - def keys(self, filter_by_opt=True): |
98 | | - if filter_by_opt and OPTIONS.optimization_level > 1: |
99 | | - return [x for x, y in self.symbols.items() if y.accessed] |
100 | | - return self.symbols.keys() |
101 | | - |
102 | | - def items(self, filter_by_opt=True): |
103 | | - if filter_by_opt and OPTIONS.optimization_level > 1: |
104 | | - return [(x, y) for x, y in self.symbols.items() if y.accessed] |
105 | | - return self.symbols.items() |
106 | | - |
107 | 33 |
|
108 | 34 | class SymbolTable: |
109 | 35 | """Implements a symbol table. |
|
0 commit comments