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

Method _print

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

Source from the content-addressed store, hash-verified

404 // VISIT endlessly for some minor speed hope
405 static String prettyPrint( Type t, Object x ) { return _print(t,x,new SB(), new IdentityHashMap<>()).toString(); }
406 static SB _print( Type t, Object x, SB sb, IdentityHashMap<Object,Object> visit ) {
407 if( x instanceof String s ) return sb.p(s);
408 if( x == null ) return sb.p("null");
409 return switch( t ) {
410 case TypeInteger i -> sb.p( (Long)x);
411 case TypeFloat f -> sb.p((Double)x);
412 case TypeMemPtr tmp -> {
413 if( visit.containsKey(x) ) yield sb.p("$cyclic");
414 visit.put(x,x);
415 // Since never can close the type cycle, without cyclic types, have
416 // to handle FRefs even here
417 if( tmp.isFRef() )
418 tmp = tmp.makeFrom(((TypeMemPtr)Parser.TYPES.get(tmp._obj._name))._obj);
419
420 Object[] xs = (Object[])x; // Array of fields
421 if( tmp._obj.isAry() ) {
422 Type elem = tmp._obj._fields[1]._type;
423 if( elem == TypeInteger.U8 ) {
424 // Shortcut u8[] as a String
425 for( Object o : xs )
426 sb.p((char)(long)(Long)o);
427 } else {
428 // Array of elements
429 elem.print(sb).p("[ ");
430 for( Object o : xs )
431 _print( elem, o, sb, visit ).p( "," );
432 sb.unchar().p("]");
433 }
434 } else {
435 sb.p(tmp._obj._name).p("{");
436 Field[] flds = tmp._obj._fields;
437 for( int i=0; i<flds.length; i++ )
438 _print( flds[i]._type,xs[i], sb.p(flds[i]._fname).p("="), visit ).p(",");
439 sb.unchar().p("}");
440 }
441 yield sb;
442 }
443 case TypeFunPtr tfp -> sb.p(x.toString());
444 case TypeRPC rpc -> sb.p(x.toString());
445 case TypeMem mem -> sb.p("$mem");
446 case TypeTuple tt -> {
447 if( tt._types.length>1 && tt._types[1] instanceof TypeMemPtr )
448 // Assume a NewNode
449 yield _print( tt._types[1], x, sb, visit );
450 throw Utils.TODO();
451 }
452 case Type tt -> {
453 if( tt == Type.NIL ) yield sb.p("null");
454 throw Utils.TODO();
455 }
456 };
457 }
458}

Callers 2

traceDataMethod · 0.95
prettyPrintMethod · 0.95

Calls 10

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

Tested by

no test coverage detected