Cleaned up function type

This commit is contained in:
Eduard Urbach 2024-06-29 11:49:32 +02:00
parent f870a5f1f8
commit 67b3a3d820
Signed by: eduard
GPG key ID: 49226B848C78F6C8
14 changed files with 310 additions and 294 deletions

28
src/build/Loop.go Normal file
View file

@ -0,0 +1,28 @@
package build
import (
"fmt"
"git.akyoto.dev/cli/q/src/build/token"
"git.akyoto.dev/cli/q/src/errors"
)
// CompileLoop compiles a loop instruction.
func (f *Function) CompileLoop(tokens token.List) error {
blockStart := tokens.IndexKind(token.BlockStart) + 1
blockEnd := tokens.LastIndexKind(token.BlockEnd)
if blockStart == -1 {
return errors.New(errors.MissingBlockStart, f.File, tokens[0].End())
}
if blockEnd == -1 {
return errors.New(errors.MissingBlockEnd, f.File, tokens[len(tokens)-1].End())
}
loop := fmt.Sprintf("%s_loop_%d", f.Name, f.count.loop)
f.assembler.Label(loop)
defer f.assembler.Jump(loop)
f.count.loop++
return f.CompileTokens(tokens[blockStart:blockEnd])
}