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

Method _print

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

Source from the content-addressed store, hash-verified

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

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