diff options
| author | XmiliaH <xmilia.hermit@gmail.com> | 2024-10-11 18:45:38 +0200 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-10-11 12:24:34 -0700 |
| commit | 60385cfcb4b972e4165749ea8336ba96ae7e6a8c (patch) | |
| tree | c12f40577d8ba980246b9433672922f23e07fa1b /chapter15/src/test | |
| parent | 95276331d269cf0d93c242fcd5dfaab909237df7 (diff) | |
| download | Simple-60385cfcb4b972e4165749ea8336ba96ae7e6a8c.tar.gz Simple-60385cfcb4b972e4165749ea8336ba96ae7e6a8c.zip | |
Initialize int and flt field in NewNode
Diffstat (limited to 'chapter15/src/test')
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java | 17 | ||||
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java | 18 |
2 files changed, 32 insertions, 3 deletions
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<n; i++) body[i+type._fields.length-1] = 0L; @@ -200,8 +200,20 @@ public class Evaluator { } else { assert c instanceof TypeMemPtr; } + num = type._fields.length-1; } else { body = new Object[type._fields.length]; + num = type._fields.length; + } + for (int i=0; i<num; i++) { + var c = type._fields[i]._type; + if (c instanceof TypeInteger) { + body[i] = 0L; + } else if (c instanceof TypeFloat) { + body[i] = 0D; + } else { + assert c instanceof TypeMemPtr; + } } Object[] mems = new Object[type._fields.length+2]; // mems[0] is control @@ -215,7 +227,7 @@ public class Evaluator { var off = vall(load.off()); var idx = getFieldIndex(from.struct, load, (Long)off); if( idx==from.struct._fields.length-1 && from.struct.isAry() ) { - long len = (Long)from.fields[0]; + long len = from.fields.length - from.struct._fields.length + 1; long i = offToIdx(off, from.struct); if( 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); |
