robpike / ivy

ivy, an APL-like calculator
Other
1.33k stars 103 forks source link

ivy: statically distinguish local from global variables #105

Closed rsc closed 2 years ago

rsc commented 2 years ago

Following Python's lead, a local variable is one that is written before being read in the program execution order, and a global variable is one that is read before being written.

The statement 'x;' serves to declare x as a global, assuming x has not been assigned yet. We could always define an operator too.

Because we have this analysis, we can also count the number of local variables, assign them indices, and use a slice instead of a map to hold them, so this change does that as well.

FOR DISCUSSION. It is unclear whether this is the right rule, and probably there is still polishing to do. It is unclear where doc changes should be applied, for example.

rsc commented 2 years ago

This still needs docs, and the exact code details will depend on the fix for #95, because the read/write checker should match the order of evaluation at run time. But here it is as a PR for now.

Where would be a good place to document this behavior?

robpike commented 2 years ago

I landed the fix to #95, which was trivial. Documentation for the behavior here should go in doc.go in the section on ops (")help ops"). The final paragraph needs an update - it even mentions a possible change this CL addresses.

rsc commented 2 years ago

Applied all suggestions, added docs.