Josh @ Dreamland
|
|
Posted on: September 13, 2012, 02:11:37 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I doubt anyone would disagree that ENIGMA needs arrays. What I'm proposing is that rather than inherit array syntax from C++, we take a page out of the JavaScript playbook and denote them with [].
I want to use [] for a couple reasons. First, of course, is that [1, 2, 3][1] looks neater than {1,2,3}[1]. That was a joke; I don't give a shit about that. What I want to allow is this sort of syntax:
[x, y] = [y, x] // Switches the values of x and y [x, y] = get_some_coordinates(); // Fetches a length-2 array of coordinates, assign
Using {} for arrays, {x,y} would be ambiguous; it could be a new scope which uses x and y, or it could be our assignment array. There would be next to no way to distinguish the two, so it's out of the question if we want the assignment array.
As for the dynamics, when compiling to C, this is what it would look like: In the [a,b,c]=array_func() case:
{ Array tmp = array_func(); // This is copied verbatim switch (tmp.length) { default: // Array size is size_t; must be >= 0 case 3: c = tmp[3]; // Waterfall case 2: b = tmp[2]; case 1: a = tmp[1]; case 0: ; } }
In the [a,b,c] = scalar_func() special case:
c = b = a = scalar_func();
As for building an array, that's easy. Old ENIGMA can do that. [expression1, expression2, expression3] simply becomes this: [snip=cpp](Array(3).put(0,expression1).put(1,expression2).put(2,expression3))[/snip]
Where [snip]Array(int N)[/snip] reserves N variants, and [snip]Array& Array::put(int, variant)[/snip] is the same as [snip]Array::operator[](int)::operator=(variant)[/snip].
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
polygone
|
|
Reply #1 Posted on: September 13, 2012, 02:16:13 pm |
|
|
Location: England Joined: Mar 2009
Posts: 794
|
What about this though
a = b [x, y] = get_some_coordinates();
|
|
« Last Edit: September 13, 2012, 02:18:40 pm by polygone »
|
Logged
|
I honestly don't know wtf I'm talking about but hopefully I can muddle my way through.
|
|
|
Josh @ Dreamland
|
|
Reply #2 Posted on: September 13, 2012, 02:19:11 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
This is an EDL feature; the expectation is that the user will distinguish those two with the use of a semicolon. However, if you have an idea to eliminate that, let's hear it.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
Josh @ Dreamland
|
|
Reply #4 Posted on: September 13, 2012, 03:11:02 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
What would you have [] = scalar do?
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
|
Josh @ Dreamland
|
|
Reply #7 Posted on: September 13, 2012, 03:55:20 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
No, that would throw a syntax error to the tune of "Assignment array elements must be variables."
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
Josh @ Dreamland
|
|
Reply #9 Posted on: September 13, 2012, 04:04:34 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
[snip=edl]y = get_some_coordinates()[2][/snip]. But I see the point you're trying to make. I guess we can allow using zero or maybe the null keyword to indicate that you don't want the field; otherwise, you'd do something like this:
int discard; [discard,y] = get_some_coordinates();
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
luiscubal
|
|
Reply #11 Posted on: September 13, 2012, 04:59:20 pm |
|
|
Joined: Jun 2009
Posts: 452
|
Here's my proposal.
1. If function returns a scalar, forbid [] syntax. Completely. It's worthless anyway. 2. If function returns Array, allow [] syntax but force the number of parameters to match. 3. If function returns Array but you don't want to use all parameters, use
[a, b, c, ...] = func();
Still, the array would be forced to have at least 3 elements. 4. If this is *still* not acceptable, you could have this:
[a, b?] = func();
or
[a, b=2]=func();
That code would work for func() returning both 1 or 2 parameters. It'd still be an error for 0 or more than 2 parameters (for 0, use a?. For more than 2, add the "...")
[a?, b?, ...] = func();
This is inspired by function arguments syntax.
As a final note, after an optional variable, all following variables must be optional. So this would still be an error:
[a?, b] = func(); //ERROR!
As an extra note, __unused could be used for worthless variables:
[__unused, x, __unused, __unused?, y?] = func(); cout << __unused << endl; //ERROR: __unused is not defined
If you dislike this syntax, this could also be used (although it might be more error-prone):
[, x, , ?, y?] = func();
|
|
« Last Edit: September 13, 2012, 05:06:43 pm by luiscubal »
|
Logged
|
|
|
|
Josh @ Dreamland
|
|
Reply #12 Posted on: September 13, 2012, 11:38:06 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I don't want to use ?, since ? is ternary. [snip=EDL](boolean_expression ? x : y) = 10;[/snip] is already valid EDL. It will remain so in the arrays, I imagine. I do like your [snip]...[/snip] to denote that the array may be larger. However, I like HaRRi's 0 idea better than __unused.
Perhaps as a prefix, ? could denote optionality. [snip=EDL][0, x, y, ?z] = get_wxyz();[/snip]
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
Josh @ Dreamland
|
|
Reply #14 Posted on: September 14, 2012, 09:02:06 am |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I was just going on about how that'd be fine. All I really need is to make sure JDI includes the & in variable types it coerces, then I can ask JDI for the type of ENIGMA's AST.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|