Comments (3)
Hi Christopher,
Thank you. You are correct. The binding of the variable in for
loop is presently broken, and unfortunately, is likely to remain so for a few months 😦. To illustrate, the following currently works (at least in interpreted mode):
I := 6
print "The factorial of ", I, " is ", I!
But with a for
loop, it does not work (yet, or no longer, depending on how you look at it).
For the longest of time, for
loops have been a problem in the language because one of their arguments is created. In your example, a variable named I
needs to be created, and if you define your own for
loop, the scope of that variable used to be very unclear. The current definition, found here, creates a local variable Var
that does not modify the incoming parameter. It currently looks like this:
for Var in Low..High loop Body is
Var := Low
while Var < High loop
Body
Var := Var + 1
That's very naive for a number of reasons One of them is Var := Var + 1
, which only works for integer types. In your case, that's not a problem, but for I in 'A'..'C'
would be broken by that. But there is nothing in that code that explains how Var
would be anything but a local parameter. Which it presently is.
Earlier versions of the code have been working around this by special-casing it in the compiler (e.g. was the case in Tao3D), or trying to have a special annotation to the parameter (e.g. I:var integer
, etc), which I was never happy with. All these variants broke under some common scenario. For example, you want Var
to be visible in the while
conditions, but to be visible in Body
under some other name, I
in your case.
I believe I finally found the "right" way to express this, which is documented here, where the code will look like this:
for N:name in R:[range of discrete] loop Body is
loop_context is
[[N]] : R.type := R.first
LoopVar is loop_context.[[N]]
while LoopVar <= R.last loop
(loop_context) (Body)
++LoopVar
Here are the things that are broken for this to work:
- Binding of the
name
type to preclude evaluation of the associated name range of discrete
is not available yet, because I just introduced (in the documentation only) thediscrete
type, and a genericrange
was never implemented since theXL2
times (circa 2005-ish)[[N]]
(a meta box in a declaration context to inject a name) is not implemented at all- I'm not entirely sure about the
LoopVar
declaration yet (whether you can modify through it). There are pros and cons. - The notation
(loop_context) (Body)
is supposed to be a scope injection of theloop_context
scope while evaluatingBody
. While some support for this kind of prefix scoping operator made it to the interpreted version about one year ago, it's insufficient and broken in other cases.
So this explains why I believe it will be a while until the for
loop actually works as intended.
Thanks a lot for reporting it, though. I will keep the issue open, and if/when I finally get the for
loop to do the right thing while being written the right way, which will be a big victory, you will know 😄 .
from xl.
In order to show what some earlier implementations of the for
loop used to look like, see this commit
from xl.
- I'm not entirely sure about the
LoopVar
declaration yet (whether you can modify through it). There are pros and cons.
This can have something to do with immutability. Feel free to look at V lang (doc with examples) as a modern take on that (I like the "immutable by default" which I'm not sure XL could easily express (I have a feeling "mutable by default" should be doable in XL, but that partially defeats the added value of immutability). On the other hand I'm not exactly sure whether immutability is that useful in non-imperative style of programming.
So if I understand the problem with LoopVar
correctly then the pros and cons
you're mentioning seem to be rather something XL should factor out and let the programmer choose instead of letting the standard library (or "the language" if you want) decide it up front.
Just my 2 cents 😉.
from xl.
Related Issues (20)
- Cleanup the runtime from old / obsolete functions
- Convert XL native modules to XL_NATIVE
- Implement modules
- Error evaluation rules
- Remove remnants of bytecode interpreter (or, alternatively, fix it) HOT 5
- Fast compiler similar to what was used in Tao3D HOT 1
- Improve llvm-crap to deal with deprecated functions in LLVM 9.0.1
- Chat room? HOT 1
- [Comment/Suggestion] The Definition Operator HOT 5
- Conceptual question: how is this language different than other prologs? HOT 1
- Imperative flavour: continue & break in loops HOT 4
- Add to https://github.com/kostya/benchmarks/
- NULL/nil/none/void/... value considered harmful (a "billion dollar mistake" of Tony Hoare) HOT 10
- Treat float literals as decimal float literals and not as binary float literals HOT 2
- Nubie feedback - can't build HOT 4
- Can't build native/HelloWorld HOT 3
- Basic Examples Compiler Explanation HOT 1
- Data Inheritance HOT 1
- A small scheme VM HOT 1
- Typo in opcodes.h HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xl.