MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / _print

Method _print

chapter23/src/test/java/com/seaofnodes/simple/Eval2.java:419–468  ·  view source on GitHub ↗
( Type t, Object x, SB sb, IdentityHashMap<Object,Object> visit )

Source from the content-addressed store, hash-verified

417 sb.clear();
418 VISIT.clear();
419 }
420
421 // Use type to print x as a string. Uses a new VISIT table so the debugger
422 // doesn't try to reuse VISIT if stopping mid-print. The trace code reuses
423 // VISIT endlessly for some minor speed hope
424 static String prettyPrint( Type t, Object x ) { return _print(t,x,new SB(), new IdentityHashMap<>()).toString(); }
425 static SB _print( Type t, Object x, SB sb, IdentityHashMap<Object,Object> visit ) {
426 if( x instanceof String s ) return sb.p(s);
427 if( x == null ) return sb.p("null");
428 return switch( t ) {
429 case TypeInteger i -> sb.p( (Long)x);
430 case TypeFloat f -> sb.p((Double)x);
431 case TypeMemPtr tmp -> {
432 if( visit.containsKey(x) ) yield sb.p("$cyclic");
433 visit.put(x,x);
434 assert !tmp.isFRef();
435
436 Object[] xs = (Object[])x; // Array of fields
437 if( tmp._obj.isAry() ) {
438 Type elem = tmp._obj._fields[1]._t;
439 if( elem == TypeInteger.U8 ) {
440 // Shortcut u8[] as a String
441 for( Object o : xs )
442 sb.p((char)(long)(Long)o);
443 } else {
444 // Array of elements
445 elem.print(sb).p("[ ");
446 if( elem instanceof TypeConAry ) elem = TypeInteger.ZERO;
447 for( Object o : xs )
448 _print( elem, o, sb, visit ).p( "," );
449 sb.unchar().p("]");
450 }
451 } else {
452 sb.p(tmp._obj._name).p("{");
453 Field[] flds = tmp._obj._fields;
454 for( int i=0; i<flds.length; i++ )
455 _print( flds[i]._t,xs[i], sb.p(flds[i]._fname).p("="), visit ).p(",");
456 sb.unchar().p("}");
457 }
458 yield sb;
459 }
460 case TypeFunPtr tfp -> sb.p(x.toString());
461 case TypeRPC rpc -> sb.p(x.toString());
462 case TypeMem mem -> sb.p("$mem");
463 case TypeTuple tt -> {
464 if( tt._types.length>1 && tt._types[1] instanceof TypeMemPtr )
465 // Assume a NewNode
466 yield _print( tt._types[1], x, sb, visit );
467 throw Utils.TODO();
468 }
469 case Type tt -> {
470 if( tt == Type.NIL ) yield sb.p("null");
471 throw Utils.TODO();

Callers 2

traceDataMethod · 0.95
prettyPrintMethod · 0.95

Calls 8

printMethod · 0.95
TODOMethod · 0.95
pMethod · 0.45
putMethod · 0.45
isFRefMethod · 0.45
isAryMethod · 0.45
uncharMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected