Skip to content

Commit

Permalink
IO.print without new line
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Aug 21, 2024
1 parent e5f865f commit 36699e9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
5 changes: 3 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/IO.enso
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import project.Any.Any
import project.Data.Text.Text
import project.Nothing.Nothing
import project.Internal.IO_Helpers

## PRIVATE
ADVANCED
Expand Down Expand Up @@ -29,8 +30,8 @@ print_err message = @Builtin_Method "IO.print_err"
Print the message "Oh yes!" to standard output.

IO.println "Oh yes!"
println : Any -> Nothing
println message = @Builtin_Method "IO.println"
println : Any -> Text -> Nothing
println message nl='\n' = IO_Helpers.println message nl

## PRIVATE
ADVANCED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ private
# building native image version of Enso with Standard
# libraries included in

# needed for Standard.Test.Test_Reporter
# tracked as https://github.com/enso-org/enso/issues/10028
polyglot java import java.io.PrintStream

# needed by Util_Spec
polyglot java import org.enso.base.text.CaseFoldedString as JCaseFoldedString
polyglot java import org.enso.base.text.CaseFoldedString.Grapheme
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
private

println message ln = @Builtin_Method "IO.println"
16 changes: 8 additions & 8 deletions distribution/lib/Standard/Test/0.0.0-dev/src/Test_Reporter.enso
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,17 @@ print_progress current_progress total_count status_text =
truncated_line = if line.length <= progress_width then line else
line.take (progress_width - 3) + '...'

Java_System.out.print '\r'
Java_System.out.print (' ' * progress_width)
Java_System.out.print '\r'
Java_System.out.print truncated_line
Java_System.out.print '\r'
IO.println nl='' '\r'
IO.println nl='' (' ' * progress_width)
IO.println nl='' '\r'
IO.println nl='' truncated_line
IO.println nl='' '\r'

## PRIVATE
clear_progress =
Java_System.out.print '\r'
Java_System.out.print (' ' * progress_width)
Java_System.out.print '\r'
IO.println nl='' '\r'
IO.println nl='' (' ' * progress_width)
IO.println nl='' '\r'

## PRIVATE
type Ignore_Progress_Reporter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package org.enso.interpreter.node.expression.builtin.io;

import java.io.PrintStream;

import org.enso.interpreter.dsl.AcceptsError;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.callable.InvokeCallableNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
import org.enso.interpreter.runtime.callable.argument.CallArgumentInfo;
import org.enso.interpreter.runtime.state.State;
import org.enso.interpreter.runtime.warning.WarningsLibrary;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
Expand All @@ -10,15 +21,6 @@
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import java.io.PrintStream;
import org.enso.interpreter.dsl.AcceptsError;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.callable.InvokeCallableNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
import org.enso.interpreter.runtime.callable.argument.CallArgumentInfo;
import org.enso.interpreter.runtime.state.State;
import org.enso.interpreter.runtime.warning.WarningsLibrary;

@BuiltinMethod(
type = "IO",
Expand All @@ -32,17 +34,18 @@ public abstract class PrintlnNode extends Node {
InvokeCallableNode.DefaultsExecutionMode.EXECUTE,
InvokeCallableNode.ArgumentsExecutionMode.PRE_EXECUTED);

abstract Object execute(VirtualFrame frame, State state, @AcceptsError Object message);
abstract Object execute(VirtualFrame frame, State state, @AcceptsError Object message, Object nl);

@Specialization(guards = "strings.isString(message)")
Object doPrintText(
VirtualFrame frame,
State state,
Object message,
Object nl,
@Shared("interop") @CachedLibrary(limit = "10") InteropLibrary strings) {
EnsoContext ctx = EnsoContext.get(this);
try {
print(ctx.getOut(), strings.asString(message));
print(ctx.getOut(), strings.asString(message), strings.asString(nl));
} catch (UnsupportedMessageException e) {
throw EnsoContext.get(this).raiseAssertionPanic(this, null, e);
}
Expand All @@ -54,6 +57,7 @@ Object doPrint(
VirtualFrame frame,
State state,
Object message,
Object nl,
@Shared("interop") @CachedLibrary(limit = "10") InteropLibrary strings,
@CachedLibrary(limit = "10") WarningsLibrary warnings,
@Cached("buildSymbol()") UnresolvedSymbol symbol,
Expand All @@ -68,21 +72,21 @@ Object doPrint(
}
}

try {
String str;
if (strings.isString(probablyStr)) {
try {
str = strings.asString(probablyStr);
} catch (UnsupportedMessageException e) {
var ctx = EnsoContext.get(this);
throw ctx.raiseAssertionPanic(this, null, e);
}
} else {
str = fallbackToString(probablyStr);
}

EnsoContext ctx = EnsoContext.get(this);
print(ctx.getOut(), str);
print(ctx.getOut(), str, strings.asString(nl));
return ctx.getNothing();
} catch (UnsupportedMessageException e) {
var ctx = EnsoContext.get(this);
throw ctx.raiseAssertionPanic(this, null, e);
}
}

@CompilerDirectives.TruffleBoundary
Expand All @@ -91,8 +95,8 @@ private String fallbackToString(Object obj) {
}

@CompilerDirectives.TruffleBoundary
private void print(PrintStream out, Object str) {
out.println(str);
private void print(PrintStream out, Object str, String nl) {
out.print(str + nl);
}

@NeverDefault
Expand Down

0 comments on commit 36699e9

Please sign in to comment.