1. pointers to variables ?
- Posted by cjnwl Mar 03, 2023
- 1729 views
Hi,
Another newbie question: are there pointers to variables, or is there any way to simulate them ?
For example, in gawk there are no pointers per se, but you can simulate them using the SYMTAB array:
my_list[1] = "test" var = "my_list" print SYMTAB[var][1]This last line is equivalent to
print my_list[1]
In my gawk app I make extensive use of this ability as I need to manipulate lists whose name I only know at runtime, and I don't know that I'll be able to do this without pointers or something similar that enables me to access the symbol table.
For example, I read an input file that could contain the following lines defining three arrays, one read from a file and two defined inline:
datafile countries countries.list datalist departments HR Admin Facilities Services Sales IT EMT enddata datalist employees joe smith andy bloggs dave simpson ian jones wendy carter jane allenby enddata
Later on in the same input file I may see a line saying
listitem employeesand I have to be able to access the corresponding array to retrieve a random element.
At the moment I'm thinking that I may be able to use a map of lists as a workaround, but I haven't really gone into my existing gawk code to think it through, and if pointers do exist it would make things much more straightforward.
2. Re: pointers to variables ?
- Posted by ChrisB (moderator) Mar 03, 2023
- 1726 views
Hi
As far as I am aware there is no access to Euphoria's symbol table - at the moment.
Cheers
Chris
3. Re: pointers to variables ?
- Posted by ghaberek (admin) Mar 03, 2023
- 1719 views
Another newbie question: are there pointers to variables, or is there any way to simulate them ?
Short answer: no.
Long answer: technically yes, but not in a way that's available to the user. Atoms and sequences are stored in memory and the "value" you're using is actually a pointer. Euphoria uses reference counting with pass-by-value semantics and copy-on-write for modifications. This is why routines like append() return the modified object instead of operating on it directly. Integers are not pointers, are literally signed int values up to "n-bits minus one" i.e. 31 bits on 32-bit systems and 63 bits on 64-bit systems. This is due to the upper bit on integers being used to indicate the value inside is actually pointer to an atom or sequence.
For example, in gawk there are no pointers per se, but you can simulate them using the SYMTAB array:
my_list[1] = "test" var = "my_list" print SYMTAB[var][1]This last line is equivalent to
print my_list[1]
That looks a lot like an associative array? Is that direct use of the symbol table even "legal" in gawk?
In my gawk app I make extensive use of this ability as I need to manipulate lists whose name I only know at runtime, and I don't know that I'll be able to do this without pointers or something similar that enables me to access the symbol table.
While Euphoria does have its own symbol table internally, the only user-facing access to it is via routine_id() which is obviously just for routines. There is no equivalent variable_id(). I think this is mainly due to routines being static at runtime. Routines either exist or they don't, and their "value" (the IL code to execute) is constant. Whereas variables are not only dynamic (their "value" can change) but also ephemeral (they can vanish at almost any time). I'm not against adding variable_id() to the interpreter and I actually did that a long long time ago (c. 2005) but I've since lost that code. If I recall correctly, my changes only worked in the interpreter and could cause memory leaks if used improperly. I was pretty new to all this at the time.
At the moment I'm thinking that I may be able to use a map of lists as a workaround, but I haven't really gone into my existing gawk code to think it through, and if pointers do exist it would make things much more straightforward.
Yep, you're definitely back to maps. From what you're describing, this seems like basically the same thing:
include std/io.e include std/map.e map datalist = map:new() map:put( datalist, "countries", io:read_lines("countries.list") ) map:put( datalist, "departments", { "HR", "Admin", "Facilities", "Services", "Sales", "IT", "EMT" }) map:put( datalist, "employees", { "joe smith", "andy bloggs", "dave simpson", "ian jones", "wendy carter", "jane allenby" }) sequence employees = map:get( datalist, "employees" ) -- etc.
-Greg
4. Re: pointers to variables ?
- Posted by katsmeow Mar 03, 2023
- 1715 views
Hi,
Another newbie question: are there pointers to variables, or is there any way to simulate them ?
Thanks for bringing this up again. Every couple of years, someone needs this feature.
A couple years ago i posted a linked list include with the name of each user "file" in one sequence, and the data of that "file" in the other list. Names and data to be known only at runtime. As usual, i was the only one interested in it, since after asking, and finding it not available, new people promptly leave Euphoria. There's simply less euphoria in Euphoria than people expect now.
Kat
5. Re: pointers to variables ?
- Posted by jimcbrown (admin) Mar 05, 2023
- 1559 views
Hi,
Another newbie question: are there pointers to variables, or is there any way to simulate them ?
Hi
As far as I am aware there is no access to Euphoria's symbol table - at the moment.
Cheers
Chris
Long answer: technically yes, but not in a way that's available to the user.
While Euphoria does have its own symbol table internally, the only user-facing access to it is via routine_id() which is obviously just for routines. There is no equivalent variable_id().
Actually, this is not quite right. This access does exist through the debugger API. You can see https://openeuphoria.org/forum/m/136714.wc for details
But it's not complete - what seems to be missing is the ability to set a value through the API, right now it only offers a read-only view.
For example, in gawk there are no pointers per se, but you can simulate them using the SYMTAB array:
my_list[1] = "test" var = "my_list" print SYMTAB[var][1]This last line is equivalent to
print my_list[1]
I'd just like to point out that using std/eumem might be another option to emulate this. Though you can't turn an existing variable into an eumem reference, and eumem references are integers (or list indices) as opposed to human readable names.
So the above could be written like this
integer var ... integer my_list = eumem:malloc({}) ram_space[my_list][1] = "test" var = my_list print(1, ram_space[var][1]) }}}
In fact, maps (in the solution/implementation suggested by ghaberek) use eumem internally for their implementation.
Thanks for bringing this up again. Every couple of years, someone needs this feature.
Yup, probably the reason why things like eumem and the debugger API exist.
As usual, <snip> finding it not available, new people promptly leave Euphoria. There's simply less euphoria in Euphoria than people expect now.
Kat
Fair point. I think in the specific context of pointers however, this isn't a widely wanted feature. Many other much more popular languages like Python, Ruby, and Javascript lack it and do well without it.
6. Re: pointers to variables ?
- Posted by katsmeow Mar 05, 2023
- 1552 views
As usual, <snip> finding it not available, new people promptly leave Euphoria. There's simply less euphoria in Euphoria than people expect now.
Kat
Fair point. I think in the specific context of pointers however, this isn't a widely wanted feature. Many other much more popular languages like Python, Ruby, and Javascript lack it and do well without it.
Does OE do equally as well without it?
Kat
7. Re: pointers to variables ?
- Posted by ghaberek (admin) Mar 05, 2023
- 1507 views
Actually, this is not quite right. This access does exist through the debugger API. You can see https://openeuphoria.org/forum/m/136714.wc for details
I'm hesitant to suggest that as an answer here. Mostly I don't want to encourage the use of debugging features outside of a debugging context. But also using any debug routine except call_stack() requires the debugger to be initialized and debug features are only available in the interpreter so translated code will not work. I also noticed that inlining will hide symbols so they won't be found with symbol_lookup(). Using without inline resolves this but at a (small) performance cost. But that's another reason to keep this as debug-only since performance isn't a priority when debugging.
But it's not complete - what seems to be missing is the ability to set a value through the API, right now it only offers a read-only view.
I'm not sure we should want to set a value this way. I guess I don't see a problem using it as a more direct pass-by-reference feature (instead of eumem) but I'm worried one could clobber the wrong data and bring the whole thing crashing down. Hopefully we can agree that doing any modification to the symbol table itself is a bad idea. The symbol table should be static at runtime. There may come a time when we can manipulate the symbol table as we like but today is not that day.
-Greg
8. Re: pointers to variables ?
- Posted by cjnwl Mar 06, 2023
- 1497 views
Thanks all for your responses, sample code, and insights into how variables are managed internally in euphoria.
Using SYMTAB in gawk (GNU-awk) is allowed, both reading and modifying values, but not creating new variables. And the ability to use this feature to emulate pointers is explicitly mentioned in the doc.
Gawk arrays are indeed associative arrays, there are no 0- or 1-indexed arrays as known in other languages. Maps seem to be the closest equivalent in OE, and so far I've been able to port other parts of my gawk code, which uses associative arrays extensively, to OE using maps, so I'll have to see how easily I can port my datalist functionalities too.
9. Re: pointers to variables ?
- Posted by jimcbrown (admin) Mar 06, 2023
- 1447 views
I'm not sure we should want to set a value this way. I guess I don't see a problem using it as a more direct pass-by-reference feature (instead of eumem) but I'm worried one could clobber the wrong data and bring the whole thing crashing down. Hopefully we can agree that doing any modification to the symbol table itself is a bad idea. The symbol table should be static at runtime. There may come a time when we can manipulate the symbol table as we like but today is not that day.
-Greg
Yup, agreed. Especially considering:
Using SYMTAB in gawk (GNU-awk) is allowed, both reading and modifying values, but not creating new variables. And the ability to use this feature to emulate pointers is explicitly mentioned in the doc.
Moving on..
I'm hesitant to suggest that as an answer here. Mostly I don't want to encourage the use of debugging features outside of a debugging context. But that's another reason to keep this as debug-only
Yeah, in retrospect I should have added a "DO NOT USE UNLESS YOU KNOW WHAT YOU ARE DOING" warning to that.
I just wanted to point out that technically, it was now technically possible, and therefore not a case of "we lack that feature". But certainly eumem and maps maybe an easier way of doing the same.
10. Re: pointers to variables ?
- Posted by jimcbrown (admin) Mar 06, 2023
- 1446 views
Fair point. I think in the specific context of pointers however, this isn't a widely wanted feature. Many other much more popular languages like Python, Ruby, and Javascript lack it and do well without it.
Does OE do equally as well without it?
Kat
Well, as pointed out earlier in this thread https://openeuphoria.org/forum/m/137710.wc I'd argue that OE technically has the pointers feature. But I don't think Phix has this, and RDS Eu doesn't either, so from that I'd say the evidence suggests that we don't need it.
11. Re: pointers to variables ?
- Posted by katsmeow Mar 06, 2023
- 1445 views
Fair point. I think in the specific context of pointers however, this isn't a widely wanted feature. Many other much more popular languages like Python, Ruby, and Javascript lack it and do well without it.
Does OE do equally as well without it?
Kat
Well, as pointed out earlier in this thread https://openeuphoria.org/forum/m/137710.wc I'd argue that OE technically has the pointers feature. But I don't think Phix has this, and RDS Eu doesn't either, so from that I'd say the evidence suggests that we don't need it.
Ok, i'll just keep on paying my money to mIRC for the feature. Sorry to bother you.
Kat
12. Re: pointers to variables ?
- Posted by jimcbrown (admin) Mar 07, 2023
- 1315 views
Well, as pointed out earlier in this thread https://openeuphoria.org/forum/m/137710.wc I'd argue that OE technically has the pointers feature.
Ok, i'll just keep on paying my money to mIRC for the feature.
Odd that you'd pay for something that OE already provides for free, but hey - it's your money.
Sorry to bother you.
Kat
Apology accepted!
13. Re: pointers to variables ?
- Posted by katsmeow Mar 07, 2023
- 1270 views
Well, as pointed out earlier in this thread https://openeuphoria.org/forum/m/137710.wc I'd argue that OE technically has the pointers feature.
In OE, it's debug only, and read only. But, whatever you say.
Kat
14. Re: pointers to variables ?
- Posted by ghaberek (admin) Mar 07, 2023
- 1261 views
In OE, it's debug only, and read only.
We've suggested that it be regarded as such for now. No one is stopping anyone from poking at it with a stick to see what happens. But offering up the ability to manipulate the symbol table at runtime as a "feature" of Euphoria would be unwise... for now.
-Greg