nightshade

The nightshade programming language, compiler and tools (WIP)
Log | Files | Refs | README

commit e32c465c8daaf0a54215c27841fa74b1d84b803c
parent 94dd31c870688255f5edf62e64ba8b0434eb6d0e
Author: citbl <citbl@citbl.org>
Date:   Mon, 22 Jun 2026 21:15:58 +1000

refac and outputing binary

Diffstat:
Msrc/ast_lower.adb | 2+-
Msrc/ast_scoper.adb | 2+-
Msrc/gen.adb | 18++++++++++++++++++
Msrc/nightshade.adb | 52++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/parser.adb | 2+-
Msrc/types.ads | 4++--
Msrc/utils.adb | 2+-
Atmp/.gitkeep | 0
8 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/src/ast_lower.adb b/src/ast_lower.adb @@ -50,7 +50,7 @@ package body AST_Lower is end if; for SS : Stmt of Func.Stmts loop - if SS.Kind = Stmt_Call then + if SS.Kind = Stmt_Call_Kind then Lower_Call(LL, SS.Call); end if; end loop; diff --git a/src/ast_scoper.adb b/src/ast_scoper.adb @@ -151,7 +151,7 @@ package body AST_Scoper is New_Scope_From_Scope (S); for Statement of Fn.Stmts loop - if Statement.Kind = Stmt_Call then + if Statement.Kind = Stmt_Call_Kind then Put_Line ("scoping call"); Scope_Call (S, Statement.Call); Statement.Scope := S.Scope; diff --git a/src/gen.adb b/src/gen.adb @@ -8,6 +8,13 @@ package body Gen is return Generator'(AST => AST, Globals => To_Unbounded_String (""), Output => To_Unbounded_String ("")); end; + procedure Gen_Call (GG : in out Generator; Stmt : in Call_Expr) 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; + end; + ------------------ -- gen function -- ------------------ @@ -16,6 +23,17 @@ package body Gen is begin GG.Output := GG.Output & "export function w $" & Func.Name & "() {" & ASCII.LF; GG.Output := GG.Output & "@start" & ASCII.LF; + + 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; GG.Output := GG.Output & "}" & ASCII.LF; end; diff --git a/src/nightshade.adb b/src/nightshade.adb @@ -1,5 +1,7 @@ -with Ada.Text_IO; use Ada.Text_IO; -with Ada.Command_Line; use Ada.Command_Line; +with Ada.Environment_Variables; +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Command_Line; use Ada.Command_Line; +with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Utils; with Types; with Lexer; @@ -7,6 +9,7 @@ with Parser; with AST_Scoper; with AST_Typer; with Gen; +with GNAT.OS_Lib; use GNAT.OS_Lib; procedure Nightshade is begin @@ -43,8 +46,53 @@ begin declare Generator : Gen.Generator := Gen.Init (P.AST); + begin Gen.Gen_IR (Generator); + + declare + F : File_Type; + QBE_Args : Argument_List := + (1 => new String'("-o"), 2 => new String'("tmp/out.s"), 3 => new String'("tmp/main.ssa")); + CC_Args : Argument_List := + (1 => new String'("tmp/out.s")); + Status : Integer; + Success : Boolean; + begin + Create (F, Out_File, "tmp/main.ssa"); + Put_Line (F, To_String (Generator.Globals)); + Put_Line (F, To_String (Generator.Output)); + Close (F); + -- Put_Line ("PATH = " & Ada.Environment_Variables.Value ("PATH")); + + Spawn + (Program_Name => "/opt/homebrew/bin/qbe", + Args => QBE_Args, + Output_File => "tmp/qbe.log", + Success => Success, + Return_Code => Status, + Err_To_Out => True); + + if Status /= 0 then + Put_Line ("QBE Compiling Failed: " & Status'Image); + Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure); + return; + end if; + + Spawn + (Program_Name => "/usr/bin/cc", + Args => CC_Args, + Output_File => "tmp/cc.log", + Success => Success, + Return_Code => Status, + Err_To_Out => True); + + if Status /= 0 then + Put_Line ("CC Compiling Failed: " & Status'Image); + Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure); + return; + end if; + end; end; end; end; diff --git a/src/parser.adb b/src/parser.adb @@ -144,7 +144,7 @@ package body Parser is T := Match (P, R_Paren); T := Match (P, Separator); CS := Call_Expr'(Name => Name, Params => Expressions, Entity_Type => null); - return Stmt'(Kind => Stmt_Call, Call => CS, Scope => null); + return Stmt'(Kind => Stmt_Call_Kind, Call => CS, Scope => null); end; ---------------- diff --git a/src/types.ads b/src/types.ads @@ -63,7 +63,7 @@ package Types is package Expr_Vectors is new Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Expr_Ref); - type Stmt_Kind is (Stmt_Call); + type Stmt_Kind is (Stmt_Call_Kind); type Call_Expr is record Name : Unbounded_String; @@ -74,7 +74,7 @@ package Types is type Stmt (Kind : Stmt_Kind) is record Scope : AST_Scope_Ref; case Kind is - when Stmt_Call => + when Stmt_Call_Kind => Call : Call_Expr; -- when Stmt_If => diff --git a/src/utils.adb b/src/utils.adb @@ -33,7 +33,7 @@ package body Utils is procedure Print_Statement (S : in Stmt; N : Natural) is begin Put (N * " "); - if S.Kind = Stmt_Call then + if S.Kind = Stmt_Call_Kind then Put ("Call: " & To_String (S.Call.Name) & "() with Params: "); for P of S.Call.Params loop if P.Kind = String_Literal then diff --git a/tmp/.gitkeep b/tmp/.gitkeep