From 60385cfcb4b972e4165749ea8336ba96ae7e6a8c Mon Sep 17 00:00:00 2001 From: XmiliaH Date: Fri, 11 Oct 2024 18:45:38 +0200 Subject: Initialize int and flt field in NewNode --- .../test/java/com/seaofnodes/simple/Chapter15Test.java | 17 +++++++++++++++++ .../com/seaofnodes/simple/evaluator/Evaluator.java | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) (limited to 'chapter15/src') diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java index 88924c52..26029cc5 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -267,6 +267,23 @@ return rez; assertEquals(primes[i],(long)(Long)obj.fields()[i+1]); } + @Test + public void testNewNodeInit() { + Parser parser = new Parser( +""" +struct S {int i; flt f;} +S s1 = new S; +S s2 = new S; +s2.i = 3; +s2.f = 2.0; +if (arg) s1 = new S; +return s1.i + s1.f; +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return ((flt).i+.f);", stop.toString()); + assertEquals(0.0, Evaluator.evaluate(stop, 0)); + } + @Test public void testBad0() { diff --git a/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java index a723095c..34921b94 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java @@ -185,13 +185,13 @@ public class Evaluator { private Object alloc(NewNode alloc) { TypeStruct type = alloc._ptr._obj; Object[] body=null; + int num; if( type.isAry() ) { long sz = (Long)val(alloc.in(1)); long n = offToIdx(sz, type); if( n < 0 ) throw new NegativeArraySizeException(""+n); body = new Object[(int)n+1]; // Array body - body[0] = n; // Array length var c = type._fields[type._fields.length-1]._type; if (c instanceof TypeInteger) { for (int i=0; i= len ) throw new ArrayIndexOutOfBoundsException("Array index out of bounds " + i + " < " + len); @@ -231,7 +243,7 @@ public class Evaluator { var val = val(store.val()); var idx = getFieldIndex(to.struct, store, (Long)off); if( idx==to.struct._fields.length-1 && to.struct.isAry() ) { - long len = (Long)to.fields[0]; + long len = to.fields.length - to.struct._fields.length + 1; long i = offToIdx(off, to.struct); if( i < 0 || i >= len ) throw new RuntimeException("Array index out of bounds " + off + " <= " + len); -- cgit v1.2.3