commit 580c309488f5470631b515947e8311815ad42920
parent e32c465c8daaf0a54215c27841fa74b1d84b803c
Author: citbl <citbl@citbl.org>
Date: Wed, 24 Jun 2026 22:41:15 +1000
gen actual string
Diffstat:
4 files changed, 48 insertions(+), 10 deletions(-)
diff --git a/makefile b/makefile
@@ -29,9 +29,10 @@ default:
$(BUILD_COMMAND)
clean:
- rm -rf ./bin/
+ rm -rf ./bin/*
rm -rf *.o *.ali b~*
mkdir -p ./bin/
+ rm -rf ./tmp/*
pedantic: clean
gnatmake -gnat2022 -gnatwa -o bin/nightshade src/nightshade.adb
diff --git a/src/gen.adb b/src/gen.adb
@@ -1,18 +1,53 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
+with Ada.Strings.Fixed; use Ada.Strings.Fixed;
package body Gen is
function Init (AST : Declaration_Vectors.Vector) return Generator is
begin
- return Generator'(AST => AST, Globals => To_Unbounded_String (""), Output => To_Unbounded_String (""));
+ return
+ Generator'
+ (AST => AST, Globals => To_Unbounded_String (""), Output => To_Unbounded_String (""), Indent => 0);
end;
- procedure Gen_Call (GG : in out Generator; Stmt : in Call_Expr) is
+ -- gen unident --
+ procedure Gen_Unindent (GG : in out Generator) is
begin
- GG.Globals := GG.GLobals & "data $str = { b ""hello world"", b 0 }" & ASCII.LF;
- GG.Output := GG.Output & " %r =w call $puts(l $str)" & ASCII.LF;
+ GG.Indent := GG.Indent - 4;
+ end;
+
+ -- gen ident --
+
+ procedure Gen_Indent (GG : in out Generator) is
+ begin
+ GG.Indent := GG.Indent + 4;
+ end;
+
+ procedure Put_Indent (GG : in out Generator) is
+ begin
+ GG.Output := GG.Output & To_Unbounded_String(GG.Indent * ' ');
+ end;
+
+ --------------
+ -- gen call --
+ --------------
+
+ procedure Gen_Call (GG : in out Generator; Call : in Call_Expr) is
+
+ begin
+ -- if Call.Params.Length /= 1 then
+ -- raise Program_error with "cannot handle calls of more or less than 1 param yet";
+ -- end if;
+ declare
+ Param : constant Expr_Ref := Call.Params.Element (Call.Params.First_Index);
+ Content : Unbounded_String := Param.Lit_Str;
+ begin
+ GG.Globals := GG.GLobals & "data $str = { b " & (Content) & ", b 0 }" & ASCII.LF;
+ Put_Indent(GG);
+ GG.Output := GG.Output & "%r =w call $puts(l $str)" & ASCII.LF;
+ end;
end;
------------------
@@ -24,16 +59,18 @@ package body Gen is
GG.Output := GG.Output & "export function w $" & Func.Name & "() {" & ASCII.LF;
GG.Output := GG.Output & "@start" & ASCII.LF;
- for Stmt of Func.Stmts loop
+ Gen_Indent (GG);
+ for Stmt of Func.Stmts loop
if Stmt.Kind = Stmt_Call_Kind then
-
Gen_Call (GG, Stmt.Call);
-
end if;
end loop;
- GG.Output := GG.Output & " ret 0" & ASCII.LF;
+ Put_Indent(GG);
+ GG.Output := GG.Output & "ret 0" & ASCII.LF;
+
+ Gen_Unindent (GG);
GG.Output := GG.Output & "}" & ASCII.LF;
end;
diff --git a/src/gen.ads b/src/gen.ads
@@ -7,6 +7,7 @@ package Gen is
AST : Declaration_Vectors.Vector;
Globals : Unbounded_String;
Output : Unbounded_String;
+ Indent : Natural;
end record;
function Init (AST : Declaration_vectors.vector) return Generator;
diff --git a/src/types.ads b/src/types.ads
@@ -100,7 +100,6 @@ package Types is
when String_Literal =>
Lit_Str : Unbounded_String;
-
File_Name : File_Name_Ref;
Line : Positive;
Col : Positive;