prefix expressions

This commit is contained in:
Chuck Smith
2024-02-07 10:51:09 -05:00
parent dcc869a6e2
commit cff4375649
5 changed files with 84 additions and 0 deletions

View File

@@ -74,6 +74,19 @@ func (vm *VM) Run() error {
if err != nil {
return err
}
case code.OpBang:
err := vm.executeBangOperator()
if err != nil {
return err
}
case code.OpMinus:
err := vm.executeMinusOperator()
if err != nil {
return err
}
}
}
@@ -167,6 +180,30 @@ func (vm *VM) executeIntegerComparison(op code.Opcode, left, right object.Object
}
}
func (vm *VM) executeBangOperator() error {
operand := vm.pop()
switch operand {
case True:
return vm.push(False)
case False:
return vm.push(True)
default:
return vm.push(False)
}
}
func (vm *VM) executeMinusOperator() error {
operand := vm.pop()
if operand.Type() != object.INTEGER_OBJ {
return fmt.Errorf("unsupported type for negation: %s", operand.Type())
}
value := operand.(*object.Integer).Value
return vm.push(&object.Integer{Value: -value})
}
func nativeBoolToBooleanObject(input bool) *object.Boolean {
if input {
return True