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

Method _print

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

Source from the content-addressed store, hash-verified

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

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