Skip to content

Commit

Permalink
typing cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Feb 23, 2024
1 parent 8c8b536 commit 3c75e94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
16 changes: 8 additions & 8 deletions FrontEnd/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pathlib
import dataclasses

from typing import List, Optional, Tuple
from typing import Optional

from FrontEnd import cwast
from FrontEnd import parse
Expand Down Expand Up @@ -102,7 +102,7 @@ def GetDoc(node):
return None


def RenderColonList(val: List, field: str, out, indent: int):
def RenderColonList(val: list, field: str, out, indent: int):

extra_indent = GetColonIndent(field)
line = out[-1]
Expand All @@ -122,7 +122,7 @@ def RenderColonList(val: List, field: str, out, indent: int):
out.append([" " * indent])


def ListIsCompact(val: List):
def ListIsCompact(val: list):
for v in val:
if GetDoc(v):
return False
Expand All @@ -134,7 +134,7 @@ def ListIsCompact(val: List):
return True


def RenderList(val: List, field: str, out, indent: int):
def RenderList(val: list, field: str, out, indent: int):
extra_indent = GetExprIndent(field)
line = out[-1]
if not val:
Expand Down Expand Up @@ -198,7 +198,7 @@ def RenderShortAttr(node):
def RenderRecursivelyToIR(node, out, indent: int):
if cwast.NF.TOP_LEVEL in node.FLAGS:
out.append([""])
line: List[str] = out[-1]
line: list[str] = out[-1]
abbrev = MaybeSimplifyLeafNode(node)
if abbrev:
line.append(abbrev)
Expand Down Expand Up @@ -360,7 +360,7 @@ def RenderRecursivelyHTML(node, tc, out, indent: int):
out.append(["<p></p>"])


