-
Notifications
You must be signed in to change notification settings - Fork 98
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
Builtin lines function part II - Functionality #566
Builtin lines function part II - Functionality #566
Conversation
@hdwalters adding these functions to the // iter_loop.rs
fn translate(...) {
// ...
let iterator = match self.expr {
ExprType::LinesInvocation => self.expr.translate_iter_loop(),
_ => self.expr.translate()
}
// ...
} And then we could remove the modifications made to
If we used the statement queue: stmt_queue.push([
format!("for line in read -r {lines}; do"),
"result+=(line)",
"done"
].join("\n"));
// ...
return "result" we'd get the following bash output:
|
Good suggestion; have implemented. The new version is a lot simpler, and touches fewer files. |
please don't merge this without me; im really not sure if this is ok to create a builtin like this |
Ok. If you could explain your concerns, I can hopefully address them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job, @hdwalters 👏👏👏
I'll start working on the new translation layer once we merge this one and publish 0.4.0-alpha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- no failability like in std functions, like when a file doesn't exist
- is this expected behaviour? there should be a test case for it
trust $echo first > a$ for line in lines("a") { trust $echo >> a$ // loops indefinetely }
- compiling this yields invalid bash code:
for line in lines("file") { /* empty */ }
Fair enough, I'll investigate how to do that.
I'm not sure how far we should go to prevent users shooting themselves in the foot like this; after all, there's nothing to stop you making exactly the same mistake writing a native Bash script. And in any case, I don't see how we could catch everywhere this might happen; what if the file gets appended to in a function call, or series of nested function calls? I do not believe this kind of static code analysis is feasible.
It generated valid Bash for me, after I removed your
|
Okay, it does generate invalid Bash with a
But it does exactly the same with an array literal:
So the existence of a comment line seems to prevent it writing the "noop" |
It seems that this case for the single comment happens not only in a loop but also in function declarations and if statements. I'll create a separate issue for it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave a big warning that this function may fail at runtime if the file doesn't exist or no permission, so that the user will check first
@Ph0enixKM and I discussed this, and there doesn't seem to be an easy way to do that right now. We agreed that it would make sense to merge the PR, and come back to it at a later date. |
Add new
LinesInvocation
builtin to iterate over lines in a file, either efficiently calling code in an iteration loop, or setting or appending lines to an array.