package core import ( "git.akyoto.dev/cli/q/src/ast" "git.akyoto.dev/cli/q/src/cpu" "git.akyoto.dev/cli/q/src/expression" "git.akyoto.dev/cli/q/src/token" ) // UsesRegister returns true if evaluating the expression would write or read the given register. func (f *Function) UsesRegister(expr *expression.Expression, register cpu.Register) bool { if expr.IsLeaf() { if expr.Token.Kind != token.Identifier { return false } variable := f.VariableByName(expr.Token.Text(f.File.Bytes)) if variable == nil || variable.Register != register { return false } return true } if ast.IsFunctionCall(expr) { if register == f.CPU.Output[0] { return true } for i, parameter := range expr.Children[1:] { if register == f.CPU.Input[i] { return true } if f.UsesRegister(parameter, register) { return true } } return false } for _, child := range expr.Children { if f.UsesRegister(child, register) { return true } } return false }