nightshade

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

commit 165ab1e1d33e678e701fb26fad9d0e90c659faf8
parent c5b32c931be8466429af81c16effea3ecea66ba2
Author: citbl <citbl@citbl.org>
Date:   Sun, 31 May 2026 19:20:14 +1000

make number

Diffstat:
Msrc/lexer.adb | 82++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 54 insertions(+), 28 deletions(-)

diff --git a/src/lexer.adb b/src/lexer.adb @@ -75,12 +75,48 @@ package body Lexer is end loop; end Skip_Spaces_Comments; + -- make number + + function Make_Number (L : in out Lexer) return Token is + T : Token; + Start : Natural; + Is_Float : Boolean := false; + begin + Start := L.Pos; + + if Peek(L) = '-' then + Nudge (L); + end if; + + while Is_Digit (Peek(L)) loop + Nudge (L); + end loop; + + if Peek(L) = '.' and Is_Digit (Peek2(L)) then + Is_Float := true; + Nudge (L); + while Is_Digit (Peek(L)) loop + Nudge (L); + end loop; + end if; + T := + (Kind => Str_Literal, + Lexeme => To_Unbounded_String (L.Source (Start .. L.Pos)), + Line => L.Line, + Col => L.Col + ); + + return T; + end Make_Number; + + -- make string + function Make_String (L : in out Lexer) return Token is C : Character; T : Token; Start : Natural; begin - Start := L.Pos; + Start := L.Pos; Nudge (L); -- advance `"` while true loop @@ -97,12 +133,13 @@ package body Lexer is T := (Kind => Str_Literal, Lexeme => To_Unbounded_String (L.Source (Start .. L.Pos)), - Line => 1, - Col => 1); + Line => L.Line, + Col => L.Col); Nudge (L); -- advance `"` return T; end Make_String; + -- next token function Next_Token (L : in out Lexer) return Token is @@ -110,6 +147,7 @@ package body Lexer is TK : Token_Kind; Start : Natural; C : Character; + C2 : Character; begin Skip_Spaces_Comments (L); @@ -121,11 +159,23 @@ package body Lexer is end if; Start := L.Pos; C := Peek (L); + C2 := Peek2 (L); + + if C = '-' and Is_Digit (C2) then + T := Make_Number (L); + return T; + end if; + + if Is_Digit (C2) then + T := Make_Number (L); + return T; + end if; + if C = '"' then - -- make string T := Make_String (L); return T; end if; + T := (Kind => TK, Lexeme => To_Unbounded_String (""), Line => 1, Col => 1); Nudge (L); @@ -149,30 +199,6 @@ package body Lexer is exit when Tok.Kind = EOF; end loop; - -- Put_Line (Natural'Image (Length (L.Source))); - - -- while I <= SRC'Last loop - -- if I < SRC'Last then - -- I2 := I + 1; - -- else - -- I2 := I; - -- end if; - - -- C := SRC (I); - - -- if Is_Space (C) or Is_Line_Terminator (C) then - -- if C = ASCII.LF then - -- Put_Line ("found return at line:" & I'Image); - -- end if; - -- end if; - -- if Is_Letter (C) then - - -- null; - -- end if; - - -- I := I + 1; - -- end loop; - end Lex; end Lexer;