add nil check in CompareValues and log before comparing value on stack

This commit is contained in:
Neemek 2024-10-09 09:41:20 +02:00
parent 898be71f05
commit 0d92fe235f
Signed by: neemek
GPG key ID: 28360A8951CD0E9B
4 changed files with 8 additions and 12 deletions

View file

@ -153,8 +153,6 @@ func (c *Compiler) Compile(tree Node) {
case CallNodeType: case CallNodeType:
n := tree.(*CallNode) n := tree.(*CallNode)
c.descend()
for _, arg := range n.args { for _, arg := range n.args {
c.Compile(arg) c.Compile(arg)
} }
@ -167,9 +165,6 @@ func (c *Compiler) Compile(tree Node) {
c.add(InstructionPop) c.add(InstructionPop)
} }
// Only descend and remove variables that are no longer within scope when the stack is clean
c.ascend()
case FunctionNodeType: case FunctionNodeType:
n := tree.(*FunctionNode) n := tree.(*FunctionNode)
@ -189,8 +184,6 @@ func (c *Compiler) Compile(tree Node) {
// reset instruction pointer (ip) // reset instruction pointer (ip)
c.ip = 0 c.ip = 0
c.descend()
for _, p := range n.params { for _, p := range n.params {
c.registerVar(p) c.registerVar(p)
} }
@ -205,8 +198,6 @@ func (c *Compiler) Compile(tree Node) {
c.chunk, c.chunk,
} }
c.ascend()
// restore old chunk and ip // restore old chunk and ip
c.chunk = mc c.chunk = mc
c.ip = mip c.ip = mip

View file

@ -11,6 +11,7 @@ func CompareStacks[T Value](t *testing.T, expected []T, actual *Stack[T]) {
} }
for i, v := range expected { for i, v := range expected {
t.Logf("Comparing value at index %d", i)
CompareValues(t, actual.items[i], v) CompareValues(t, actual.items[i], v)
} }

View file

@ -89,16 +89,16 @@ func (v BoolValue) String() string {
} }
// NumberValue Integer or floating-point values // 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 { func (v NumberValue) Type() ValueType {
return NumberValueType return NumberValueType
} }
func (v NumberValue) String() string { func (v NumberValue) String() string {
return strconv.FormatFloat(float64(v), 'g', -1, 32) return strconv.FormatFloat(float64(v), 'g', -1, NumberSize)
} }
type StringValue string type StringValue string

View file

@ -3,6 +3,10 @@ package main
import "testing" import "testing"
func CompareValues(t *testing.T, got Value, want Value) { 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() { if got.Type() != want.Type() {
t.Fatalf("type mismatch: got %v want %v", got.Type(), want.Type()) t.Fatalf("type mismatch: got %v want %v", got.Type(), want.Type())
} }