-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Multi-line functions and control flow like for/do/while and if/else #518
Comments
Definitely! Both loops and function blocks would be very powerful. Thanks for your suggestion. |
I would be willing to help if you point me in the right direction. |
Thanks for your offer. Basically, the expression parser must be extended. We probably need a few new types of Nodes. You can find the relevant code here to have a look: https://github.com/josdejong/mathjs/blob/master/lib/expression/parse.js |
So is there an outline or spec for what the expression parser language should look like in the end? Mulit-line functions could follow matlab syntax: function [output1, output2] = functonName(input1, input2) or something sorta like javascript: f(input1, input2) = { |
Indeed we have to discuss the syntax for functions and loops. We have to keep in mind that the expression parser is mostly useful for a mathematical audience, not a programmer audience, so it makes sense to make it similar syntax from matlab which has a sort of a BASIC style for loops and functions. I don't particularly like the syntax of Matlab without explicit return, though I like being able to return multiple outputs (maybe you could just do the same with destructuring though). It would also be nice if the multiline function syntax would be similar to the oneline function syntax. Just to drop some ideas into the discussion:
|
hello @josdejong, are there any improvements in that way? |
@kbiedrzycki no this feature hasn't been picked up yet. |
Okay @josdejong. Just wondering if that could be done using some regular expressions, I need quite qick solution for that, cause I am creating project with worksheets where user could set few lines of code as functions and then parse them, looking for some solution and wondering how to solve that.. |
That may be possible. When you only accept a function definition enclosing a block of expressions, and not itself as part of a block of expressions, a regexp could work I think. |
As discussed in #1906: support for I'll update the title of this topic a bit to make it easier to find. |
Help would be very welcome here :) |
I'll try to take a stab on this. |
Thanks Mohammad! I think the easiest is to go through parse.js and some of the Node classes like https://github.com/josdejong/mathjs/blob/develop/src/expression/parse.js After that it may be good to first work out a plan and discuss it here. On thing for example that may become relevant when implementing these control flow features is keeping some kind of stack trace or pointer or something, to be able to throw meaningful errors. When you have a larger script with a lot of for loops and if statements, just a message "Error in if statement" is not enough, you want to know which if statement. |
@josdejong thank you so much and i apologize for the delayed reply. I forked the project so i can play around the codebase and get familiar. I will use this issue for design discussions, feedback, and implementation details. |
👍 enjoy! |
@josdejong I start on a branch on a forked repo. Here is my initial stab on it There is still a lot of work left but wanted to get early feedback on the implementation of IfElseNode before digging deeper. |
Nice! The code looks neat at first sight. I think we should think through the exact syntax that we want to have for if/else, for, do, while (do we want do and while?), a multi line function. Do you have any ideas? We could stay close to JavaScript, or look at Matlab or Python for inspiration. |
I don't have a particular opinion on the exact syntax. I just used Personally, I'm leaning toward staying close to javascript syntax: however, with matlab, what do you think? |
Who is the target audience? I think Matlab syntax is a little easier for
less experienced programmers... No scary { } everywhere.
Also I'm pretty sure this is valid Matlab:
if true; x=4; end. They use ; not newline. That being said newline might
also be easier for the same reason... No pesky ; everywhere.
Just a thought.
…On Thu, Jul 23, 2020, 12:02 AM Mohammad Asad Mohammad < ***@***.***> wrote:
I don't have a particular opinion on the exact syntax. I just used if end
from matlab as a start to get the ball rolling.
Personally, I'm leaning toward staying close to javascript syntax: If
(true) { x = 4 }. The only reason is that it would simplify implementing
parsing for each scope. Also we could support single line expressions.
however, with matlab, if end the user is required to use new lines for
every if condition even if it's small.
what do you think?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#518 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABNJ7P6TLSXCN3GZ7VFLUHTR46Y6JANCNFSM4BWXAUIQ>
.
|
It makes sense to choose a syntax similar to Matlab: the expression parser is aimed at mathematicians, not hard core C programmers :). please no required semicolons at the end of a line. Semicolons are already supported though to support multiple expressions on a single line. I do like the Matlab syntax of for c = 1:ncols
for r = 1:nrows
if r == c
A(r,c) = 2;
elseif abs(r-c) == 1
A(r,c) = -1;
else
A(r,c) = 0;
end
end
end As for mutiline functions, I think we need to divert there a bit. The syntax should be somehow consistent with the way we write a single line function, which is:
A multiline function could look like:
or:
Matlab doesn't work with |
thank you for the feedback. you are right that Matlab syntax is more friendly to non programmers. one question i have if how to handle the following case 2;
3
if true
5;
6
end From my understanding of how semi columns work in in the parser, this will return a result set of |
as for functions, my only confusion with the having another way to return results can potentially be confusing. that said, i do like that |
Agree, ok let's go for a Matlab-like style 👍 I think we should allow the current solution with semicolon |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Hello, I was wondering if there's a plan to support multi-line functions in the future? Also control flows like if/elseif/else and for/while loops? I read in the docs that this is targeting math people not programmers but I feel like these features are critical to solving real world problems.
Thanks!
The text was updated successfully, but these errors were encountered: