Added simple variable lifetimes

This commit is contained in:
Eduard Urbach 2024-06-27 15:23:02 +02:00
parent 9e3112ad52
commit 6b5bc22ec6
Signed by: eduard
GPG key ID: 49226B848C78F6C8
6 changed files with 107 additions and 39 deletions

View file

@ -62,6 +62,17 @@ func (f *Function) ExecuteFunctionCall(expr *expression.Expression) error {
// ExecuteLeaf performs an operation on a register with the given leaf operand.
func (f *Function) ExecuteLeaf(operation token.Token, register cpu.Register, operand token.Token) error {
switch operand.Kind {
case token.Identifier:
name := operand.Text()
variable, exists := f.Variables[name]
if !exists {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, operand.Position)
}
defer f.useVariable(variable)
return f.ExecuteRegisterRegister(operation, register, variable.Register)
case token.Number:
value := operand.Text()
number, err := strconv.Atoi(value)
@ -71,16 +82,6 @@ func (f *Function) ExecuteLeaf(operation token.Token, register cpu.Register, ope
}
return f.ExecuteRegisterNumber(operation, register, number)
case token.Identifier:
name := operand.Text()
variable, exists := f.Variables[name]
if !exists {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, operand.Position)
}
return f.ExecuteRegisterRegister(operation, register, variable.Register)
}
return errors.New(errors.NotImplemented, f.File, operation.Position)