Skip to content

Commit

Permalink
Support int greater/less-than-or-equal ops in vm
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jan 15, 2025
1 parent 8ec50ec commit 6ac49ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bbq/vm/value_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,23 @@ func (v IntValue) Less(other IntValue) Value {
return FalseValue
}

func (v IntValue) LessOrEqual(other IntValue) Value {
if v.SmallInt <= other.SmallInt {
return TrueValue
}
return FalseValue
}

func (v IntValue) Greater(other IntValue) Value {
if v.SmallInt > other.SmallInt {
return TrueValue
}
return FalseValue
}

func (v IntValue) GreaterOrEqual(other IntValue) Value {
if v.SmallInt >= other.SmallInt {
return TrueValue
}
return FalseValue
}
18 changes: 18 additions & 0 deletions bbq/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,27 @@ func opBinaryIntLess(vm *VM) {
vm.replaceTop(leftNumber.Less(rightNumber))
}

func opBinaryIntLessOrEqual(vm *VM) {
left, right := vm.peekPop()
leftNumber := left.(IntValue)
rightNumber := right.(IntValue)
vm.replaceTop(leftNumber.LessOrEqual(rightNumber))
}

func opBinaryIntGreater(vm *VM) {
left, right := vm.peekPop()
leftNumber := left.(IntValue)
rightNumber := right.(IntValue)
vm.replaceTop(leftNumber.Greater(rightNumber))
}

func opBinaryIntGreaterOrEqual(vm *VM) {
left, right := vm.peekPop()
leftNumber := left.(IntValue)
rightNumber := right.(IntValue)
vm.replaceTop(leftNumber.GreaterOrEqual(rightNumber))
}

func opTrue(vm *VM) {
vm.push(TrueValue)
}
Expand Down Expand Up @@ -662,8 +676,12 @@ func (vm *VM) run() {
opBinaryIntSubtract(vm)
case opcode.InstructionIntLess:
opBinaryIntLess(vm)
case opcode.InstructionIntLessOrEqual:
opBinaryIntLessOrEqual(vm)
case opcode.InstructionIntGreater:
opBinaryIntGreater(vm)
case opcode.InstructionIntGreaterOrEqual:
opBinaryIntGreaterOrEqual(vm)
case opcode.InstructionTrue:
opTrue(vm)
case opcode.InstructionFalse:
Expand Down

0 comments on commit 6ac49ca

Please sign in to comment.