nightshade

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

commit f0c9a9362930f4080d65cdd40729a254358e413a
parent 751f1436fcd76eb98b216ff1948bc9ee17e98b31
Author: citbl <citbl@citbl.org>
Date:   Sun, 31 May 2026 22:32:42 +1000

fixes

Diffstat:
Msrc/lexer.adb | 66++++++++++++++++++++++++++++++++++++++++++------------------------
Mtest.mty | 1-
2 files changed, 42 insertions(+), 25 deletions(-)

diff --git a/src/lexer.adb b/src/lexer.adb @@ -21,13 +21,16 @@ package body Lexer is function Peek (L : in Lexer) return Character is begin + if L.Pos > L.Source'Length then + return ASCII.NUL; + end if; return L.Source (L.Pos); end; function Peek2 (L : in Lexer) return Character is C : Character; begin - C := L.Source (L.Pos); + C := Peek (L); if L.Pos < L.Source'Length then C := L.Source (L.Pos + 1); end if; @@ -74,32 +77,43 @@ package body Lexer is null; end loop; end Skip_Spaces_Comments; + -- make ident - function make_ident (l : in out lexer) return token is - c : character; - t : token; - tk : token_kind; - start : natural; + function Make_Ident (L : in out Lexer) return Token is + C : Character; + T : Token; + TK : Token_Kind; + Start : Natural; + Word : Unbounded_String; begin - start := l.pos; + Start := L.Pos; while true loop - c := peek (l); - if is_alphanumeric (c) or c = '_' then - nudge (l); + if Is_Alphanumeric (Peek (L)) or Peek (L) = '_' then + Nudge (L); else exit; end if; end loop; - end make_ident; - function make_ident (l : in out lexer) return token is + TK := Ident; - begin + Word := To_Unbounded_String (L.Source (Start .. L.Pos - 1)); + if Word = "print" then + Put_Line ("eureka"); + end if; - null; - end; + Put_Line (" <- make ident: |" & L.Source (Start .. L.Pos - 1) & "|"); + -- TODO upgrade to keywords + T := + (Kind => TK, + Lexeme => To_Unbounded_String (L.Source (Start .. L.Pos - 1)), + Line => L.Line, + Col => L.Col); + + return T; + end Make_Ident; -- make number @@ -110,29 +124,28 @@ package body Lexer is begin Start := L.Pos; - if Peek(L) = '-' then + if Peek (L) = '-' then Nudge (L); end if; - while Is_Digit (Peek(L)) loop + while Is_Digit (Peek (L)) loop Nudge (L); end loop; - if Peek(L) = '.' and Is_Digit (Peek2(L)) then + if Peek (L) = '.' and Is_Digit (Peek2 (L)) then Is_Float := true; Nudge (L); - while Is_Digit (Peek(L)) loop + 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)), + Lexeme => To_Unbounded_String (L.Source (Start .. L.Pos - 1)), Line => L.Line, - Col => L.Col - ); + Col => L.Col); - return T; + return T; end Make_Number; -- make string @@ -187,12 +200,17 @@ package body Lexer is C := Peek (L); C2 := Peek2 (L); + if Is_Letter (C) or C = '_' then + T := Make_Ident (L); + return T; + end if; + if C = '-' and Is_Digit (C2) then T := Make_Number (L); return T; end if; - if Is_Digit (C2) then + if Is_Digit (C) then T := Make_Number (L); return T; end if; diff --git a/test.mty b/test.mty @@ -5,4 +5,3 @@ use io fx main() :: print("hello") end -