DWScript を使用して Read–eval–print ループ (REPL) を作成しようとしていますが、これが可能かどうかわかりません。
名前に基づいて、RecompileInContext
そのコンテキストでは問題なく機能すると思いましたが、いくつかの制限が発生しています。
- バグのある行はプログラムに永遠に含まれます: その行が原因で、将来の実行は常に失敗します
- 入力するだけで変数の値を出力する方法が見つかりませんでした。たとえば、
var test = "content";
thenと入力するとtest
、content
が表示されます。私が知る限り、実行ごとに実行されるため、使用print
またはprintln
機能しません
だから私の質問は: DWScript を使用して REPL を作成することは可能ですか?
これが私がこれまでに得たものです:
program DwsRepl;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
dwsComp,
dwsCompiler,
dwsExprs;
var
oCompiler: TDelphiWebScript;
oProgram: IdwsProgram;
oExecution: IdwsProgramExecution;
sInput: string;
begin
try
oCompiler := TDelphiWebScript.Create(nil);
try
oProgram := oCompiler.Compile('', 'ReplScript');
oExecution := oProgram.BeginNewExecution;
try
while True do
begin
Write('> ');
// Read user input
Readln(sInput);
// Exit if needed
if sInput = '' then Break;
// Compile
oCompiler.RecompileInContext(oProgram, sInput);
if not oProgram.Msgs.HasErrors then
begin
oExecution.RunProgram(0);
// Output
if not oExecution.Msgs.HasErrors then
Writeln(oExecution.Result.ToString)
else
Writeln('EXECUTION ERROR: ' + oExecution.Msgs.AsInfo);
end
else
Writeln('COMPILE ERROR: ' + oProgram.Msgs.AsInfo);
end;
finally
oExecution.EndProgram;
end;
finally
oCompiler.Free();
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.