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

View file

@ -1,46 +1,20 @@
package build
import (
"fmt"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
"git.akyoto.dev/cli/q/src/errors"
)
// CompileKeyword compiles an instruction that starts with a keyword.
func (f *Function) CompileKeyword(line token.List) error {
switch line[0].Text() {
func (f *Function) CompileKeyword(tokens token.List) error {
switch tokens[0].Text() {
case "return":
if len(line) > 1 {
value := expression.Parse(line[1:])
defer value.Close()
// TODO: Set the return value
}
f.Assembler.Return()
return f.CompileReturn(tokens)
case "loop":
blockStart := line.IndexKind(token.BlockStart) + 1
blockEnd := line.LastIndexKind(token.BlockEnd)
if blockStart == -1 {
return errors.New(errors.MissingBlockStart, f.File, line[0].End())
}
if blockEnd == -1 {
return errors.New(errors.MissingBlockEnd, f.File, line[len(line)-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(line[blockStart:blockEnd])
return f.CompileLoop(tokens)
default:
return errors.New(&errors.KeywordNotImplemented{Keyword: line[0].Text()}, f.File, line[0].Position)
return errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text()}, f.File, tokens[0].Position)
}
return nil
}