Simplified identifier lookup
This commit is contained in:
parent
6af02d8fa3
commit
6959379182
@ -10,8 +10,9 @@ import (
|
|||||||
// Define defines a new variable.
|
// Define defines a new variable.
|
||||||
func (f *Function) Define(leaf *expression.Expression) (*scope.Variable, error) {
|
func (f *Function) Define(leaf *expression.Expression) (*scope.Variable, error) {
|
||||||
name := leaf.Token.Text(f.File.Bytes)
|
name := leaf.Token.Text(f.File.Bytes)
|
||||||
|
variable, _ := f.Identifier(name)
|
||||||
|
|
||||||
if f.IdentifierExists(name) {
|
if variable != nil {
|
||||||
return nil, errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, leaf.Token.Position)
|
return nil, errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, leaf.Token.Position)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ func (f *Function) Define(leaf *expression.Expression) (*scope.Variable, error)
|
|||||||
return nil, errors.New(&errors.UnusedVariable{Name: name}, f.File, leaf.Token.Position)
|
return nil, errors.New(&errors.UnusedVariable{Name: name}, f.File, leaf.Token.Position)
|
||||||
}
|
}
|
||||||
|
|
||||||
variable := &scope.Variable{
|
variable = &scope.Variable{
|
||||||
Name: name,
|
Name: name,
|
||||||
Register: f.NewRegister(),
|
Register: f.NewRegister(),
|
||||||
Alive: uses,
|
Alive: uses,
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/asm"
|
"git.akyoto.dev/cli/q/src/asm"
|
||||||
"git.akyoto.dev/cli/q/src/errors"
|
"git.akyoto.dev/cli/q/src/errors"
|
||||||
"git.akyoto.dev/cli/q/src/expression"
|
"git.akyoto.dev/cli/q/src/expression"
|
||||||
@ -16,7 +14,7 @@ func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Me
|
|||||||
if node.IsLeaf() {
|
if node.IsLeaf() {
|
||||||
if node.Token.Kind == token.Identifier {
|
if node.Token.Kind == token.Identifier {
|
||||||
name := node.Token.Text(f.File.Bytes)
|
name := node.Token.Text(f.File.Bytes)
|
||||||
variable := f.VariableByName(name)
|
variable, function := f.Identifier(name)
|
||||||
|
|
||||||
if variable != nil {
|
if variable != nil {
|
||||||
f.UseVariable(variable)
|
f.UseVariable(variable)
|
||||||
@ -24,11 +22,8 @@ func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Me
|
|||||||
return types.Pointer, nil
|
return types.Pointer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
uniqueName := fmt.Sprintf("%s.%s", f.Package, name)
|
if function != nil {
|
||||||
_, exists := f.Functions[uniqueName]
|
f.MemoryLabel(asm.STORE, memory, function.UniqueName)
|
||||||
|
|
||||||
if exists {
|
|
||||||
f.MemoryLabel(asm.STORE, memory, uniqueName)
|
|
||||||
return types.Pointer, nil
|
return types.Pointer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
src/core/Identifier.go
Normal file
25
src/core/Identifier.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.akyoto.dev/cli/q/src/scope"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Identifier looks up an identifier which can be a variable or a function.
|
||||||
|
func (f *Function) Identifier(name string) (*scope.Variable, *Function) {
|
||||||
|
variable := f.VariableByName(name)
|
||||||
|
|
||||||
|
if variable != nil {
|
||||||
|
return variable, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
uniqueName := fmt.Sprintf("%s.%s", f.Package, name)
|
||||||
|
function, exists := f.Functions[uniqueName]
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
return nil, function
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
// IdentifierExists returns true if the identifier has been defined.
|
|
||||||
func (f *Function) IdentifierExists(name string) bool {
|
|
||||||
variable := f.VariableByName(name)
|
|
||||||
|
|
||||||
if variable != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
_, exists := f.Functions[name]
|
|
||||||
return exists
|
|
||||||
}
|
|
@ -1,8 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.akyoto.dev/cli/q/src/asm"
|
"git.akyoto.dev/cli/q/src/asm"
|
||||||
"git.akyoto.dev/cli/q/src/cpu"
|
"git.akyoto.dev/cli/q/src/cpu"
|
||||||
"git.akyoto.dev/cli/q/src/errors"
|
"git.akyoto.dev/cli/q/src/errors"
|
||||||
@ -16,7 +14,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (*types
|
|||||||
switch t.Kind {
|
switch t.Kind {
|
||||||
case token.Identifier:
|
case token.Identifier:
|
||||||
name := t.Text(f.File.Bytes)
|
name := t.Text(f.File.Bytes)
|
||||||
variable := f.VariableByName(name)
|
variable, function := f.Identifier(name)
|
||||||
|
|
||||||
if variable != nil {
|
if variable != nil {
|
||||||
f.UseVariable(variable)
|
f.UseVariable(variable)
|
||||||
@ -25,12 +23,9 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (*types
|
|||||||
return variable.Type, nil
|
return variable.Type, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
uniqueName := fmt.Sprintf("%s.%s", f.Package, name)
|
if function != nil {
|
||||||
_, exists := f.Functions[uniqueName]
|
|
||||||
|
|
||||||
if exists {
|
|
||||||
f.SaveRegister(register)
|
f.SaveRegister(register)
|
||||||
f.RegisterLabel(asm.MOVE, register, uniqueName)
|
f.RegisterLabel(asm.MOVE, register, function.UniqueName)
|
||||||
return types.Pointer, nil
|
return types.Pointer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user