hcl/lib2/h3-compilers.adb

192 lines
5.6 KiB
Ada
Raw Permalink Normal View History

2021-11-07 17:32:50 +00:00
with H3.Utf8;
2021-10-30 01:57:19 +00:00
package body H3.Compilers is
2021-11-07 17:32:50 +00:00
type Char_Array is array(System_Index range<>) of Standard.Character;
package Utf8 is new H3.Utf8(Standard.Character, S.Rune, Char_Array, S.Rune_Array);
LB_EOF: constant S.Rune_Array := (R.V.Left_Arrow,R.V.UC_E,R.V.UC_O,R.V.UC_F,R.V.Right_Arrow); -- <EOF>
2021-11-28 17:00:17 +00:00
LB_EOL: constant S.Rune_Array := (R.V.Left_Arrow,R.V.UC_E,R.V.UC_O,R.V.UC_L,R.V.Right_Arrow); -- <EOL>
2021-11-07 17:32:50 +00:00
LB_XINCLUDE: constant S.Rune_Array := (R.V.Number_Sign,R.V.LC_I,R.V.LC_N,R.V.LC_C,R.V.LC_L,R.V.LC_U,R.V.LC_D,R.V.LC_E); -- #include
2021-12-01 17:06:30 +00:00
LB_CLASS: constant S.Rune_Array := (R.V.LC_C,R.V.LC_L,R.V.LC_A,R.V.LC_S,R.V.LC_S); -- class
LB_FUN: constant S.Rune_Array := (R.V.LC_F,R.V.LC_U,R.V.LC_N); -- fun
LB_END: constant S.Rune_Array := (R.V.LC_E,R.V.LC_N,R.V.LC_D); -- end
LB_IF: constant S.Rune_Array := (R.V.LC_I,R.V.LC_F); -- if
LB_ELIF: constant S.Rune_Array := (R.V.LC_E,R.V.LC_L,R.V.LC_I,R.V.LC_F); -- elif
LB_ELSE: constant S.Rune_Array := (R.V.LC_E,R.V.LC_L,R.V.LC_S,R.V.LC_E); -- else
LB_WHILE: constant S.Rune_Array := (R.V.LC_W,R.V.LC_H,R.V.LC_I,R.V.LC_L,R.V.LC_E); -- while
LB_BREAK: constant S.Rune_Array := (R.V.LC_B,R.V.LC_R,R.V.LC_E,R.V.LC_A,R.V.LC_K); -- break
LB_CONTINUE: constant S.Rune_Array := (R.V.LC_C,R.V.LC_O,R.V.LC_N,R.V.LC_T,R.V.LC_I,R.V.LC_N,R.V.LC_U,R.V.LC_E); -- continue
LB_TRY: constant S.Rune_Array := (R.V.LC_T,R.V.LC_R,R.V.LC_Y); -- try
LB_CATCH: constant S.Rune_Array := (R.V.LC_C,R.V.LC_A,R.V.LC_T,R.V.LC_T,R.V.LC_H); -- catch
LB_RAISE: constant S.Rune_Array := (R.V.LC_R,R.V.LC_A,R.V.LC_I,R.V.LC_S,R.V.LC_E); -- raise
2021-11-16 13:34:32 +00:00
procedure Dump_Token (Tk: in Token) is
begin
Ada.Text_IO.Put (Tk.Id'Img);
Ada.Text_IO.Put (": ");
Ada.Text_IO.Put_Line (Standard.String(Utf8.From_Unicode_String(Tk.Buf.To_Rune_Array)));
end Dump_Token;
procedure Dump_Rune (Code: in R.Code) is
begin
if R.Is_Eof(Code) then
Ada.Text_IO.Put_Line ("EOF");
else
Ada.Text_IO.Put_Line (R.To_Rune(Code)'Img);
end if;
end Dump_Rune;
2022-08-23 10:13:02 +00:00
function Is_Line_Break(Code: in R.Code) return Boolean is
2021-11-14 15:07:41 +00:00
begin
2022-08-23 10:13:02 +00:00
return R.Is_Rune(Code, R.V.LF); -- TODO: consider different line end convention
end Is_Line_Break;
2021-10-30 01:57:19 +00:00
2021-11-28 17:00:17 +00:00
function Is_Ident_Starter(Code: in R.Code) return Boolean is
begin
2021-12-01 17:06:30 +00:00
return R.Is_Alnum(Code) or else
R.Is_Rune(Code, R.V.Underline) or else
R.Is_Rune(Code, R.V.Minus_Sign);
2021-11-28 17:00:17 +00:00
end Is_Ident_Starter;
function Is_Ident_Char(Code: in R.Code) return Boolean is
begin
2021-12-01 17:06:30 +00:00
return Is_Ident_Starter(Code); -- or else R.Is_Rune(Code, R.V.Underline); -- or else R.Is_Rune(C, ...);
2021-11-28 17:00:17 +00:00
end Is_Ident_Char;
2022-08-23 10:13:02 +00:00
-- -------------------------------------------------------------------
2021-12-01 17:06:30 +00:00
2022-08-23 10:13:02 +00:00
package body Feeder is
procedure Switch_To_Start (C: in out Compiler) is
begin
C.F.Lx.Data := Lex_Data'(State => LX_START);
end Switch_To_Start;
2021-11-28 17:00:17 +00:00
2022-08-23 10:13:02 +00:00
procedure Start (C: in out Compiler; Code: in R.Code; Consumed: out Boolean) is
begin
null;
end Start;
2021-11-28 17:00:17 +00:00
2022-08-23 10:13:02 +00:00
procedure Comment (C: in out Compiler; Code: in R.Code; Consumed: out Boolean) is
begin
if Is_Line_Break(Code) then
Switch_To_Start (C);
end if;
end Comment;
2021-10-30 01:57:19 +00:00
2022-08-23 10:13:02 +00:00
procedure Delim_Token (C: in out Compiler; Code: in R.Code; Consumed: out Boolean) is
begin
null;
end Delim_Token;
2021-11-12 23:31:25 +00:00
2022-08-23 10:13:02 +00:00
procedure Hmarked_Token (C: in out Compiler; Code: in R.Code; Consumed: out Boolean) is
begin
null;
end Hmarked_Token;
2021-11-16 13:34:32 +00:00
2022-08-23 10:13:02 +00:00
procedure Update_Location (C: in out Compiler; Code: in R.Code) is
begin
if Is_Line_Break(Code) then
C.F.Lx.Loc.Line := C.F.Lx.Loc.Line + 1;
C.F.Lx.Loc.Colm := 1;
else
C.F.Lx.Loc.Colm := C.F.Lx.Loc.Colm + 1;
end if;
end Update_Location;
2021-11-16 13:34:32 +00:00
2022-08-23 10:13:02 +00:00
procedure Begin_Include (C: in out Compiler) is
begin
null;
end Begin_Include;
2021-11-16 13:34:32 +00:00
2022-08-23 10:13:02 +00:00
procedure Feed_From_Includee (C: in out Compiler) is
begin
null;
end Feed_From_Includee;
end Feeder;
2021-11-16 13:34:32 +00:00
2021-11-12 23:31:25 +00:00
2022-08-23 10:13:02 +00:00
-- -------------------------------------------------------------------
2021-11-12 23:31:25 +00:00
2022-08-23 10:13:02 +00:00
procedure Feed_Char_Code (C: in out Compiler; Code: in R.Code; Consumed: out Boolean) is
begin
case C.F.Lx.Data.State is
when Feeder.LX_START =>
Feeder.Start(C, Code, Consumed);
when Feeder.LX_COMMENT =>
Feeder.Comment(C, Code, Consumed);
when Feeder.LX_DT =>
Feeder.Delim_Token(C, Code, Consumed);
when Feeder.LX_HC =>
Feeder.Hmarked_Token(C, Code, Consumed);
2021-10-30 01:57:19 +00:00
end case;
2021-11-28 17:00:17 +00:00
2022-08-23 10:13:02 +00:00
--raise Internal_Error with "internal error - unknown flx state";
end Feed_Char_Code;
2021-11-14 15:07:41 +00:00
2022-08-23 10:13:02 +00:00
procedure Feed (C: in out Compiler; Data: in S.Rune_Array) is
Consumed: Boolean;
CC: R.Code;
I: System_Index := Data'First;
begin
while I <= Data'Last loop
CC := R.To_Code(Data(I));
Feed_Char_Code (C, CC, consumed);
if Consumed then
Feeder.Update_Location (C, CC);
I := I + 1;
2021-11-14 15:07:41 +00:00
end if;
2022-08-23 10:13:02 +00:00
if C.F.Rd.Do_Include_File then
Feeder.Begin_Include (C);
C.F.Rd.Do_Include_File := False;
2021-11-07 17:32:50 +00:00
end if;
2022-08-23 10:13:02 +00:00
--if C->Cur then
-- Feeder.Feed_From_Includee (C);
--end if;
2021-10-30 01:57:19 +00:00
end loop;
end Feed;
procedure End_Feed (C: in out Compiler) is
2022-08-23 10:13:02 +00:00
Consumed: Boolean;
2021-10-30 01:57:19 +00:00
begin
2022-08-23 10:13:02 +00:00
begin
loop
Feed_Char_Code(C, R.P.EOF, Consumed);
exit when Consumed;
end loop;
exception
when others =>
--if C.Feed.Rd.Level <= 0 and then C.Get_Error_Number = HCL_ERR_SYNTAX and then C.Get_Syntax_Error_Number = HCL_SYNERR_EOF then
-- null; -- normal EOF
--else
raise;
--end if;
end;
2021-10-30 01:57:19 +00:00
end End_Feed;
2021-11-16 13:34:32 +00:00
-- -------------------------------------------------------------------
procedure Initialize (C: in out Compiler) is
begin
2022-08-23 10:13:02 +00:00
--Push_Parse_State (C, PS_START);
2021-12-11 15:57:06 +00:00
declare
X: H3.Trees.Tree;
begin
H3.Trees.New_Node (X);
end;
2021-11-16 13:34:32 +00:00
end Initialize;
procedure Finalize (C: in out Compiler) is
begin
2022-08-23 10:13:02 +00:00
--while C.Inc.Top > 0 loop
-- Pop_Inclusion (C, False);
--end loop;
--while C.Prs.Top > 0 loop
-- Pop_Parse_State (C);
--end loop;
null;
2021-11-16 13:34:32 +00:00
end Finalize;
2022-08-23 10:13:02 +00:00
2021-10-30 01:57:19 +00:00
end H3.Compilers;