diff --git a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java index 136388102c545428d784ceba9356143cab167ae0..22b534649928e94f96477303396dd11cee8ad05b 100644 --- a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java +++ b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java @@ -170,7 +170,14 @@ public class RCommand { consoleHandler.setContext(context); StartupTiming.timestamp("VM Created"); StartupTiming.printSummary(); - return readEvalPrint(context, consoleHandler); + + File srcFile = null; + String fileOption = options.getString(RCmdOption.FILE); + if (fileOption != null) { + srcFile = new File(fileOption); + } + + return readEvalPrint(context, consoleHandler, srcFile); } } @@ -239,6 +246,10 @@ public class RCommand { private static final Source GET_PROMPT = Source.newBuilder("R", ".Internal(getOption('prompt'))", "<prompt>").internal(true).buildLiteral(); private static final Source GET_CONTINUE_PROMPT = Source.newBuilder("R", ".Internal(getOption('continue'))", "<continue-prompt>").internal(true).buildLiteral(); + public static int readEvalPrint(Context context, ConsoleHandler consoleHandler) { + return readEvalPrint(context, consoleHandler, null); + } + /** * The read-eval-print loop, which can take input from a console, command line expression or a * file. There are two ways the repl can terminate: @@ -249,8 +260,9 @@ public class RCommand { * In case 2, we must implicitly execute a {@code quit("default, 0L, TRUE} command before * exiting. So,in either case, we never return. */ - public static int readEvalPrint(Context context, ConsoleHandler consoleHandler) { + public static int readEvalPrint(Context context, ConsoleHandler consoleHandler, File srcFile) { int lastStatus = 0; + int line = 0; try { while (true) { // processing inputs boolean doEcho = doEcho(context); @@ -260,6 +272,7 @@ public class RCommand { if (input == null) { throw new EOFException(); } + line++; String trInput = input.trim(); if (trInput.equals("") || trInput.charAt(0) == '#') { // nothing to parse @@ -271,7 +284,13 @@ public class RCommand { while (true) { // processing subsequent lines while input is incomplete lastStatus = 0; try { - context.eval(Source.newBuilder("R", sb.toString(), "<REPL>").interactive(true).buildLiteral()); + Source src; + if (srcFile != null) { + src = Source.newBuilder("R", sb.toString(), srcFile.getName() + "#" + line).interactive(true).uri(srcFile.toURI()).buildLiteral(); + } else { + src = Source.newBuilder("R", sb.toString(), "<REPL>").interactive(true).buildLiteral(); + } + context.eval(src); } catch (PolyglotException e) { if (continuePrompt == null) { continuePrompt = doEcho ? getContinuePrompt(context) : null; diff --git a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RscriptCommand.java b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RscriptCommand.java index e4ca8fa47dd8e44941047937ff8f9eea6d58b639..0469f798539a393238d087829d9bb02843a0f2bf 100644 --- a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RscriptCommand.java +++ b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RscriptCommand.java @@ -22,6 +22,7 @@ */ package com.oracle.truffle.r.launcher; +import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; @@ -140,7 +141,8 @@ public class RscriptCommand { try (Context context = Context.newBuilder().allowHostAccess(useJVM).options(polyglotOptions).arguments("R", arguments).in(consoleHandler.createInputStream()).out(outStream).err( errStream).build()) { consoleHandler.setContext(context); - return RCommand.readEvalPrint(context, consoleHandler); + String fileOption = options.getString(RCmdOption.FILE); + return RCommand.readEvalPrint(context, consoleHandler, new File(fileOption)); } } }