Implemented an abstract syntax tree

This commit is contained in:
Eduard Urbach 2024-06-30 22:54:59 +02:00
parent 3b29d4cdee
commit 8453273d73
Signed by: eduard
GPG key ID: 49226B848C78F6C8
28 changed files with 422 additions and 315 deletions

View file

@ -3,26 +3,14 @@ package build
import (
"fmt"
"git.akyoto.dev/cli/q/src/build/token"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/build/ast"
)
// 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)
func (f *Function) CompileLoop(loop *ast.Loop) error {
label := fmt.Sprintf("%s_loop_%d", f.Name, f.count.loop)
f.assembler.Label(label)
defer f.assembler.Jump(label)
f.count.loop++
return f.CompileTokens(tokens[blockStart:blockEnd])
return f.CompileAST(loop.Body)
}