Simplified compiler package

This commit is contained in:
Eduard Urbach 2025-02-12 19:05:40 +01:00
parent bd71a29c04
commit 693ef7e9c3
Signed by: eduard
GPG key ID: 49226B848C78F6C8
11 changed files with 234 additions and 201 deletions

View file

@ -0,0 +1,32 @@
package compiler
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/core"
)
// eachFunction recursively finds all the calls to external functions.
// It avoids calling the same function twice with the help of a hashmap.
func (r *Result) eachFunction(caller *core.Function, traversed map[*core.Function]bool, call func(*core.Function)) {
call(caller)
traversed[caller] = true
for _, x := range caller.Assembler.Instructions {
if x.Mnemonic != asm.CALL {
continue
}
name := x.Data.(*asm.Label).Name
callee, exists := r.Functions[name]
if !exists {
continue
}
if traversed[callee] {
continue
}
r.eachFunction(callee, traversed, call)
}
}