commit 3b65c14fc4876b58cab2405175f34e1047f49ede
parent 3863be81c14395a5bca359fbe1b49cfeb070fb1e
Author: citbl <citbl@citbl.org>
Date: Mon, 15 Jun 2026 22:57:19 +1000
lowering wip
Diffstat:
4 files changed, 86 insertions(+), 9 deletions(-)
diff --git a/src/ast_lower.adb b/src/ast_lower.adb
@@ -1,10 +1,75 @@
+with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
+with Ada.Text_IO; use Ada.Text_IO;
package body AST_Lower is
- function Init
+ 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);
+ return Lowerer'(File_Name => File_Name, Source => Source, AST => AST, Module => null);
end Init;
+ function Lower_Expr(LL : in out Lowerer; Expr : Expr) return IR_Value is
+ begin
+
+ 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 in Func.Stmts loop
+ if SS.Kind = Stmt_Call 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;
diff --git a/src/ast_lower.ads b/src/ast_lower.ads
@@ -4,12 +4,6 @@ with Types; use Types;
package AST_Lower is
- type Lowerer is record
- File_Name : File_Name_Ref;
- Source : Source_Ref;
- AST : Declaration_Vectors.Vector;
- end record;
-
type IR_Type is (IR_VOID, IR_STR, IR_I64, IR_BOOL);
type IR_Value is record
@@ -44,6 +38,7 @@ package AST_Lower is
type IR_Function is record
Name : Unbounded_String;
Instructions : Instructions_Vector.Vector;
+ Return_Type : IR_Type;
end record;
package Functions_Vector is new
@@ -53,7 +48,18 @@ package AST_Lower is
Functions : Functions_Vector.Vector;
end record;
+ type IR_Module_Ref is access IR_Module;
+
+ type Lowerer is record
+ File_Name : File_Name_Ref;
+ Source : Source_Ref;
+ AST : Declaration_Vectors.Vector;
+ Module : IR_Module_Ref;
+ end record;
+
function Init
(File_Name : File_Name_Ref; Source : Source_Ref; AST : Declaration_Vectors.Vector) return Lowerer;
+ procedure Lower (LL : in out Lowerer);
+ procedure Print_IR (LL : in out Lowerer);
end AST_Lower;
diff --git a/src/nightshade.adb b/src/nightshade.adb
@@ -40,6 +40,12 @@ begin
TypedAST : AST_Typer.Typer := AST_Typer.Init (P.AST);
begin
AST_Typer.Type_Check (TypedAST);
+
+ declare
+ Lowered : AST_Lower.Lowerer := AST_Lower.Init(File_Name_Ref, Src_Ref, P.AST);
+ begin
+ AST_Lower.Print_IR(Lowered);
+ end;
end;
end;
end;
diff --git a/src/types.ads b/src/types.ads
@@ -110,7 +110,7 @@ package Types is
type Func_Decl is record
Name : Unbounded_String;
Params : Param_Vectors.Vector;
- Return_Kind : Unbounded_String;
+ Return_Kind : Unbounded_String; -- TODO convert return type to a type we can type check and spit IR for
Stmts : Stmt_Vectors.Vector;
Public : Boolean;
File_Name : File_Name_Ref;