def PrettyPrintHTML(mod: cwast.DefMod, tc): # -> List[Tuple[int, str]]:
def PrettyPrintHTML(mod: cwast.DefMod, tc): # -> list[Tuple[int, str]]:
out = [[
"""<html>
<style>
Expand Down Expand Up @@ -514,7 +514,7 @@ def IsTopLevelBeg(self):
class TS:

def __init__(self):
self._tokens: List[Token] = []
self._tokens: list[Token] = []
self._count = 0

def Pos(self) -> int:
Expand Down Expand Up @@ -598,7 +598,7 @@ def TokensParenList(ts: TS, lst, is_grouping: bool):
ts.EmitEnd(beg)


def TokensFunctional(ts: TS, name, nodes: List):
def TokensFunctional(ts: TS, name, nodes: list):
if isinstance(name, str):
ts.EmitUnOp(name)
else:
Expand Down
32 changes: 16 additions & 16 deletions FrontEnd/symbolize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging

from typing import List, Dict, Optional, Any
from typing import Optional, Any

from FrontEnd import pp
from FrontEnd import macros
Expand All @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)


SYMTAB_MAP = Dict[cwast.DefMod, "SymTab"]
SYMTAB_MAP = dict[cwast.DefMod, "SymTab"]


def AnnotateNodeSymbol(id_node, def_node):
Expand All @@ -39,17 +39,17 @@ class SymTab:
"""Symbol Table For Global Symbols"""

def __init__(self):
self._type_syms: Dict[str, cwast.DefType] = {}
self._type_syms: dict[str, cwast.DefType] = {}

self._rec_syms: Dict[str, cwast.DefRec] = {}
self._enum_syms: Dict[str, cwast.DefEnum] = {}
self._rec_syms: dict[str, cwast.DefRec] = {}
self._enum_syms: dict[str, cwast.DefEnum] = {}

self._fun_syms: Dict[str, cwast.DefFun] = {}
self._macro_syms: Dict[str, cwast.DefMacro] = {}
self._fun_syms: dict[str, cwast.DefFun] = {}
self._macro_syms: dict[str, cwast.DefMacro] = {}

self._var_syms: Dict[str, cwast.DefGlobal] = {}
self._mod_syms: Dict[str, cwast.DefMod] = {}
self._all_syms: Dict[str, Any] = {}
self._var_syms: dict[str, cwast.DefGlobal] = {}
self._mod_syms: dict[str, cwast.DefMod] = {}
self._all_syms: dict[str, Any] = {}

def AddSymWithDupCheck(self, name, node):
prev = self._all_syms.get(name)
Expand Down Expand Up @@ -249,7 +249,7 @@ def FindAndExpandMacrosRecursively(node, symtab_map: SYMTAB_MAP, nesting, ctx: m


def ResolveSymbolsInsideFunctionsRecursively(
node, symtab: SymTab, symtab_map: SYMTAB_MAP, scopes: List[Dict]):
node, symtab: SymTab, symtab_map: SYMTAB_MAP, scopes: list[dict]):

def record_local_sym(node):
name = node.name
Expand Down Expand Up @@ -387,7 +387,7 @@ def visitor(node, parents):
cwast.VisitAstRecursivelyWithAllParents(node, [], visitor)


def MacroExpansionDecorateASTWithSymbols(mod_topo_order: List[cwast.DefMod]):
def MacroExpansionDecorateASTWithSymbols(mod_topo_order: list[cwast.DefMod]):
"""
* extract global symbols
* resolve global symbols (= setting x_symbol)
Expand Down Expand Up @@ -424,7 +424,7 @@ def MacroExpansionDecorateASTWithSymbols(mod_topo_order: List[cwast.DefMod]):
for node in mod.body_mod:
if isinstance(node, (cwast.DefFun)):
logger.info("Resolving symbols inside fun: %s", node)
scopes: List[Dict] = []
scopes: list[dict] = []
ResolveSymbolsInsideFunctionsRecursively(
node, symtab, symtab_map, scopes)
assert not scopes
Expand All @@ -433,8 +433,8 @@ def MacroExpansionDecorateASTWithSymbols(mod_topo_order: List[cwast.DefMod]):
VerifyASTSymbolsRecursively(mod)


def IterateValRec(inits_field: List[cwast.RecField], def_rec: cwast.CanonType):
inits: Dict[cwast.RecField,
def IterateValRec(inits_field: list[cwast.RecField], def_rec: cwast.CanonType):
inits: dict[cwast.RecField,
cwast.FieldVal] = {i.x_field: i for i in inits_field}
used = 0
assert isinstance(def_rec.ast_node, cwast.DefRec)
Expand Down Expand Up @@ -491,7 +491,7 @@ def NormalizeModParam(node):
def AreEqualNormalizedModParam(a, b) -> bool:
if a is None or b is None:
return False
if type(a) != type(b):
if a is not type(b):
return False
if a is b:
return True
Expand Down
20 changes: 10 additions & 10 deletions FrontEnd/type_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import dataclasses

from typing import List, Dict, Optional
from typing import Optional

from FrontEnd import cwast

Expand Down Expand Up @@ -191,7 +191,7 @@ def is_mutable_array_or_slice(node) -> bool:
assert False


_BASE_TYPE_MAP: Dict[cwast.BASE_TYPE_KIND, List[str]] = {
_BASE_TYPE_MAP: dict[cwast.BASE_TYPE_KIND, list[str]] = {
cwast.BASE_TYPE_KIND.SINT: ["S64"],
cwast.BASE_TYPE_KIND.S8: ["S8"],
cwast.BASE_TYPE_KIND.S16: ["S16"],
Expand Down Expand Up @@ -273,11 +273,11 @@ class TypeCorpus:
def __init__(self, target_arch_config: TargetArchConfig):
self._target_arch_config: TargetArchConfig = target_arch_config
self._wrapped_curr = 1
self._base_type_map: Dict[cwast.BASE_TYPE_KIND, cwast.CanonType] = {}
self._base_type_map: dict[cwast.BASE_TYPE_KIND, cwast.CanonType] = {}
self._typeid_curr = 0
# maps to ast
self.topo_order: List[cwast.CanonType] = []
self.corpus: Dict[str, cwast.CanonType] = {} # name to canonical type
self.topo_order: list[cwast.CanonType] = []
self.corpus: dict[str, cwast.CanonType] = {} # name to canonical type

# VOID should get typeid zero
self._insert_base_type(cwast.BASE_TYPE_KIND.VOID)
Expand Down Expand Up @@ -323,10 +323,10 @@ def get_uint_reg_type(self):
def get_address_size(self):
return self._target_arch_config.data_addr_bitwidth // 8

def _get_register_type_for_sum_type(self, ct: cwast.CanonType) -> Optional[List[str]]:
def _get_register_type_for_sum_type(self, ct: cwast.CanonType) -> Optional[list[str]]:
assert ct.node is cwast.TypeUnion
num_void = 0
scalars: List[cwast.CanonType] = []
scalars: list[cwast.CanonType] = []
largest_by_kind: dict[str, int] = {}
largest = 0
for t in ct.union_member_types():
Expand All @@ -350,7 +350,7 @@ def _get_register_type_for_sum_type(self, ct: cwast.CanonType) -> Optional[List[
k = next(iter(largest_by_kind)) if len(largest_by_kind) == 1 else "U"
return [f"U{largest}", f"U{self._target_arch_config.typeid_bitwidth}"]

def _get_register_type(self, ct: cwast.CanonType) -> Optional[List[str]]:
def _get_register_type(self, ct: cwast.CanonType) -> Optional[list[str]]:
"""As long as a type can fit into no more than two regs it will have
register representation which is also how it will be past in function calls.
"""
Expand Down Expand Up @@ -504,7 +504,7 @@ def insert_enum_type(self, name: str, ast_node: cwast.DefEnum) -> cwast.CanonTyp
return self._insert(cwast.CanonType(cwast.DefEnum, name,
base_type_kind=ast_node.base_type_kind, ast_node=ast_node))

def insert_union_type(self, components: List[cwast.CanonType], untagged: bool) -> cwast.CanonType:
def insert_union_type(self, components: list[cwast.CanonType], untagged: bool) -> cwast.CanonType:
assert len(components) > 1
pp = set()
for c in components:
Expand All @@ -519,7 +519,7 @@ def insert_union_type(self, components: List[cwast.CanonType], untagged: bool) -
name = f"sum{extra}<{','.join(sorted_names)}>"
return self._insert(cwast.CanonType(cwast.TypeUnion, name, children=sorted_children, untagged=untagged))

def insert_fun_type(self, params: List[cwast.CanonType],
def insert_fun_type(self, params: list[cwast.CanonType],
result: cwast.CanonType, original_type=None) -> cwast.CanonType:
x = [p.name for p in params]
x.append(result.name)
Expand Down
8 changes: 4 additions & 4 deletions FrontEnd/typify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging

from typing import List, Dict, Tuple, Any
from typing import Tuple, Any


from FrontEnd import cwast
Expand Down Expand Up @@ -132,7 +132,7 @@ class _PolyMap:
"""Polymorphism map"""

def __init__(self, tc: type_corpus.TypeCorpus):
self._map: Dict[Tuple[cwast.DefMod, str, str], cwast.DefFun] = {}
self._map: dict[Tuple[cwast.DefMod, str, str], cwast.DefFun] = {}
self._type_corpus = tc

def Register(self, fun: cwast.DefFun):
Expand Down Expand Up @@ -359,7 +359,7 @@ def _TypifyNodeRecursively(node, tc: type_corpus.TypeCorpus,
elif isinstance(node, cwast.ValRec):
ct = _TypifyNodeRecursively(node.type, tc, target_type, ctx)
assert ct.is_rec()
all_fields: List[cwast.RecField] = [f for f in ct.ast_node.fields]
all_fields: list[cwast.RecField] = [f for f in ct.ast_node.fields]
for val in node.inits_field:
assert isinstance(val, cwast.FieldVal)
if val.init_field:
Expand Down Expand Up @@ -1154,7 +1154,7 @@ def visitor(node, _):
cwast.VisitAstRecursivelyPost(node, visitor)


def DecorateASTWithTypes(mod_topo_order: List[cwast.DefMod],
def DecorateASTWithTypes(mod_topo_order: list[cwast.DefMod],
tc: type_corpus.TypeCorpus):
"""This checks types and maps them to a canonical node
Expand Down

0 comments on commit 3c75e94

Please sign in to comment.