Implemented an abstract syntax tree
This commit is contained in:
parent
3b29d4cdee
commit
8453273d73
28 changed files with 422 additions and 315 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue