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

[SR-5860] Support "swift -e" for immediate execution #48430

Closed
belkadan opened this issue Sep 8, 2017 · 9 comments · Fixed by #61171
Closed

[SR-5860] Support "swift -e" for immediate execution #48430

belkadan opened this issue Sep 8, 2017 · 9 comments · Fixed by #61171
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers legacy driver Area → compiler: the integrated C++ legacy driver. Succeeded by the swift-driver project

Comments

@belkadan
Copy link
Contributor

belkadan commented Sep 8, 2017

Previous ID SR-5860
Radar rdar://problem/34310802
Original Reporter @belkadan
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels New Feature, StarterBug
Assignee alberto.lagos (JIRA)
Priority Medium

md5: faa7261c29c8eaec2f0913a8952f8e77

Issue Description:

Currently swift (the command) always reads a file when you want to run a script; if you want to pass the code on the command line, you have to do it through STDIN:

% echo 'print("hello")' | swift -
hello

We can make this a little nicer by supporting -e, like Ruby and Perl:

% swift -e 'print("hello")'
hello
% swift -e 'let x = 42' -e 'print(x)'
42
@belkadan
Copy link
Contributor Author

belkadan commented Sep 8, 2017

The original Apple bug asked for -c, like Python and sh, but since swiftc -c already has a meaning it's probably better to go with -e.

@belkadan
Copy link
Contributor Author

belkadan commented Sep 8, 2017

This is a little more than a starter bug, but not too much:

1. Add a new command-line option in Options.td.
2. Pass it through for an "interpret" job in ToolChains.cpp.
3. Have the compiler construct an input MemoryBuffer with a dummy name in CompilerInvocation.cpp's ParseFrontendArgs. (Separate multiple -e arguments with newlines.)
4. Add tests in test/Driver/ that show that the arguments are getting passed through. (You can use -### to print the commands that would have run without actually running them.)
5. Add tests in test/Interpreter/ that show them actually running.

@belkadan
Copy link
Contributor Author

belkadan commented Sep 8, 2017

Ah, what the heck. I'll tag it anyway. Just let it be known that there might be easier ones. :-)

@swift-ci
Copy link
Collaborator

Comment by Keith Ermel (JIRA)

Think I'll give this a try.

@belkadan the instructions above are pretty clear (thanks!), but I may need a little guidance along the way, some of the details are a little fuzzy right now.

This is the first issue I'll be working on so bear with me, I'm brand new to the codebase, process, etc. so I have plenty to learn!

@belkadan
Copy link
Contributor Author

Sounds good! Feel free to tag me in an eventual pull request (even if it's still WIP), or to send emails to the swift-dev mailing list to ask questions.

@keith
Copy link
Collaborator

keith commented Jul 27, 2018

I started looking into this one and could use some advice. I'm trying to follow the recommendations above and have the argument getting here

swift/lib/Driver/Driver.cpp

Lines 1814 to 1829 in ae6645d

case OutputInfo::Mode::Immediate: {
if (Inputs.empty())
return;
assert(OI.CompilerOutputType == file_types::TY_Nothing);
auto *CA = C.createAction<InterpretJobAction>();
for (const InputPair &Input : Inputs) {
file_types::ID InputType = Input.first;
const Arg *InputArg = Input.second;
CA->addInput(C.createAction<InputAction>(*InputArg, InputType));
}
TopLevelActions.push_back(CA);
return;
}
case OutputInfo::Mode::REPL: {
(which I think follows the recommendations). But now I need to provide some inputs to the `InterpretJobAction`. I'm wondering if this is the right place to create the temporary file and populate it with the passed string(s), or if I should be doing this somewhere else.

Any advice would be appreciated!

@keith
Copy link
Collaborator

keith commented Jul 27, 2018

I've gone ahead with the approach I mentioned above, I'd love feedback on the PR! #18303

@belkadan
Copy link
Contributor Author

Resetting assignee for all Starter Bugs not modified since 2018.

brentdax (JIRA User) actually pushed this one a little past @keith's PR, but didn't get it merged. Anyone should feel free to pick up from there! #18842

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@MarcoEidinger
Copy link
Contributor

Fixed with #61171

@keith keith closed this as completed Feb 18, 2023
@AnthonyLatsis AnthonyLatsis self-assigned this Feb 18, 2023
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation legacy driver Area → compiler: the integrated C++ legacy driver. Succeeded by the swift-driver project and removed new feature labels Feb 18, 2023
@AnthonyLatsis AnthonyLatsis removed their assignment Feb 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers legacy driver Area → compiler: the integrated C++ legacy driver. Succeeded by the swift-driver project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants