๐Ÿ“ฆ samueltardieu / aforth

๐Ÿ“„ forth-interpreter.ads ยท 266 lines
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266------------------------------------------------------------------------------
--                                                                          --
--                            AFORTH COMPONENTS                             --
--                                                                          --
--                    F O R T H . I N T E R P R E T E R                     --
--                                                                          --
--                                 S p e c                                  --
--                                                                          --
--          Copyright (C) 2006-2010 Samuel Tardieu <sam@rfc1149.net>        --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
--   The main repository for this software is located at:                   --
--       http://git.rfc1149.net/aforth.git                                  --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Containers.Vectors;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with Forth.Stacks;
with Forth.Types;            use Forth.Types;
with Interfaces;             use Interfaces;

package Forth.Interpreter is

   pragma Elaborate_Body;

   type Interpreter_Type is private;

   subtype IT is Interpreter_Type;
   --  Shortcut

   type Cell_Access is access all Cell;
   pragma No_Strict_Aliasing (Cell_Access);

   type Ada_Word_Access is access procedure (I : IT);

   function New_Interpreter
     (Memory_Size : Cell := 65536;
      Stack_Size  : Cell := 256)
     return IT;
   --  Memory size is in bytes, stack size is in cells. Both data and return
   --  stacks are bounded to avoid runaway memory exhaustion.

   procedure Free_Interpreter (I : in out IT);
   --  Reclaim the memory used by the interpreter. After this call, the
   --  interpreter cannot be used anymore.

   procedure Push (I : IT; X : Cell);
   procedure Push_Unsigned (I : IT; X : Unsigned_32);
   procedure Push_Unsigned_64 (I : IT; X : Unsigned_64);
   procedure Push_64 (I : IT; X : Integer_64);
   procedure Push (I : IT; B : Boolean);
   function Pop (I : IT) return Cell;
   function Pop_Unsigned (I : IT) return Unsigned_32;
   function Pop_64 (I : IT) return Integer_64;
   function Pop_Unsigned_64 (I : IT) return Unsigned_64;
   --  Shortcut operating on Data_Stack

   procedure Make_And_Remember_Variable
     (I             : IT;
      Name          : String;
      Var           : out Cell_Access;
      Size          : Cell := 4;
      Initial_Value : Cell := 0);

   procedure Make_And_Remember_Variable
     (I             : IT;
      Name          : String;
      Var           : out Cell;
      Size          : Cell := 4;
      Initial_Value : Cell := 0);

   function Fetch (I : IT; Addr : Cell) return Cell;
   function Cfetch (I : IT; Addr : Cell) return Cell;
   procedure Store (I : IT; Addr : Cell; Value : Cell);

   procedure Make_Variable
     (I             : IT;
      Name          : String;
      Size          : Cell := 4;
      Initial_Value : Cell := 0);

   procedure Register_Ada_Word
     (I         : IT;
      Name      : String;
      Word      : Ada_Word_Access;
      Immediate : Boolean := False);

   procedure Register_Constant
     (I     : IT;
      Name  : String;
      Value : Cell);

   procedure Include_File (I : IT; File_Name : String);
   --  This may raise Ada.IO_Exceptions.Name_Error if the file cannot be found,
   --  or Bye_Exception if the "BYE" word is used while reading the file.

   procedure Interpret_Line (I : IT; Line : String);

   --  Predefined Ada words
   procedure Again (I : IT);
   procedure Ahead (I : IT);
   procedure Align (I : IT);
   procedure Bye (I : IT);
   procedure Cfetch (I : IT);
   procedure Colon (I : IT);
   procedure Colon_Noname (I : IT);
   procedure Compile_Comma (I : IT);
   procedure Compile_Exit (I : IT);
   procedure Compile_Mode (I : IT);
   procedure Count (I : IT);
   procedure Cr (I : IT);
   procedure Cstore (I : IT);
   procedure D_Abs (I : IT);
   procedure D_Equal (I : IT);
   procedure D_Max (I : IT);
   procedure D_Min (I : IT);
   procedure D_Minus (I : IT);
   procedure D_Plus (I : IT);
   procedure D_Smaller (I : IT);
   procedure D_Two_Div (I : IT);
   procedure D_Two_Times (I : IT);
   procedure Depth (I : IT);
   procedure DivMod (I : IT);
   procedure Does (I : IT);
   procedure Drop (I : IT);
   procedure Dup (I : IT);
   procedure Emit (I : IT);
   procedure Equal (I : IT);
   procedure Evaluate (I : IT);
   procedure Execute (I : IT);
   procedure Fetch (I : IT);
   procedure Find (I : IT);
   procedure Fm_Slash_Mod (I : IT);
   procedure Forth_And (I : IT);
   procedure Forth_Begin (I : IT);
   procedure Forth_Do (I : IT);
   procedure Forth_If (I : IT);
   procedure Forth_Or (I : IT);
   procedure Forth_Then (I : IT);
   procedure Forth_While (I : IT);
   procedure Forth_Xor (I : IT);
   procedure From_R (I : IT);
   procedure Greaterequal (I : IT);
   procedure Include (I : IT);
   procedure Interpret (I : IT);
   procedure Interpret_Mode (I : IT);
   procedure J (I : IT);
   procedure Key (I : IT);
   procedure Leave (I : IT);
   procedure Literal (I : IT);
   procedure Lshift (I : IT);
   procedure MS (I : IT);
   procedure Mstar (I : IT);
   procedure Negative (I : IT);
   procedure Parse (I : IT);
   procedure Parse_Word (I : IT);
   procedure Pick (I : IT);
   procedure Plus (I : IT);
   procedure Plus_Loop (I : IT);
   procedure Postpone (I : IT);
   procedure Quit (I : IT);
   procedure R_At (I : IT);
   procedure Recurse (I : IT);
   procedure Refill (I : IT);
   procedure Repeat (I : IT);
   procedure Roll (I : IT);
   procedure Rshift (I : IT);
   procedure S_To_D (I : IT);
   procedure ScaleMod (I : IT);
   procedure See (I : IT);
   procedure Semicolon (I : IT);
   procedure Set_Immediate (I : IT);
   procedure Set_Inline (I : IT);
   procedure Skip_Blanks (I : IT);
   procedure Sm_Slash_Rem (I : IT);
   procedure Store (I : IT);
   procedure Swap (I : IT);
   procedure Tick (I : IT);
   procedure Times (I : IT);
   procedure To_Body (I : IT);
   procedure To_R (I : IT);
   procedure Two_Div (I : IT);
   procedure Two_Dup (I : IT);
   procedure Two_R_At (I : IT);
   procedure Two_To_R (I : IT);
   procedure U_Smaller (I : IT);
   procedure Um_Slash_Mod (I : IT);
   procedure Um_Star (I : IT);
   procedure Unloop (I : IT);
   procedure Unused (I : IT);
   procedure Word (I : IT);
   procedure Words (I : IT);

