Skip to content

Commit 4a51dbe

Browse files
committed
Fixed bug in assigning values to indexed arrays
1 parent d465bc6 commit 4a51dbe

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

VirtualMachine/uIntStack.pas

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,24 @@ procedure create (var stack : TStack; n : integer);
2929
procedure push (var stack : TStack; value : integer);
3030
function pop (var stack : TStack) : integer;
3131
function peek (var stack : TStack) : integer;
32+
function getCount (var stack : TStack) : integer;
3233

3334
implementation
3435

3536
procedure create (var stack : TStack; n : integer);
3637
begin
3738
stack.maxSize := n;
39+
stack.stackPtr := -1;
3840
setLength (stack.data, n);
3941
end;
4042

43+
44+
function getCount (var stack : TStack) : integer;
45+
begin
46+
result := stack.stackPtr + 1;
47+
end;
48+
49+
4150
procedure push (var stack : TStack; value : integer);
4251
begin
4352
if stack.stackPtr = stack.maxSize then

VirtualMachine/uVM.pas

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ TVM = class(TObject)
6565

6666
symbolTable : TSymbolTable;
6767
VMStateStack : TStack<TVMState>;
68+
//subscriptStack : TStack<integer>;//
6869
subscriptStack : uIntStack.TStack; // lightweight integer stack
6970

7071
frameStackTop: integer;
@@ -247,6 +248,7 @@ constructor TVM.Create;
247248
createStack(MAX_STACK_SIZE);
248249
createFrameStack(recursionLimit);
249250
VMStateStack := TStack<TVMState>.Create;
251+
// HMS
250252
//subscriptStack := TStack<integer>.Create;
251253
uIntStack.create (subscriptStack, uIntStack.MAX_ENTRIES);
252254

@@ -263,6 +265,7 @@ destructor TVM.Destroy;
263265
freeStack;
264266
freeFrameStack;
265267
VMStateStack.Free;
268+
// HMS
266269
//subscriptStack.Free;
267270
inherited;
268271
end;
@@ -2056,17 +2059,23 @@ procedure TVM.storeIndexableArray(variable: PMachineStackRecord; index: integer;
20562059
raise ERuntimeException.Create('left-hand side must be a array');
20572060

20582061
//if subscriptStack.Count + 1 < nSubscripts then
2059-
if subscriptStack.stackPtr + 1 < nSubscripts then
2062+
if uIntStack.getCount (subscriptStack) + 1 < nSubscripts then
20602063
begin
2064+
//subscriptStack.Push(index);
20612065
uIntStack.Push(subscriptStack, index);
20622066
push (variable);
20632067
end
20642068
else
20652069
begin
20662070
uIntStack.Push(subscriptStack, index);
2067-
setLength (idx, subscriptStack.stackPtr);
2071+
setLength (idx, uIntStack.getCount (subscriptStack));// HMMS subscriptStack.stackPtr);
2072+
2073+
//subscriptStack.Push(index);
2074+
//setLength (idx, subscriptStack.Count);// HMMS subscriptStack.stackPtr);
20682075
// Index backwards since the stack entries are backwards
2069-
for i := subscriptStack.stackPtr - 1 downto 0 do
2076+
//for i := subscriptStack.Count - 1 downto 0 do
2077+
// idx[i] := subscriptStack.Pop ();
2078+
for i := uIntStack.getCount (subscriptStack) - 1 downto 0 do
20702079
idx[i] := uIntStack.Pop(subscriptStack);
20712080

20722081
variable.aValue.setValue (idx, value);
@@ -2272,8 +2281,10 @@ procedure TVM.loadIndexableArray(st: PMachineStackRecord; index: integer; nSubsc
22722281
i : integer;
22732282
begin
22742283
// For an n dimensional array we will collect the subscripts.
2275-
if subscriptStack.StackPtr + 1 < nSubscripts then
2284+
// HMSif subscriptStack.Count + 1 < nSubscripts then
2285+
if uIntStack.getCount (subscriptStack) + 1 < nSubscripts then
22762286
begin
2287+
//subscriptStack.Push(index);
22772288
uIntStack.Push(subscriptStack, index);
22782289
push (st);
22792290
end
@@ -2288,10 +2299,15 @@ procedure TVM.loadIndexableArray(st: PMachineStackRecord; index: integer; nSubsc
22882299
exit;
22892300
end;
22902301

2302+
//subscriptStack.Push(index);
2303+
//setLength (idx, subscriptStack.Count);
22912304
uIntStack.Push(subscriptStack, index);
2292-
setLength (idx, subscriptStack.stackPtr);
2305+
setLength (idx, uIntStack.getCount (subscriptStack));
22932306
// Index backwards since the stack entries are backwards
2294-
for i := subscriptStack.stackPtr - 1 downto 0 do
2307+
2308+
//for i := subscriptStack.Count - 1 downto 0 do
2309+
// idx[i] := subscriptStack.Pop();
2310+
for i := uIntStack.getCount (subscriptStack) - 1 downto 0 do
22952311
idx[i] := uIntStack.Pop(subscriptStack);
22962312

22972313
push (st.aValue.getValue (idx));

0 commit comments

Comments
 (0)