Build with USE_READLINE=1 to switch from libedit to readline.
step6_file.pas step7_quote.pas step8_macros.pas \
step9_try.pas stepA_mal.pas
-STEP0_DEPS = pas-readline/src/readline.pas regexpr/Source/RegExpr.pas
+STEP0_DEPS = mal_readline.pas
STEP1_DEPS = $(STEP0_DEPS) mal_types.pas reader.pas printer.pas
STEP3_DEPS = $(STEP1_DEPS) mal_env.pas
STEP4_DEPS = $(STEP3_DEPS) core.pas
-SOURCES = mal_types.pas mal_func.pas reader.pas printer.pas \
- mal_env.pas core.pas stepA_mal.pas
+SOURCES = mal_readline.pas mal_types.pas mal_func.pas \
+ reader.pas printer.pas mal_env.pas core.pas stepA_mal.pas
SOURCES_LISP = mal_env.pas core.pas stepA_mal.pas
#####################
DEBUG = -gl
-FPC = fpc -MOBJFPC -ve $(DEBUG) -Fupas-readline/src -Furegexpr/Source
+# Set this to link with libreadline instead of libedit
+USE_READLINE =
-all: $(patsubst %.pas,%,$(STEPS))
+FPC = fpc -MOBJFPC -ve -Furegexpr/Source $(DEBUG) $(if $(strip $(USE_READLINE)),-dUSE_READLINE,)
-# Downloaded units
-pas-readline: pas-readline/src/readline.pas
-pas-readline/src/readline.pas:
- curl -L https://github.com/hansiglaser/pas-readline/archive/master.tar.gz | tar xzf -
- mv pas-readline-master pas-readline
-
-#regexpr: regexpr/Source/RegExpr.pas
-#regexpr/Source/RegExpr.pas: regexpr-linux.patch
-# mkdir -p regexpr
-# curl -O http://regexpstudio.com/Downloads/regexpr.zip
-# cd regexpr && unzip ../regexpr.zip
-# rm regexpr.zip
-# patch -p1 < regexpr-linux.patch
+all: $(patsubst %.pas,%,$(STEPS))
step%: step%.pas
$(FPC) $<
uses Classes,
sysutils,
fgl,
- Readline in 'pas-readline/src/readline.pas',
+ mal_readline,
mal_types,
mal_func,
mal_env,
end;
function do_readline(Args: TMalArray) : TMal;
var
- Prompt : PChar;
- Line : PChar;
+ Prompt : string;
+ Line : string;
begin
- Prompt := PChar((Args[0] as TMalString).Val);
- Line := Readline.readline(Prompt);
- if Line = nil then
- do_readline := TMalNil.Create
- else
+ Prompt := (Args[0] as TMalString).Val;
+ try
+ Line := _readline(Prompt);
do_readline := TMalString.Create(Line);
+ except
+ On E : MalEOF do do_readline := TMalNil.Create;
+ end;
end;
function slurp(Args: TMalArray) : TMal;
var
--- /dev/null
+unit mal_readline;
+
+{$H+} // Use AnsiString
+
+interface
+
+uses sysutils,
+ CTypes;
+
+{$IFDEF USE_READLINE}
+
+{$LINKLIB readline}
+
+{$ELSE}
+
+{$LINKLIB libedit}
+
+{$ENDIF}
+
+//
+function readline(Prompt: PChar) : PChar; cdecl; external;
+procedure add_history(Line: PChar); cdecl; external;
+
+type MalEOF = class(Exception);
+
+function _readline(Prompt: string) : string;
+
+implementation
+
+function _readline(Prompt: string) : string;
+var
+ Line : PChar;
+begin
+ Line := readline(PChar(Prompt));
+ if Line = Nil then
+ raise MalEOF.Create('MalEOF');
+ if Line <> '' then
+ add_history(Line);
+
+ _readline := Line;
+end;
+
+end.
type TMalInt = class(TMal)
public
- Val: Longint;
+ Val: int64;
- constructor Create(V : Longint);
+ constructor Create(V : int64);
end;
type TMalString = class(TMal)
// Mal types
//
-constructor TMalInt.Create(V : Longint);
+constructor TMalInt.Create(V : int64);
begin
inherited Create();
Self.Val := V;
program Mal;
+{$H+} // Use AnsiString
+
Uses CMem,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas';
+ mal_readline;
var
Repl_Env: string = '';
- Line : PChar;
+ Line : string;
// read
function READ(const Str: string) : string;
begin
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
- WriteLn(REP(Line));
+ try
+ Line := _readline('user> ');
+ if Line = '' then continue;
+ WriteLn(REP(Line))
+ except
+ On E : MalEOF do Halt(0);
+ end;
end;
end.
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
reader,
printer;
var
- Repl_Env: string = '';
- Line : PChar;
+ Repl_Env : string = '';
+ Line : string;
// read
function READ(const Str: string) : TMal;
begin
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
Uses sysutils,
CMem,
fgl,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
// read
function READ(const Str: string) : TMal;
Repl_Env.Add('/', TMalFunc.Create(@divide));
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
// read
function READ(const Str: string) : TMal;
Repl_Env.Add(TMalSymbol.Create('/'), TMalFunc.Create(@divide));
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
math,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
CmdArgs : TMalArray;
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
math,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
CmdArgs : TMalArray;
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
math,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
CmdArgs : TMalArray;
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
math,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
CmdArgs : TMalArray;
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);
program Mal;
+{$H+} // Use AnsiString
+
Uses sysutils,
CMem,
fgl,
math,
- Readline in 'pas-readline/src/readline.pas',
- History in 'pas-readline/src/history.pas',
+ mal_readline,
mal_types,
mal_func,
reader,
var
Repl_Env : TEnv;
- Line : PChar;
+ Line : string;
I : longint;
Key : string;
CmdArgs : TMalArray;
REP('(println (str "Mal [" *host-language* "]"))');
while True do
begin
- Line := Readline.readline('user> ');
- if Line = Nil then
- Halt(0);
- if Line[0] = #0 then
- continue;
- add_history(Line);
-
try
+ Line := _readline('user> ');
+ if Line = '' then continue;
WriteLn(REP(Line))
except
+ On E : MalEOF do Halt(0);
On E : Exception do
begin
WriteLn('Error: ' + E.message);