Reduced memory usage
This commit is contained in:
parent
c027208369
commit
eb160afd91
@ -3,8 +3,8 @@ package ast
|
||||
import "git.akyoto.dev/cli/q/src/build/token"
|
||||
|
||||
// Count counts how often the given token appears in the AST.
|
||||
func Count(body AST, buffer []byte, kind token.Kind, name string) int {
|
||||
count := 0
|
||||
func Count(body AST, buffer []byte, kind token.Kind, name string) uint8 {
|
||||
count := uint8(0)
|
||||
|
||||
for _, node := range body {
|
||||
switch node := node.(type) {
|
||||
|
@ -3,6 +3,7 @@ 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/scope"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
)
|
||||
|
||||
@ -20,5 +21,15 @@ func (f *Function) CompileDefinition(node *ast.Define) error {
|
||||
return errors.New(&errors.UnusedVariable{Name: name}, f.File, node.Name.Position)
|
||||
}
|
||||
|
||||
return f.storeVariableInRegister(name, node.Value, uses)
|
||||
register := f.CurrentScope().MustFindFree(f.cpu.General)
|
||||
f.CurrentScope().Reserve(register)
|
||||
err := f.ExpressionToRegister(node.Value, register)
|
||||
|
||||
f.AddVariable(&scope.Variable{
|
||||
Name: name,
|
||||
Register: register,
|
||||
Alive: uses,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/scope"
|
||||
)
|
||||
|
||||
func (f *Function) storeVariableInRegister(name string, value *expression.Expression, uses int) error {
|
||||
reg := f.CurrentScope().MustFindFree(f.cpu.General)
|
||||
f.CurrentScope().Reserve(reg)
|
||||
err := f.ExpressionToRegister(value, reg)
|
||||
|
||||
f.AddVariable(&scope.Variable{
|
||||
Name: name,
|
||||
Register: reg,
|
||||
Alive: uses,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
@ -37,8 +37,8 @@ func (expr *Expression) AddChild(child *Expression) {
|
||||
}
|
||||
|
||||
// Count counts how often the given token appears in the expression.
|
||||
func (expr *Expression) Count(buffer []byte, kind token.Kind, name string) int {
|
||||
count := 0
|
||||
func (expr *Expression) Count(buffer []byte, kind token.Kind, name string) uint8 {
|
||||
count := uint8(0)
|
||||
|
||||
expr.EachLeaf(func(leaf *Expression) error {
|
||||
if leaf.Token.Kind == kind && leaf.Token.Text(buffer) == name {
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
// Variable represents a named register.
|
||||
type Variable struct {
|
||||
Name string
|
||||
Alive int
|
||||
Alive uint8
|
||||
Depth uint8
|
||||
Register cpu.Register
|
||||
}
|
||||
@ -19,9 +19,9 @@ func (v *Variable) IsAlive() bool {
|
||||
|
||||
// Use reduces the lifetime counter by one.
|
||||
func (v *Variable) Use() {
|
||||
v.Alive--
|
||||
|
||||
if v.Alive < 0 {
|
||||
if v.Alive == 0 {
|
||||
panic("incorrect number of variable use calls")
|
||||
}
|
||||
|
||||
v.Alive--
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package token
|
||||
|
||||
// Count counts how often the given token appears in the token list.
|
||||
func Count(tokens []Token, buffer []byte, kind Kind, name string) int {
|
||||
count := 0
|
||||
func Count(tokens []Token, buffer []byte, kind Kind, name string) uint8 {
|
||||
count := uint8(0)
|
||||
|
||||
for _, t := range tokens {
|
||||
if t.Kind == Identifier && t.Text(buffer) == name {
|
||||
|
Loading…
Reference in New Issue
Block a user