Another Ada implementation.
[jackhill/mal.git] / ada2 / step1_read_print.adb
CommitLineData
cbbb51b4
NB
1with Ada.Exceptions;
2with Ada.Strings.Unbounded;
3with Ada.Text_IO.Unbounded_IO;
4with Interfaces.C.Strings; use type Interfaces.C.Strings.chars_ptr;
5with Printer;
6with Reader;
7with Types;
8
9procedure Step1_Read_Print is
10
11 function Read (Source : in String) return Types.Mal_Type
12 renames Reader.Read_Str;
13
14 function Eval (Ast : in Types.Mal_Type) return Types.Mal_Type
15 is (Ast);
16
17 function Print (Ast : in Types.Mal_Type;
18 Print_Readably : in Boolean := True)
19 return Ada.Strings.Unbounded.Unbounded_String
20 renames Printer.Pr_Str;
21
22 function Rep (Source : in String)
23 return Ada.Strings.Unbounded.Unbounded_String
24 is (Print (Eval (Read (Source))))
25 with Inline;
26
27 procedure Interactive_Loop
28 with Inline;
29
30 ----------------------------------------------------------------------
31
32 procedure Interactive_Loop
33 is
34
35 function Readline (Prompt : in Interfaces.C.char_array)
36 return Interfaces.C.Strings.chars_ptr
37 with Import, Convention => C, External_Name => "readline";
38
39 procedure Add_History (Line : in Interfaces.C.Strings.chars_ptr)
40 with Import, Convention => C, External_Name => "add_history";
41
42 procedure Free (Line : in Interfaces.C.Strings.chars_ptr)
43 with Import, Convention => C, External_Name => "free";
44
45 Prompt : constant Interfaces.C.char_array
46 := Interfaces.C.To_C ("user> ");
47 C_Line : Interfaces.C.Strings.chars_ptr;
48 begin
49 loop
50 C_Line := Readline (Prompt);
51 exit when C_Line = Interfaces.C.Strings.Null_Ptr;
52 declare
53 Line : constant String := Interfaces.C.Strings.Value (C_Line);
54 begin
55 if Line /= "" then
56 Add_History (C_Line);
57 end if;
58 Free (C_Line);
59 Ada.Text_IO.Unbounded_IO.Put_Line (Rep (Line));
60 exception
61 when Reader.Empty_Source =>
62 null;
63 when E : others =>
64 Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
65 -- but go on proceeding.
66 end;
67 end loop;
68 Ada.Text_IO.New_Line;
69 end Interactive_Loop;
70
71 ----------------------------------------------------------------------
72
73begin
74 Interactive_Loop;
75end Step1_Read_Print;