Issue 124359
Summary Kaleidoscope variables are resotred in a First In First Out order rather then First In Last Out
Labels new issue
Assignees
Reporter jmriesen
    # Issue summery
The Kaleidoscope tutorial restores shadowed variables in a First-In-First-Out fashion rather then a First-In-Last-Out fashion. 
All the programming languages I can think of off the top of my head restore variables in an FILO fashion.
Additional there is the associated code comment 
```c++
// Pop all our variables from scope.
```
Which seems to indicate that to me that FILO ordering was intended.
# Example of a program where this difference matters
```
# Note this sequence operator is given as an example of a user defined operator in chapter 6.
def binary : 1 (x y) y;
var a = 9 in (var a = 10, a = 11 in 0):a;
```
I would expect this _expression_ to evaluate to 9, however it currently evaluates to 10
# Purposed solution
Iterate over the `OldBindings` in reverse order.

Existing code
```c++
// Pop all our variables from scope.
for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
    NamedValues[VarNames[i].first] = OldBindings[i];
```

Purposed change
```c++
// Pop all our variables from scope.
for (unsigned i = VarNames.size(); i != 0; --i)
    NamedValues[VarNames[i-1].first] = OldBindings[i-1];
```
# Affected files
A naive ripgrep search shows the following files as affected. 
``` bash
llvm-project % rg "// Pop all our variables from scope." -l
docs-build/docs/html/_sources/tutorial/MyFirstLanguageFrontend/LangImpl07.rst.txt
docs-build/docs/html/tutorial/MyFirstLanguageFrontend/LangImpl08.html
docs-build/docs/html/tutorial/MyFirstLanguageFrontend/LangImpl07.html
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
llvm/examples/Kaleidoscope/Chapter9/toy.cpp
llvm/examples/Kaleidoscope/Chapter7/toy.cpp
llvm/examples/Kaleidoscope/Chapter8/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/lazy/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp
llvm/examples/Kaleidoscope/MCJIT/cached/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/cached/toy-jit.cpp
llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/initial/toy.cpp
llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to