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