1. pointers to variables ?

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 employees 
and 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.

new topic     » topic index » view message » categorize

2. Re: pointers to variables ?

Hi

As far as I am aware there is no access to Euphoria's symbol table - at the moment.

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

3. Re: pointers to variables ?

cjnwl said...

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.

cjnwl said...

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?

cjnwl said...

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.

cjnwl said...

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

new topic     » goto parent     » topic index » view message » categorize

4. Re: pointers to variables ?

cjnwl said...

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: pointers to variables ?

cjnwl said...

Hi,

Another newbie question: are there pointers to variables, or is there any way to simulate them ?

ChrisB said...

Hi

As far as I am aware there is no access to Euphoria's symbol table - at the moment.

Cheers

Chris

ghaberek said...

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.

cjnwl said...

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.

katsmeow said...

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.

katsmeow said...

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.

new topic     » goto parent     » topic index » view message » categorize

6. Re: pointers to variables ?

jimcbrown said...
katsmeow said...

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: pointers to variables ?

jimcbrown said...

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.

jimcbrown said...

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: pointers to variables ?

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.

new topic     » goto parent     » topic index » view message » categorize

9. Re: pointers to variables ?

ghaberek said...

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:

cjnwl said...

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..

ghaberek said...

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.

new topic     » goto parent     » topic index » view message » categorize

10. Re: pointers to variables ?

katsmeow said...
jimcbrown said...

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.

new topic     » goto parent     » topic index » view message » categorize

11. Re: pointers to variables ?

jimcbrown said...
katsmeow said...
jimcbrown said...

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

new topic     » goto parent     » topic index » view message » categorize

12. Re: pointers to variables ?

jimcbrown said...

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.

katsmeow said...

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.

katsmeow said...

Sorry to bother you.

Kat

Apology accepted!

new topic     » goto parent     » topic index » view message » categorize

13. Re: pointers to variables ?

jimcbrown said...

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

new topic     » goto parent     » topic index » view message » categorize

14. Re: pointers to variables ?

katsmeow said...

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu