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"
|
"errors"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/compiler/internal"
|
||||||
|
|
||||||
"github.com/MontFerret/ferret/pkg/stdlib"
|
"github.com/MontFerret/ferret/pkg/stdlib"
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
"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) {
|
func (c *Compiler) Compile(query string) (program *vm.Program, err error) {
|
||||||
if query == "" {
|
if query == "" {
|
||||||
return nil, ErrEmptyQuery
|
return nil, internal.ErrEmptyQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -62,24 +64,20 @@ func (c *Compiler) Compile(query string) (program *vm.Program, err error) {
|
|||||||
p := parser.New(query)
|
p := parser.New(query)
|
||||||
p.AddErrorListener(newErrorListener())
|
p.AddErrorListener(newErrorListener())
|
||||||
|
|
||||||
l := newVisitor(query)
|
l := internal.NewVisitor(query)
|
||||||
|
|
||||||
p.Visit(l)
|
p.Visit(l)
|
||||||
|
|
||||||
if l.err != nil {
|
if l.Err != nil {
|
||||||
return nil, l.err
|
return nil, l.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
program = &vm.Program{}
|
program = &vm.Program{}
|
||||||
program.Bytecode = l.emitter.instructions
|
program.Bytecode = l.Emitter.Bytecode()
|
||||||
program.Constants = l.symbols.constants
|
program.Constants = l.Symbols.Constants()
|
||||||
program.CatchTable = l.catchTable
|
program.CatchTable = l.CatchTable
|
||||||
program.Registers = int(l.registers.nextRegister)
|
program.Registers = l.Registers.Size()
|
||||||
program.Params = make([]string, 0, len(l.symbols.params))
|
program.Params = l.Symbols.Params()
|
||||||
|
|
||||||
for _, param := range l.symbols.params {
|
|
||||||
program.Params = append(program.Params, param)
|
|
||||||
}
|
|
||||||
|
|
||||||
return program, err
|
return program, err
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
"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
|
// Allocate assigns a register based on variable type
|
||||||
func (ra *RegisterAllocator) Allocate(regType RegisterType) vm.Operand {
|
func (ra *RegisterAllocator) Allocate(regType RegisterType) vm.Operand {
|
||||||
// Try to find a free register first
|
// Try to find a free register first
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
"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 {
|
func (e *Emitter) Size() int {
|
||||||
return len(e.instructions)
|
return len(e.instructions)
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler
|
package internal
|
||||||
|
|
||||||
import "github.com/pkg/errors"
|
import "github.com/pkg/errors"
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
"github.com/MontFerret/ferret/pkg/vm"
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"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 {
|
func (st *SymbolTable) Scope() int {
|
||||||
return st.scope
|
return st.scope
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package compiler_test
|
package internal_test
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user