3 {$H+} // Use AnsiString
11 function pr_str_array(Args
: TMalArray
;
12 print_readably
: Boolean;
13 Separator
: string) : string;
15 function pr_str(Obj
: TMal
; print_readably
: Boolean) : string;
19 function pr_str_array(Args
: TMalArray
;
20 print_readably
: Boolean;
21 Separator
: string) : string;
27 for I
:= 0 to Length(Args
)-1 do
29 Str
:= Str
+ pr_str(Args
[I
], print_readably
);
30 if I
<> Length(Args
)-1 then
31 Str
:= Str
+ Separator
;
36 function pr_str_dict(Dict
: TMalDict
;
37 print_readably
: Boolean;
38 Separator
: string) : string;
43 SetLength(Arr
, Dict
.Count
* 2);
45 while I
< Dict
.Count
do
47 Arr
[I
*2] := TMalString
.Create(Dict
.Keys
[I
]);
48 Arr
[I
*2+1] := Dict
[Dict
.Keys
[I
]];
51 pr_str_dict
:= pr_str_array(Arr
, print_readably
, ' ');
55 function pr_str(Obj
: TMal
; print_readably
: Boolean) : string;
60 if Obj
.ClassType
= TMalList
then
61 pr_str
:= '(' + pr_str_array((Obj
as TMalList
).Val
,
64 else if Obj
.ClassType
= TMalVector
then
65 pr_str
:= '[' + pr_str_array((Obj
as TMalList
).Val
,
68 else if Obj
is TMalHashMap
then
69 pr_str
:= '{' + pr_str_dict((Obj
as TMalHashMap
).Val
,
72 else if Obj
is TMalString
then
74 Str
:= (Obj
as TMalString
).Val
;
75 if (Length(Str
) > 0) and (Str
[1] = #127) then
76 pr_str
:= ':' + copy(Str
, 2, Length(Str
))
77 else if print_readably
then
79 Str
:= StringReplace(Str
, '\', '\\', [rfReplaceAll
]);
80 Str
:= StringReplace(Str
, '"', '\"', [rfReplaceAll
]);
81 Str
:= StringReplace(Str
, #10, '\n', [rfReplaceAll
]);
82 pr_str
:= Format('"%s"', [Str
])
87 else if Obj
is TMalNil
then
89 else if Obj
is TMalTrue
then
91 else if Obj
is TMalFalse
then
93 else if Obj
is TMalInt
then
94 pr_str
:= IntToStr((Obj
as TMalInt
).Val
)
95 else if Obj
is TMalSymbol
then
96 pr_str
:= (Obj
as TMalSymbol
).Val
97 else if Obj
is TMalAtom
then
99 pr_str((Obj
as TMalAtom
).Val
, print_readably
) +
101 else if Obj
is TMalFunc
then
103 Fn
:= (Obj
as TMalFunc
);
105 pr_str
:= '#<native function>'
107 pr_str
:= '(fn* ' + pr_str(Fn
.Params
,true) +
108 ' ' + pr_str(Fn
.Ast
,true) + ')'
111 pr_str
:= '#unknown';