66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package core
|
|
|
|
import (
|
|
"git.akyoto.dev/cli/q/src/build/ast"
|
|
"git.akyoto.dev/cli/q/src/build/errors"
|
|
"git.akyoto.dev/cli/q/src/build/token"
|
|
)
|
|
|
|
// Compile turns a function into machine code.
|
|
func (f *Function) Compile() {
|
|
defer close(f.finished)
|
|
f.AddLabel(f.Name)
|
|
f.Err = f.CompileTokens(f.Body)
|
|
f.Return()
|
|
}
|
|
|
|
// CompileTokens compiles a token list.
|
|
func (f *Function) CompileTokens(tokens token.List) error {
|
|
body, err := ast.Parse(tokens)
|
|
|
|
if err != nil {
|
|
err.(*errors.Error).File = f.File
|
|
return err
|
|
}
|
|
|
|
return f.CompileAST(body)
|
|
}
|
|
|
|
// CompileAST compiles an abstract syntax tree.
|
|
func (f *Function) CompileAST(tree ast.AST) error {
|
|
for _, node := range tree {
|
|
err := f.CompileASTNode(node)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CompileASTNode compiles a node in the AST.
|
|
func (f *Function) CompileASTNode(node ast.Node) error {
|
|
switch node := node.(type) {
|
|
case *ast.Assign:
|
|
return f.CompileAssign(node)
|
|
|
|
case *ast.Call:
|
|
return f.CompileCall(node.Expression)
|
|
|
|
case *ast.Define:
|
|
return f.CompileDefinition(node)
|
|
|
|
case *ast.Return:
|
|
return f.CompileReturn(node)
|
|
|
|
case *ast.If:
|
|
return f.CompileIf(node)
|
|
|
|
case *ast.Loop:
|
|
return f.CompileLoop(node)
|
|
|
|
default:
|
|
panic("unknown AST type")
|
|
}
|
|
}
|