47 lines
720 B
Go
47 lines
720 B
Go
package ast
|
|
|
|
import "git.akyoto.dev/cli/q/src/build/token"
|
|
|
|
// EachInstruction calls the function on each instruction.
|
|
func EachInstruction(body token.List, call func(token.List) error) error {
|
|
start := 0
|
|
groupLevel := 0
|
|
blockLevel := 0
|
|
|
|
for i, t := range body {
|
|
if start == i && t.Kind == token.NewLine {
|
|
start = i + 1
|
|
continue
|
|
}
|
|
|
|
switch t.Kind {
|
|
case token.NewLine:
|
|
if groupLevel > 0 || blockLevel > 0 {
|
|
continue
|
|
}
|
|
|
|
err := call(body[start:i])
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
start = i + 1
|
|
|
|
case token.GroupStart:
|
|
groupLevel++
|
|
|
|
case token.GroupEnd:
|
|
groupLevel--
|
|
|
|
case token.BlockStart:
|
|
blockLevel++
|
|
|
|
case token.BlockEnd:
|
|
blockLevel--
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|