Skip to content

Commit

Permalink
Hint reference parsing (#625)
Browse files Browse the repository at this point in the history
* add current failing tests

* add new grammar

* fix parsing of number as string

* fix grammar to operate sub correctly

* add explanation comments

---------

Co-authored-by: Shourya Goel <shouryagoel10000@gmail.com>
  • Loading branch information
cicr99 and Sh0g0-1758 authored Aug 20, 2024
1 parent 8e99de4 commit d2dfc2d
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 200 deletions.
15 changes: 6 additions & 9 deletions pkg/hintrunner/hinter/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,23 @@ type Operator uint8
const (
Add Operator = iota
Mul
Sub
)

type BinaryOp struct {
Operator Operator
Lhs CellRefer
Rhs ResOperander // (except DoubleDeref and BinaryOp)
Lhs ResOperander
Rhs ResOperander
}

func (bop BinaryOp) String() string {
return "BinaryOperator"
}

func (bop BinaryOp) Resolve(vm *VM.VirtualMachine) (mem.MemoryValue, error) {
lhsAddr, err := bop.Lhs.Get(vm)
lhs, err := bop.Lhs.Resolve(vm)
if err != nil {
return mem.UnknownValue, fmt.Errorf("get lhs address %s: %w", bop.Lhs, err)
}
lhs, err := vm.Memory.ReadFromAddress(&lhsAddr)
if err != nil {
return mem.UnknownValue, fmt.Errorf("read lhs address %s: %w", lhsAddr, err)
return mem.UnknownValue, fmt.Errorf("resolve lhs operand %s: %w", lhs, err)
}

rhs, err := bop.Rhs.Resolve(vm)
Expand Down Expand Up @@ -221,7 +218,7 @@ func (v DoubleDeref) ApplyApTracking(hint, ref zero.ApTracking) Reference {
}

func (v BinaryOp) ApplyApTracking(hint, ref zero.ApTracking) Reference {
v.Lhs = v.Lhs.ApplyApTracking(hint, ref).(CellRefer)
v.Lhs = v.Lhs.ApplyApTracking(hint, ref).(ResOperander)
v.Rhs = v.Rhs.ApplyApTracking(hint, ref).(ResOperander)
return v
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/hintrunner/hinter/operand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,20 @@ func TestResolveAddOp(t *testing.T) {
memory.MemoryValueFromInt(30),
)

// lhs
// Lhs
var ap ApCellRef = 7
lhs := Deref{ap}

// Rhs
var fp FpCellRef = 20
deref := Deref{fp}
rhs := Deref{fp}

operator := Add

bop := BinaryOp{
Operator: operator,
Lhs: ap,
Rhs: deref,
Lhs: lhs,
Rhs: rhs,
}

res, err := bop.Resolve(vm)
Expand All @@ -164,19 +165,20 @@ func TestResolveMulOp(t *testing.T) {
memory.MemoryValueFromInt(5),
)

// lhs
// Lhs
var ap ApCellRef = 7
lhs := Deref{ap}

// Rhs
var fp FpCellRef = 20
deref := Deref{fp}
rhs := Deref{fp}

operator := Mul

bop := BinaryOp{
Operator: operator,
Lhs: ap,
Rhs: deref,
Lhs: lhs,
Rhs: rhs,
}

res, err := bop.Resolve(vm)
Expand Down
Loading

0 comments on commit d2dfc2d

Please sign in to comment.