2021-10-30 05:32:16 +00:00
|
|
|
with H3.Runes;
|
2021-10-30 01:57:19 +00:00
|
|
|
with H3.Strings;
|
2021-11-16 13:34:32 +00:00
|
|
|
with Ada.Finalization;
|
2021-11-07 17:32:50 +00:00
|
|
|
with Ada.Text_IO;
|
2021-10-30 01:57:19 +00:00
|
|
|
|
|
|
|
generic
|
|
|
|
type Rune_Type is (<>);
|
|
|
|
package H3.Compilers is
|
2021-10-30 05:32:16 +00:00
|
|
|
package R is new H3.Runes(Rune_Type);
|
2021-10-30 01:57:19 +00:00
|
|
|
package S is new H3.Strings(Rune_Type);
|
|
|
|
|
|
|
|
Syntax_Error: exception;
|
|
|
|
|
2021-11-16 13:34:32 +00:00
|
|
|
--type Compiler is tagged limited private;
|
|
|
|
type Compiler is new Ada.Finalization.Limited_Controlled with private;
|
2021-10-30 01:57:19 +00:00
|
|
|
|
|
|
|
procedure Feed (C: in out Compiler; Data: in S.Rune_Array);
|
|
|
|
procedure End_Feed (C: in out Compiler);
|
|
|
|
|
2021-11-16 13:34:32 +00:00
|
|
|
overriding procedure Initialize (C: in out Compiler);
|
|
|
|
overriding procedure Finalize (C: in out Compiler);
|
|
|
|
|
2021-10-30 01:57:19 +00:00
|
|
|
private
|
|
|
|
type Lexer_State is (
|
|
|
|
LX_START,
|
2021-11-12 23:31:25 +00:00
|
|
|
|
2021-10-30 01:57:19 +00:00
|
|
|
LX_COMMENT,
|
2021-11-12 23:31:25 +00:00
|
|
|
LX_CSTR,
|
2021-11-02 00:31:30 +00:00
|
|
|
LX_DIRECTIVE,
|
2021-10-30 01:57:19 +00:00
|
|
|
LX_IDENT,
|
|
|
|
LX_NUMBER,
|
2021-11-16 13:34:32 +00:00
|
|
|
LX_OP_DIV,
|
2021-10-30 01:57:19 +00:00
|
|
|
LX_OP_GREATER,
|
2021-11-16 13:34:32 +00:00
|
|
|
LX_OP_LESS,
|
|
|
|
LX_OP_MINUS,
|
|
|
|
LX_OP_MUL,
|
|
|
|
LX_OP_PLUS
|
2021-10-30 01:57:19 +00:00
|
|
|
);
|
|
|
|
type Lexer is record
|
|
|
|
State: Lexer_State := LX_START;
|
|
|
|
end record;
|
|
|
|
|
|
|
|
type Token_Id is (
|
|
|
|
TK_BSTR,
|
|
|
|
TK_BYTE,
|
|
|
|
TK_CHAR,
|
|
|
|
TK_CSTR,
|
2021-11-02 00:31:30 +00:00
|
|
|
TK_DIRECTIVE,
|
2021-11-16 13:34:32 +00:00
|
|
|
TK_DIV,
|
|
|
|
TK_DIVDIV,
|
2021-10-30 01:57:19 +00:00
|
|
|
TK_EOF,
|
|
|
|
TK_EOL,
|
|
|
|
TK_IDENT,
|
|
|
|
TK_GE,
|
|
|
|
TK_GT,
|
|
|
|
TK_LE,
|
|
|
|
TK_LT,
|
2021-11-16 13:34:32 +00:00
|
|
|
TK_MINUS,
|
|
|
|
TK_MINUSMINUS,
|
|
|
|
TK_MUL,
|
|
|
|
TK_MULMUL,
|
|
|
|
TK_PLUS,
|
|
|
|
TK_PLUSPLUS,
|
2021-10-30 01:57:19 +00:00
|
|
|
TK_SEMICOLON
|
|
|
|
);
|
|
|
|
type Token is record
|
|
|
|
Id: Token_Id := TK_EOF;
|
|
|
|
Buf: S.Elastic_String;
|
|
|
|
end record;
|
|
|
|
|
2021-11-16 13:34:32 +00:00
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Parse_State_Code is (
|
2021-11-07 17:32:50 +00:00
|
|
|
PS_START,
|
2021-11-14 15:07:41 +00:00
|
|
|
PS_INCLUDE_TARGET,
|
|
|
|
PS_INCLUDE_TERMINATOR
|
2021-11-07 17:32:50 +00:00
|
|
|
);
|
2021-11-16 13:34:32 +00:00
|
|
|
|
|
|
|
type Parse_State is record
|
|
|
|
Current: Parse_State_Code := PS_START;
|
|
|
|
end record;
|
|
|
|
|
|
|
|
type Parse_State_Array is array(System_Index range<>) of Parse_State;
|
|
|
|
|
|
|
|
type Parse_State_Stack(Capa: System_Index) is record
|
|
|
|
States: Parse_State_Array(System_Index'First .. Capa);
|
|
|
|
Top: System_Size := System_Size'First; -- 0
|
2021-11-14 15:07:41 +00:00
|
|
|
end record;
|
|
|
|
|
2021-11-16 13:34:32 +00:00
|
|
|
-- ------------------------------------------------------------------
|
|
|
|
|
2021-11-14 15:07:41 +00:00
|
|
|
type Stream is record
|
|
|
|
Handle: Ada.Text_IO.File_Type;
|
2021-11-16 13:34:32 +00:00
|
|
|
Prs_Level: System_Index;
|
2021-11-14 15:07:41 +00:00
|
|
|
end record;
|
|
|
|
|
|
|
|
type Stream_Array is array(System_Index range <>) of Stream;
|
2021-11-16 13:34:32 +00:00
|
|
|
|
2021-11-14 15:07:41 +00:00
|
|
|
type Include_Stack(Capa: System_Index) is record
|
|
|
|
Streams: Stream_Array(System_Index'First .. Capa);
|
2021-11-16 13:34:32 +00:00
|
|
|
Top: System_Size := System_Size'First; -- 0
|
2021-10-30 01:57:19 +00:00
|
|
|
end record;
|
2021-11-16 13:34:32 +00:00
|
|
|
-- ------------------------------------------------------------------
|
2021-10-30 01:57:19 +00:00
|
|
|
|
2021-11-16 13:34:32 +00:00
|
|
|
--type Compiler is tagged limited record
|
|
|
|
type Compiler is new Ada.Finalization.Limited_Controlled with record
|
2021-10-30 01:57:19 +00:00
|
|
|
Lx: Lexer;
|
|
|
|
Tk: Token;
|
2021-11-16 13:34:32 +00:00
|
|
|
Prs: Parse_State_Stack(128); -- TODO: make this dynamic. single access type. dynamic allocation
|
|
|
|
Inc: Include_Stack(32); -- TODO: make this dynamic. single access type. dynamic allocation
|
2021-10-30 01:57:19 +00:00
|
|
|
end record;
|
2021-11-16 13:34:32 +00:00
|
|
|
|
2021-10-30 05:32:16 +00:00
|
|
|
end H3.Compilers;
|