25 lines
636 B
Go
25 lines
636 B
Go
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/token"
|
|
)
|
|
|
|
// CompileDefinition compiles a variable definition.
|
|
func (f *Function) CompileDefinition(node *ast.Define) error {
|
|
name := node.Name.Text()
|
|
|
|
if f.identifierExists(name) {
|
|
return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, node.Name.Position)
|
|
}
|
|
|
|
uses := token.Count(f.Body, token.Identifier, name) - 1
|
|
|
|
if uses == 0 {
|
|
return errors.New(&errors.UnusedVariable{Name: name}, f.File, node.Name.Position)
|
|
}
|
|
|
|
return f.storeVariableInRegister(name, node.Value, uses)
|
|
}
|