ast_lower.adb (2215B)
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
package body AST_Lower is
procedure Print_IR (LL : in out Lowerer) is
Program : Unbounded_String;
Footer : Unbounded_String;
begin
Put_Line ("printing IR");
-- init --
Program := To_Unbounded_String ("export function w $main() {\n " & "@start\n ");
Footer := To_Unbounded_String ("\n}");
Put (To_String (Program));
Put_Line (To_String (Footer));
end;
function Init
(File_Name : File_Name_Ref; Source : Source_Ref; AST : Declaration_Vectors.Vector) return Lowerer is
begin
return Lowerer'(File_Name => File_Name, Source => Source, AST => AST, Module => null, Pos => 0);
end Init;
function Lower_Expr (LL : in out Lowerer; Expr : Expr) return IR_Value is
begin
LL.Pos := LL.Pos + 1;
return IR_Value'(Id => LL.Pos, Typ => IR_Void);
end;
function Lower_Call (LL : in out Lowerer; Call : Call_Expr) return IR_Instruction is
C : IR_Instruction;
begin
Fields := Lower_Arguments (LL, Call.Params);
C := IR_Instruction'(Kind => CALL, Call_Name => Call.Name, Call_Has_Dest => false, Call_Args => Fields);
end;
function Lower_Function (LL : in out Lowerer; Func : Func_Decl) return IR_Function is
F : IR_Function;
T : IR_Type;
begin
if Func.Return_Kind = "" then
-- TODO assess more typechecked types to IR Type.
T := IR_VOID;
else
T := IR_VOID;
end if;
for SS : Stmt of Func.Stmts loop
if SS.Kind = Stmt_Call_Kind then
Lower_Call (LL, SS.Call);
end if;
end loop;
return IR_function'(Name => Func.Name, Return_Type => T);
end Lower_Function;
procedure Lower (LL : in out Lowerer) is
Func : IR_Function :=
(Name => To_Unbounded_String ("main"), Instructions => Instructions_Vector.Empty_Vector);
begin
LL.Module := new IR_Module;
LL.Module.Functions.Append (Func);
for D : Decl of LL.AST loop
if D.Kind = Function_Kind then
Lower_Function (LL, Decl.Func);
end if;
end loop;
end;
end AST_Lower;