conditionals

This commit is contained in:
Chuck Smith
2024-02-07 15:46:45 -05:00
parent cff4375649
commit 77401260a2
5 changed files with 198 additions and 15 deletions

View File

@@ -87,12 +87,35 @@ func (vm *VM) Run() error {
return err
}
case code.OpJump:
pos := int(code.ReadUint16(vm.instructions[ip+1:]))
ip = pos - 1
case code.OpJumpNotTruthy:
pos := int(code.ReadUint16(vm.instructions[ip+1:]))
ip += 2
condition := vm.pop()
if !isTruthy(condition) {
ip = pos - 1
}
}
}
return nil
}
func isTruthy(obj object.Object) bool {
switch obj := obj.(type) {
case *object.Boolean:
return obj.Value
default:
return true
}
}
func (vm *VM) push(o object.Object) error {
if vm.sp >= StackSize {
return fmt.Errorf("stack overflow")