Skip to content

Commit fac5e6a

Browse files
committed
fixed an issue with popping stack in interactive mode
1 parent 937617c commit fac5e6a

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

VirtualMachine/uSymbolTable.pas

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,16 @@ function TSymbol.Clone : TSymbol;
345345
end;
346346

347347

348+
348349
function TSymbol.toString : string;
349350
begin
350351
case symbolType of
351-
symBoolean : result := BoolToStr (bValue, True);
352-
symInteger : result := inttostr (iValue);
353-
symDouble : result := floattostr (dValue);
354-
symString : result := sValue.value;
355-
symList : result := lValue.ToString();
352+
symBoolean : result := 'boolean';
353+
symInteger : result := 'integer';
354+
symDouble : result := 'float';
355+
symString : result := 'string';
356+
symList : result := 'list';
357+
symUserFunc : result := 'function';
356358
symUndefined : result := 'Undefined variable';
357359
else
358360
raise Exception.Create('toString type in TSymbol not implemented: ' + inttostr (integer (symbolType)));

VirtualMachine/uVM.pas

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ TVMState = record
4949
symbolTable : TSymbolTable;
5050
end;
5151

52+
// Only used for debugging
53+
TStackInfo = record
54+
stacktop : integer;
55+
end;
56+
5257
TVM = class(TObject)
5358
private
5459
stack: TMachineStack;
@@ -149,6 +154,7 @@ TVM = class(TObject)
149154
procedure collectGarbage;
150155
function getGarbageSize : integer;
151156
public
157+
interactive : boolean;
152158
constructor Create;
153159
destructor Destroy; override;
154160
procedure registerPrintCallBack(fcn: TVMPrintCallBack);
@@ -183,6 +189,7 @@ TVM = class(TObject)
183189
procedure run(code: TProgram; symbolTable : TSymbolTable);
184190
procedure runModule(module: TModule);
185191

192+
function getStackInfo : TStackInfo; // for debuggin purposes)
186193
procedure setcallBack(proc: TVMCallBack);
187194
procedure unsetcallBack;
188195
end;
@@ -217,6 +224,7 @@ constructor TVM.Create;
217224
printCallbackPtr := nil;
218225
printlnCallbackPtr := nil;
219226
assertCounter := 1;
227+
interactive := False;
220228
end;
221229

222230

@@ -229,6 +237,12 @@ destructor TVM.Destroy;
229237
end;
230238

231239

240+
function TVM.getStackInfo : TStackInfo; // for debuggin purposes)
241+
begin
242+
result.stacktop := stackTop;
243+
end;
244+
245+
232246
procedure TVM.registerSetColorCallback (fcn : TVMSetColorCallBack);
233247
begin
234248
setColorCallBackPtr := fcn;
@@ -2084,7 +2098,14 @@ procedure TVM.run (code: TProgram; symbolTable : TSymbolTable);
20842098
oPushNone: push (@noneStackType);
20852099
oPushFunction: pushFunction (c[ip].index1);
20862100
oPushLocalSymbol : pushLocalSymbol (c[ip].index1);
2087-
oPop: value := pop(); // this pop just throws the data away
2101+
oPop: begin
2102+
// If the next instrction is halt, we will leave the item
2103+
// on the stack and let the caller deal with it. This is
2104+
// mainly useful when used in interactive mode so that the
2105+
// console can print the stack item to the console
2106+
if c[ip+1].opCode <> oHalt then
2107+
pop();
2108+
end;
20882109
oDup: dupStack;
20892110
oIsLt: isLt;
20902111
oIsLte: isLte;
@@ -2132,17 +2153,7 @@ procedure TVM.run (code: TProgram; symbolTable : TSymbolTable);
21322153

21332154
oImportModule : importModule (c[ip].moduleName);
21342155
// Method call opcodes
2135-
oCall: begin
2136-
//vmstate.module := self.module;
2137-
//vmstate.symbolTable := self.symbolTable;
2138-
//VMStateStack.push (vmstate);
2139-
2140-
callUserFunction (c[ip].index1);
2141-
2142-
//vmstate := VMStateStack.Pop;
2143-
//self.module := vmstate.module;
2144-
//self.symbolTable := vmstate.symbolTable;
2145-
end;
2156+
oCall: callUserFunction (c[ip].index1);
21462157
oRet:
21472158
begin
21482159
// Note that anything that is returned isn't bound to any symbol.

0 commit comments

Comments
 (0)