Skip to content

Commit

Permalink
Organize hintrunner/operand.go (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop committed Sep 5, 2023
1 parent f9ecd9d commit 411782a
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions pkg/hintrunner/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ type CellRefer interface {

type ApCellRef int16

type FpCellRef int16

func (ap ApCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
res, overflow := safemath.SafeOffset(vm.Context.Ap, int16(ap))
if overflow {
Expand All @@ -41,6 +39,8 @@ func (ap ApCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
return vm.MemoryManager.Memory.Peek(VM.ExecutionSegment, res)
}

type FpCellRef int16

func (fp FpCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
res, overflow := safemath.SafeOffset(vm.Context.Fp, int16(fp))
if overflow {
Expand All @@ -55,34 +55,10 @@ func (fp FpCellRef) Get(vm *VM.VirtualMachine) (*memory.Cell, error) {
//
// All ResOperand definitions

type ResOperander interface {
Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error)
}

type Deref struct {
deref CellRefer
}

type DoubleDeref struct {
deref CellRefer
offset int16
}

type Immediate big.Int

type Operator uint8

const (
Add Operator = iota
Mul
)

type BinaryOp struct {
operator Operator
lhs CellRefer
rhs ResOperander // (except DoubleDeref and BinaryOp)
}

func (deref Deref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
cell, err := deref.deref.Get(vm)
if err != nil {
Expand All @@ -91,6 +67,11 @@ func (deref Deref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
return cell.Read(), nil
}

type DoubleDeref struct {
deref CellRefer
offset int16
}

func (dderef DoubleDeref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
cell, err := dderef.deref.Get(vm)
if err != nil {
Expand Down Expand Up @@ -121,6 +102,8 @@ func (dderef DoubleDeref) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, e
return value, nil
}

type Immediate big.Int

// todo(rodro): Specs from Starkware stablish this can be uint256 and not a felt.
// Should we respect that, or go straight to felt?
func (imm Immediate) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
Expand All @@ -133,6 +116,23 @@ func (imm Immediate) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error)
return memory.MemoryValueFromFieldElement(felt), nil
}

type Operator uint8

const (
Add Operator = iota
Mul
)

type ResOperander interface {
Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error)
}

type BinaryOp struct {
operator Operator
lhs CellRefer
rhs ResOperander // (except DoubleDeref and BinaryOp)
}

func (bop BinaryOp) Resolve(vm *VM.VirtualMachine) (*memory.MemoryValue, error) {
cell, err := bop.lhs.Get(vm)
if err != nil {
Expand Down

0 comments on commit 411782a

Please sign in to comment.