private

   use Forth.Stacks;

   type Action_Kind is (Ada_Word, Forth_Word, Number);

   type Action_Type (Kind : Action_Kind := Number) is record
      Immediate : Boolean;
      case Kind is
         when Ada_Word =>
            Ada_Proc   : Ada_Word_Access;
         when Forth_Word =>
            Forth_Proc : Cell;
            Inline     : Boolean := False;
         when Number =>
            Value      : Cell;
      end case;
   end record;

   subtype Natural_Cell is Cell range 1 .. Cell'Last;
   package Compilation_Buffers is
      new Ada.Containers.Vectors (Natural_Cell, Action_Type);

   type Dictionary_Entry is record
      Name   : Unbounded_String;
      Action : Action_Type;
   end record;

   package Dictionaries is
     new Ada.Containers.Vectors (Positive, Dictionary_Entry);

   type Byte_Array is array (Cell range <>) of aliased Unsigned_8;

   type Byte_Access is access all Unsigned_8;

   type Interpreter_Body (Last_Address : Cell) is record
      Data_Stack         : Stack_Type;
      Return_Stack       : Stack_Type;
      Compilation_Buffer : Compilation_Buffers.Vector;
      Dict               : Dictionaries.Vector;
      Memory             : Byte_Array (0 .. Last_Address);
      Here               : Cell_Access;
      Base               : Cell_Access;
      TIB                : Cell;
      TIB_Count          : Cell_Access;
      IN_Ptr             : Cell_Access;
      State              : Cell_Access;
      Current_Name       : Unbounded_String;
      Current_Action     : Action_Type (Forth_Word);
      Current_IP         : Cell := -1;
      Use_RL             : Boolean := True;
   end record;

   type Interpreter_Type is access Interpreter_Body;

end Forth.Interpreter;