Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add eval (-e) expression evaluation to command line #14263

Merged
merged 3 commits into from Jan 20, 2022
Merged

add eval (-e) expression evaluation to command line #14263

merged 3 commits into from Jan 20, 2022

Conversation

philwalk
Copy link
Contributor

@philwalk philwalk commented Jan 13, 2022

Implements a command line expression evaluator.

bin/scala -e 'println("Hello World!")'
Hello World!

bin/scala -e 'println(33*56)'
1848

# bin/scala -e 'val x = 100f ; println(x)'
100.0

A temporary script file is created and executed, with a prefix inserted:

@main def main(args: String *): Unit =
  <expression-goes-here>

The -save option is disabled, to avoid uselessly creating a jar.

@@ -149,6 +149,13 @@ object MainGenericRunner {
process(remainingArgs, settings)
case (o @ colorOption(_*)) :: tail =>
process(tail, settings.withScalaArgs(o))
case "-e" :: expression :: tail =>
val tempScript = writeFile(s"@main def main(args: String *): Unit =\n ${expression}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit the newline character as a needless complication.

I don't remember whether scala 2 has to write a file, but I bet it doesn't. Is it necessary to write a file?

Tired recently of deleting scala temp files from my WSL /tmp due to running scala and dotty tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, possibly newline and indent matters for braceless syntax.

Copy link
Contributor Author

@philwalk philwalk Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit the newline character as a needless complication.

Sounds good.

Tired recently of deleting scala temp files from my WSL /tmp due to running scala and dotty tests.

Totally agree. I reviewed the scala2 code, and it doesn't create a file, although it's has different Runners available.
I will poke around a bi more and see if I can find something.

@philwalk
Copy link
Contributor Author

A new class dotty.tools.scripting.StringDriver provides a way to compile and run source code from a String, rather than from a file.

The code it shares with dotty.tools.scripting.ScriptingDriver, was refactored to new file dotty.tools.scripting.Util.

end match
end detectMainClassAndMethod

def pathsep = sys.props("path.separator")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a previous effort to avoid dependencies on packages that might become modules in 2.13, which did not happen. I'm conflicted about sys.props because OT1H it's just a Map interface for System.getProperty or ies, but OTOH it is not so trivial that it didn't harbor a couple of bugs.

I guess the convenience here is java.io.File.pathSeparator. No big deal, I just happened to have recently seen the Scala 2 commit about not using sys.props.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware of the discussion around packages that might become modules, but java.io.File.pathSeparator is a good alternative in any case.

It turns out that pathsep is defined identically in dotty.tools.scripting.Main, so the definition maybe belongs in dotty.tools.scripting.Util.

Copy link
Member

@BarkingBad BarkingBad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@anatoliykmetyuk anatoliykmetyuk merged commit 697e675 into scala:master Jan 20, 2022
@philwalk philwalk deleted the cli-add-expression-eval-12648 branch January 24, 2022 19:13
anatoliykmetyuk added a commit that referenced this pull request Jan 28, 2022
fix ExpressionTest#verifyImports test compile error introduced in #14263
@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants