Cleaned up linter warnings

This commit is contained in:
Eduard Urbach 2024-06-27 20:54:07 +02:00
parent a64169d624
commit 77cfe9ff31
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 29 additions and 28 deletions

View File

@ -74,8 +74,6 @@ func (f *Function) CompileTokens(body token.List) error {
if err != nil { if err != nil {
return err return err
} }
start = -1
} }
start = i + 1 start = i + 1

View File

@ -51,30 +51,11 @@ func compile(functions <-chan *Function, errors <-chan error) (Result, error) {
result.Used = append(result.Used, main) result.Used = append(result.Used, main)
delete(result.Unused, "main") delete(result.Unused, "main")
result.findCalls(main) result.findAliveCode(main)
return result, nil return result, nil
} }
func (result *Result) findCalls(f *Function) {
for _, x := range f.Assembler.Instructions {
if x.Mnemonic != asm.CALL {
continue
}
name := x.Data.(*asm.Label).Name
called, exists := result.Unused[name]
if !exists {
continue
}
result.Used = append(result.Used, called)
delete(result.Unused, name)
result.findCalls(called)
}
}
// compileFunctions starts a goroutine for each function compilation and waits for completion. // compileFunctions starts a goroutine for each function compilation and waits for completion.
func compileFunctions(functions map[string]*Function) { func compileFunctions(functions map[string]*Function) {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@ -90,3 +71,23 @@ func compileFunctions(functions map[string]*Function) {
wg.Wait() wg.Wait()
} }
// findAliveCode recursively finds all the calls to external functions and marks them as required.
func (result *Result) findAliveCode(f *Function) {
for _, x := range f.Assembler.Instructions {
if x.Mnemonic != asm.CALL {
continue
}
name := x.Data.(*asm.Label).Name
called, exists := result.Unused[name]
if !exists {
continue
}
result.Used = append(result.Used, called)
delete(result.Unused, name)
result.findAliveCode(called)
}
}

View File

@ -102,14 +102,15 @@ func TestEachLeaf(t *testing.T) {
expr := expression.Parse(tokens) expr := expression.Parse(tokens)
leaves := []string{} leaves := []string{}
expr.EachLeaf(func(leaf *expression.Expression) error { err := expr.EachLeaf(func(leaf *expression.Expression) error {
leaves = append(leaves, leaf.Token.Text()) leaves = append(leaves, leaf.Token.Text())
return nil return nil
}) })
assert.Nil(t, err)
assert.DeepEqual(t, leaves, []string{"1", "2", "3", "4", "5", "6", "7", "8"}) assert.DeepEqual(t, leaves, []string{"1", "2", "3", "4", "5", "6", "7", "8"})
err := expr.EachLeaf(func(leaf *expression.Expression) error { err = expr.EachLeaf(func(leaf *expression.Expression) error {
return fmt.Errorf("error") return fmt.Errorf("error")
}) })

View File

@ -4,8 +4,8 @@ import (
"git.akyoto.dev/cli/q/src/build/token" "git.akyoto.dev/cli/q/src/build/token"
) )
// List generates a list of expressions from comma separated parameters. // NewList generates a list of expressions from comma separated parameters.
func List(tokens token.List) []*Expression { func NewList(tokens token.List) []*Expression {
var list []*Expression var list []*Expression
EachParameter(tokens, func(parameter token.List) error { EachParameter(tokens, func(parameter token.List) error {

View File

@ -38,7 +38,7 @@ func Parse(tokens token.List) *Expression {
isFunctionCall := isComplete(cursor) isFunctionCall := isComplete(cursor)
if isFunctionCall { if isFunctionCall {
parameters := List(tokens[groupPosition:i]) parameters := NewList(tokens[groupPosition:i])
node := New() node := New()
node.Token.Kind = token.Operator node.Token.Kind = token.Operator

View File

@ -10,10 +10,11 @@ import (
func TestWalk(t *testing.T) { func TestWalk(t *testing.T) {
var files []string var files []string
fs.Walk(".", func(file string) { err := fs.Walk(".", func(file string) {
files = append(files, file) files = append(files, file)
}) })
assert.Nil(t, err)
assert.Contains(t, files, "Walk.go") assert.Contains(t, files, "Walk.go")
assert.Contains(t, files, "Walk_test.go") assert.Contains(t, files, "Walk_test.go")
} }