Moved AST generation to its own package
This commit is contained in:
46
src/build/ast/EachInstruction.go
Normal file
46
src/build/ast/EachInstruction.go
Normal file
@ -0,0 +1,46 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user