Simplified file structure

This commit is contained in:
Eduard Urbach 2024-08-07 19:39:10 +02:00
parent cacee7260a
commit a466281307
Signed by: eduard
GPG key ID: 49226B848C78F6C8
219 changed files with 453 additions and 457 deletions

View file

@ -0,0 +1,62 @@
package ast
import "git.akyoto.dev/cli/q/src/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--
if groupLevel > 0 || blockLevel > 0 {
continue
}
err := call(body[start : i+1])
if err != nil {
return err
}
start = i + 1
}
}
if start != len(body) {
return call(body[start:])
}
return nil
}