add nil check in CompareValues and log before comparing value on stack
This commit is contained in:
parent
898be71f05
commit
0d92fe235f
4 changed files with 8 additions and 12 deletions
|
|
@ -153,8 +153,6 @@ func (c *Compiler) Compile(tree Node) {
|
|||
case CallNodeType:
|
||||
n := tree.(*CallNode)
|
||||
|
||||
c.descend()
|
||||
|
||||
for _, arg := range n.args {
|
||||
c.Compile(arg)
|
||||
}
|
||||
|
|
@ -167,9 +165,6 @@ func (c *Compiler) Compile(tree Node) {
|
|||
c.add(InstructionPop)
|
||||
}
|
||||
|
||||
// Only descend and remove variables that are no longer within scope when the stack is clean
|
||||
c.ascend()
|
||||
|
||||
case FunctionNodeType:
|
||||
n := tree.(*FunctionNode)
|
||||
|
||||
|
|
@ -189,8 +184,6 @@ func (c *Compiler) Compile(tree Node) {
|
|||
// reset instruction pointer (ip)
|
||||
c.ip = 0
|
||||
|
||||
c.descend()
|
||||
|
||||
for _, p := range n.params {
|
||||
c.registerVar(p)
|
||||
}
|
||||
|
|
@ -205,8 +198,6 @@ func (c *Compiler) Compile(tree Node) {
|
|||
c.chunk,
|
||||
}
|
||||
|
||||
c.ascend()
|
||||
|
||||
// restore old chunk and ip
|
||||
c.chunk = mc
|
||||
c.ip = mip
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ func CompareStacks[T Value](t *testing.T, expected []T, actual *Stack[T]) {
|
|||
}
|
||||
|
||||
for i, v := range expected {
|
||||
t.Logf("Comparing value at index %d", i)
|
||||
CompareValues(t, actual.items[i], v)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -89,16 +89,16 @@ func (v BoolValue) String() string {
|
|||
}
|
||||
|
||||
// NumberValue Integer or floating-point values
|
||||
type NumberValue float32
|
||||
type NumberValue float64
|
||||
|
||||
const NumberSize int = 32
|
||||
const NumberSize int = 64
|
||||
|
||||
func (v NumberValue) Type() ValueType {
|
||||
return NumberValueType
|
||||
}
|
||||
|
||||
func (v NumberValue) String() string {
|
||||
return strconv.FormatFloat(float64(v), 'g', -1, 32)
|
||||
return strconv.FormatFloat(float64(v), 'g', -1, NumberSize)
|
||||
}
|
||||
|
||||
type StringValue string
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ package main
|
|||
import "testing"
|
||||
|
||||
func CompareValues(t *testing.T, got Value, want Value) {
|
||||
if got == nil || want == nil {
|
||||
t.Fatalf("a value is nil: got %v; want %v", got, want)
|
||||
}
|
||||
|
||||
if got.Type() != want.Type() {
|
||||
t.Fatalf("type mismatch: got %v want %v", got.Type(), want.Type())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue