mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-13 19:52:52 +02:00
Move compiler-related components to internal
package
Refactored the structure by relocating `compiler` elements into an `internal` package. Made `visitor` struct and methods exported for broader access. Added new utility methods in `Emitter` and `RegisterAllocator` to optimize functionality.
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"errors"
|
||||
goruntime "runtime"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/compiler/internal"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/stdlib"
|
||||
"github.com/MontFerret/ferret/pkg/vm"
|
||||
|
||||
@@ -35,7 +37,7 @@ func New(setters ...Option) *Compiler {
|
||||
|
||||
func (c *Compiler) Compile(query string) (program *vm.Program, err error) {
|
||||
if query == "" {
|
||||
return nil, ErrEmptyQuery
|
||||
return nil, internal.ErrEmptyQuery
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@@ -62,24 +64,20 @@ func (c *Compiler) Compile(query string) (program *vm.Program, err error) {
|
||||
p := parser.New(query)
|
||||
p.AddErrorListener(newErrorListener())
|
||||
|
||||
l := newVisitor(query)
|
||||
l := internal.NewVisitor(query)
|
||||
|
||||
p.Visit(l)
|
||||
|
||||
if l.err != nil {
|
||||
return nil, l.err
|
||||
if l.Err != nil {
|
||||
return nil, l.Err
|
||||
}
|
||||
|
||||
program = &vm.Program{}
|
||||
program.Bytecode = l.emitter.instructions
|
||||
program.Constants = l.symbols.constants
|
||||
program.CatchTable = l.catchTable
|
||||
program.Registers = int(l.registers.nextRegister)
|
||||
program.Params = make([]string, 0, len(l.symbols.params))
|
||||
|
||||
for _, param := range l.symbols.params {
|
||||
program.Params = append(program.Params, param)
|
||||
}
|
||||
program.Bytecode = l.Emitter.Bytecode()
|
||||
program.Constants = l.Symbols.Constants()
|
||||
program.CatchTable = l.CatchTable
|
||||
program.Registers = l.Registers.Size()
|
||||
program.Params = l.Symbols.Params()
|
||||
|
||||
return program, err
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package compiler
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/MontFerret/ferret/pkg/vm"
|
||||
@@ -45,6 +45,10 @@ func NewRegisterAllocator() *RegisterAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
func (ra *RegisterAllocator) Size() int {
|
||||
return int(ra.nextRegister)
|
||||
}
|
||||
|
||||
// Allocate assigns a register based on variable type
|
||||
func (ra *RegisterAllocator) Allocate(regType RegisterType) vm.Operand {
|
||||
// Try to find a free register first
|
@@ -1,4 +1,4 @@
|
||||
package compiler
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/MontFerret/ferret/pkg/vm"
|
||||
@@ -14,6 +14,10 @@ func NewEmitter() *Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Emitter) Bytecode() []vm.Instruction {
|
||||
return e.instructions
|
||||
}
|
||||
|
||||
func (e *Emitter) Size() int {
|
||||
return len(e.instructions)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package compiler
|
||||
package internal
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package compiler
|
||||
package internal
|
||||
|
||||
import (
|
||||
"strings"
|
@@ -1,4 +1,4 @@
|
||||
package compiler
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/MontFerret/ferret/pkg/vm"
|
@@ -1,4 +1,4 @@
|
||||
package compiler
|
||||
package internal
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
@@ -36,6 +36,20 @@ func NewSymbolTable(registers *RegisterAllocator) *SymbolTable {
|
||||
}
|
||||
}
|
||||
|
||||
func (st *SymbolTable) Params() []string {
|
||||
params := make([]string, 0, len(st.params))
|
||||
|
||||
for _, name := range st.params {
|
||||
params = append(params, name)
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
func (st *SymbolTable) Constants() []runtime.Value {
|
||||
return st.constants
|
||||
}
|
||||
|
||||
func (st *SymbolTable) Scope() int {
|
||||
return st.scope
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package compiler_test
|
||||
package internal_test
|
||||
|
||||
import "testing"
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user