-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpaslox.lpr
More file actions
80 lines (65 loc) · 1.54 KB
/
paslox.lpr
File metadata and controls
80 lines (65 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
program paslox;
{$mode objfpc}{$H+}
uses
SysUtils, uChunk, uMemory, uValue, uDebug, uVM;
procedure Repl;
var
Line: String;
begin
while True do
begin
Write('paslox > ');
ReadLn(Line);
if Line = '' then Break;
Line := Line;
Interpret(Line);
end;
end;
function ReadFile(const FileName: TFileName): String;
var
InputFile: THandle;
FileSize, BytesRead: Integer;
Buffer: String;
begin
try
InputFile := FileOpen(FileName, fmOpenRead);
if InputFile = -1 then
begin
WriteLn(Format('Error opening file "%s".', [FileName]));
Halt(74);
end;
FileSize := FileSeek(InputFile, 0, fsFromEnd);
SetLength(Buffer, FileSize);
FileSeek(InputFile, 0, fsFromBeginning);
BytesRead := FileRead(InputFile, Buffer[1], FileSize);
if BytesRead < FileSize then
begin
WriteLn(Format('Error reading file "%s".', [FileName]));
Halt(74);
end;
Result := Buffer;
finally
FileClose(InputFile);
end;
end;
procedure RunFile(const Path: String);
var
Source: String;
InterpretResult: TInterpretResult;
begin
Source := ReadFile(Path);
InterpretResult := Interpret(Source);
if InterpretResult = irCompileError then Halt(65);
if InterpretResult = irRuntimeError then Halt(70);
end;
begin
InitVM;
case ParamCount of
0: Repl;
1: RunFile(ParamStr(1));
else
WriteLn('Usage: paslox [path]');
Halt(64);
end;
FreeVM;
end.