refactor: use Code.forM more (#12649)

This commit is contained in:
Henrik Böving 2026-02-23 15:06:28 +01:00 committed by GitHub
parent 24380fc900
commit e16e2b2ffa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 26 deletions

View file

@ -21,21 +21,13 @@ partial def findSccCalls (scc : Std.HashMap Name (Decl pu)) (decl : Decl pu) : B
| .extern .. => return {}
where
goCode (c : Code pu) : StateRefT (Std.HashSet Name) BaseIO Unit := do
match c with
| .let decl k =>
match decl.value with
| .const name .. | .fap name .. | .pap name .. =>
if scc.contains name then
modify fun s => s.insert name
| _ => pure ()
goCode k
| .fun decl k _ | .jp decl k =>
goCode decl.value
goCode k
| .cases cases => cases.alts.forM (·.forCodeM goCode)
| .jmp .. | .return .. | .unreach .. => return ()
| .uset (k := k) .. | .sset (k := k) .. | .inc (k := k) .. | .dec (k := k) .. =>
goCode k
c.forM fun c => do
if let .let decl _ := c then
match decl.value with
| .const name .. | .fap name .. | .pap name .. =>
if scc.contains name then
modify fun s => s.insert name
| _ => return
end SplitScc

View file

@ -43,24 +43,18 @@ where
return ()
modify fun s => { s with seen := s.seen.insert decl.name }
decl.value.forCodeM visit
decl.value.forCodeM (·.forM visitConsts)
modify fun s => { s with order := s.order.push decl }
visit (code : Code pu) : ToposortM pu Unit := do
visitConsts (code : Code pu) : ToposortM pu Unit := do
match code with
| .let decl k =>
| .let decl _ =>
match decl.value with
| .const declName .. | .fap declName .. | .pap declName .. =>
if let some d := (← read).declsMap[declName]? then
process d
| _ => pure ()
visit k
| .jp decl k | .fun decl k .. =>
visit decl.value
visit k
| .cases c => c.alts.forM fun alt => visit alt.getCode
| .jmp .. | .return .. | .unreach .. => return ()
| .uset (k := k) .. | .sset (k := k) .. | .inc (k := k) .. | .dec (k := k) .. => visit k
| _ => return ()
| _ => return ()
public def toposortDecls (decls : Array (Decl pu)) : CompilerM (Array (Decl pu)) := do
let (externDecls, otherDecls) := decls.partition (fun decl => decl.value matches .extern ..)