Skip to content

Commit 2dd01a7

Browse files
committed
small bug fixes and commenting improvements
1 parent 88d3794 commit 2dd01a7

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

BE/Base/cfg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ def FunAddUnconditionalBranches(fun: ir.Fun):
270270
if n + 1 == len(fun.bbls) or fun.bbls[n + 1] != succ:
271271
bbl.inss.append(ir.Ins(o.BRA, [succ], False))
272272
continue
273-
# more complex case - we have two successors, we need to check if one of them is the fallthrough and if not we need to add a branch to the fallthrough
273+
# more complex case - we have two successors, we need to check if one of them
274+
# is the fallthrough and if not we need to add a branch to the fallthrough
274275
assert len(bbl.edge_out) == 2
275276
cond_bra = bbl.inss[-1]
276277
assert cond_bra.opcode.kind is o.OPC_KIND.COND_BRA, (

FE/lexer_tab.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
NODE = list[int]
2929
TRIE = list[list[int]]
3030

31-
31+
# The integer in a trie nodes are encoded as follows:
32+
# 0..len(trie) - 1: index of the next node in the
33+
# TK_KIND_OFFSET - TK_KIND_OFFSET_FOR_LOOK_AHEAD: the token kind is not longer part of the kw
34+
# TK_KIND_OFFSET_FOR_LOOK_AHEAD -: the token kind if the current node is a terminal
3235
TK_KIND_OFFSET = 1000
3336
TK_KIND_OFFSET_FOR_LOOK_AHEAD = 2000
3437

@@ -121,19 +124,21 @@ def rewrite(t: NODE):
121124
return out
122125

123126

124-
def FindInTrie(trie: TRIE, s: str) -> tuple[int, int]:
127+
def FindInTrie(trie: TRIE, s: str) -> tuple[int, TK_KIND]:
128+
""" Returns (size, TK_KIND) if s[:size] is a token in the trie and 0, 0 otherwise"""
125129
node = trie[0]
126130
for n, cc in enumerate(s):
127131
x = node[ord(cc)]
128132
if x == NODE_NULL:
129-
return 0, 0
133+
return 0, TK_KIND.INVALID
130134
if x >= len(trie):
131135
if x >= TK_KIND_OFFSET_FOR_LOOK_AHEAD:
136+
# we found a char that is not part of the kw
132137
return n, TK_KIND(x - TK_KIND_OFFSET_FOR_LOOK_AHEAD)
133138
else:
134139
return n + 1, TK_KIND(x - TK_KIND_OFFSET)
135140
node = trie[x]
136-
return 0, 0
141+
return 0, TK_KIND.INVALID
137142

138143

139144
def VerifyTrie(trie: TRIE, KWs):
@@ -259,7 +264,7 @@ def add_kw_simple(kw, tag):
259264
def add_kw(kw, tag, non_succ):
260265
# keyword is only valid if not followed by char in non_succ
261266
# E.g.
262-
# if is a keyword but ifoo is not
267+
# `if`` is a keyword but `ifoo`` is not
263268
# simarly
264269
# >> is an operator(-keyword) for most subsequent chars
265270
# except >>> and >>=
@@ -307,6 +312,8 @@ def add_kw(kw, tag, non_succ):
307312

308313
def MakeTrieNoisy():
309314
KWs = GetAllKWAndOps()
315+
for k, v in sorted(KWs):
316+
print(k, v)
310317
trie = MakeInitialTrie(KWs)
311318
#
312319
print("Stats")
@@ -650,6 +657,13 @@ def MakePerfectHashForBinOp():
650657

651658
if __name__ == "__main__":
652659
if len(sys.argv) == 1:
660+
inp = Lexer(LexerRaw("stdin", sys.stdin))
661+
while True:
662+
tk = inp.next()
663+
if tk.kind == TK_KIND.SPECIAL_EOF:
664+
break
665+
print(tk)
666+
elif sys.argv[1] == "trie_stats":
653667
MakeTrieNoisy()
654668
elif sys.argv[1] == "gen_cc":
655669
cgen.ReplaceContent(GenerateCodeCC, sys.stdin, sys.stdout)

FE/parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,8 @@ def main():
858858
logging.basicConfig(level=logging.WARNING)
859859
logger.setLevel(logging.WARNING)
860860
inp = lexer.Lexer(lexer.LexerRaw("stdin", sys.stdin))
861-
mod = _ParseDefMod(inp)
861+
mod = _ParseDefMod(inp, "dummy")
862862
RemoveRedundantParens(mod)
863-
pp_sexpr.PrettyPrint(mod)
863+
pp_sexpr.PrettyPrint(mod, sys.stdout)
864864

865865
main()

FE/typify.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ def _TypifyExprOrType(node, tc: type_corpus.TypeCorpus,
649649
elif ct.is_enum():
650650
return _NodeSetType(node, ct.underlying_type())
651651
else:
652+
cwast.CompilerError(node.x_srcloc, f"expected enum or wrapped type in unwrap expression but got {ct}")
652653
assert False
653654
elif isinstance(node, cwast.ExprIs):
654655
_TypifyExprOrType(node.type, tc, cwast.NO_TYPE, pm)

0 commit comments

Comments
 (0)