q/src/build/Keyword.go

47 lines
1.1 KiB
Go

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() {
case "return":
if len(line) > 1 {
value := expression.Parse(line[1:])
defer value.Close()
// TODO: Set the return value
}
f.Assembler.Return()
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])
default:
return errors.New(&errors.KeywordNotImplemented{Keyword: line[0].Text()}, f.File, line[0].Position)
}
return nil
}