Refactored code structure

This commit is contained in:
Eduard Urbach 2024-07-03 11:39:24 +02:00
parent 9e52e2dd1c
commit fd6e874b44
Signed by: eduard
GPG key ID: 49226B848C78F6C8
54 changed files with 583 additions and 450 deletions

View file

@ -1,70 +0,0 @@
package build
import (
"sync"
"git.akyoto.dev/cli/q/src/errors"
)
// compile waits for the scan to finish and compiles all functions.
func compile(functions <-chan *Function, errs <-chan error) (Result, error) {
result := Result{}
allFunctions := map[string]*Function{}
for functions != nil || errs != nil {
select {
case err, ok := <-errs:
if !ok {
errs = nil
continue
}
return result, err
case function, ok := <-functions:
if !ok {
functions = nil
continue
}
function.functions = allFunctions
allFunctions[function.Name] = function
}
}
compileFunctions(allFunctions)
for _, function := range allFunctions {
if function.err != nil {
return result, function.err
}
result.InstructionCount += len(function.assembler.Instructions)
}
main, exists := allFunctions["main"]
if !exists {
return result, errors.MissingMainFunction
}
result.Main = main
result.Functions = allFunctions
return result, nil
}
// compileFunctions starts a goroutine for each function compilation and waits for completion.
func compileFunctions(functions map[string]*Function) {
wg := sync.WaitGroup{}
for _, function := range functions {
wg.Add(1)
go func() {
defer wg.Done()
function.Compile()
}()
}
wg.Wait()
}