Implemented an abstract syntax tree
This commit is contained in:
parent
3b29d4cdee
commit
8453273d73
28 changed files with 422 additions and 315 deletions
|
@ -3,6 +3,7 @@ package build
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/ast"
|
||||
"git.akyoto.dev/cli/q/src/build/config"
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/fs"
|
||||
|
@ -11,8 +12,8 @@ import (
|
|||
|
||||
// Function represents a function.
|
||||
type Function struct {
|
||||
Name string
|
||||
File *fs.File
|
||||
Name string
|
||||
Body token.List
|
||||
compiler
|
||||
}
|
||||
|
@ -26,61 +27,60 @@ func (f *Function) Compile() {
|
|||
}
|
||||
|
||||
// CompileTokens compiles a token list.
|
||||
func (f *Function) CompileTokens(body token.List) error {
|
||||
start := 0
|
||||
groupLevel := 0
|
||||
blockLevel := 0
|
||||
func (f *Function) CompileTokens(tokens token.List) error {
|
||||
tree, err := f.toAST(tokens)
|
||||
|
||||
for i, t := range body {
|
||||
if start == i && t.Kind == token.NewLine {
|
||||
start = i + 1
|
||||
continue
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.CompileAST(tree)
|
||||
}
|
||||
|
||||
// CompileAST compiles an abstract syntax tree.
|
||||
func (f *Function) CompileAST(tree ast.AST) error {
|
||||
for _, node := range tree {
|
||||
if config.Verbose {
|
||||
f.Logf("%T %s", node, node)
|
||||
}
|
||||
|
||||
switch t.Kind {
|
||||
case token.NewLine:
|
||||
if groupLevel > 0 || blockLevel > 0 {
|
||||
continue
|
||||
}
|
||||
if config.Assembler {
|
||||
f.debug = append(f.debug, debug{
|
||||
position: len(f.assembler.Instructions),
|
||||
source: node,
|
||||
})
|
||||
}
|
||||
|
||||
instruction := body[start:i]
|
||||
err := f.CompileNode(node)
|
||||
|
||||
if config.Verbose {
|
||||
f.Logf("compiling: %s", instruction)
|
||||
}
|
||||
|
||||
if config.Assembler {
|
||||
f.debug = append(f.debug, debug{
|
||||
position: len(f.assembler.Instructions),
|
||||
source: instruction,
|
||||
})
|
||||
}
|
||||
|
||||
err := f.CompileInstruction(instruction)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
start = i + 1
|
||||
|
||||
case token.GroupStart:
|
||||
groupLevel++
|
||||
|
||||
case token.GroupEnd:
|
||||
groupLevel--
|
||||
|
||||
case token.BlockStart:
|
||||
blockLevel++
|
||||
|
||||
case token.BlockEnd:
|
||||
blockLevel--
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompileNode compiles a node in the AST.
|
||||
func (f *Function) CompileNode(node ast.Node) error {
|
||||
switch node := node.(type) {
|
||||
case *ast.Call:
|
||||
return f.CompileFunctionCall(node.Expression)
|
||||
|
||||
case *ast.Define:
|
||||
return f.CompileVariableDefinition(node)
|
||||
|
||||
case *ast.Return:
|
||||
return f.CompileReturn(node)
|
||||
|
||||
case *ast.Loop:
|
||||
return f.CompileLoop(node)
|
||||
|
||||
default:
|
||||
panic("Unknown AST type")
|
||||
}
|
||||
}
|
||||
|
||||
// Logf formats a message for verbose output.
|
||||
func (f *Function) Logf(format string, data ...any) {
|
||||
fmt.Printf("[%s @ %d] %s\n", f, len(f.assembler.Instructions), fmt.Sprintf(format, data...))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue