30 lines
617 B
Go
30 lines
617 B
Go
package scope
|
|
|
|
import (
|
|
"git.urbach.dev/cli/q/src/cpu"
|
|
)
|
|
|
|
// Scope represents an independent code block.
|
|
type Scope struct {
|
|
Variables []*Variable
|
|
Depth uint8
|
|
InLoop bool
|
|
cpu.State
|
|
}
|
|
|
|
// AddVariable adds a new variable to the current scope.
|
|
func (s *Scope) AddVariable(variable *Variable) {
|
|
s.Variables = append(s.Variables, variable)
|
|
s.Use(variable.Value.Register)
|
|
}
|
|
|
|
// VariableByName returns the variable with the given name or `nil` if it doesn't exist.
|
|
func (s *Scope) VariableByName(name string) *Variable {
|
|
for _, v := range s.Variables {
|
|
if v.Name == name {
|
|
return v
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|