commit f5940714e6810f25fcfd37432cd877323f65957e
parent f0c9a9362930f4080d65cdd40729a254358e413a
Author: citbl <citbl@citbl.org>
Date: Mon, 1 Jun 2026 19:35:39 +1000
upgrade to keywords
Diffstat:
2 files changed, 50 insertions(+), 14 deletions(-)
diff --git a/src/lexer.adb b/src/lexer.adb
@@ -86,8 +86,12 @@ package body Lexer is
TK : Token_Kind;
Start : Natural;
Word : Unbounded_String;
+ Line : Positive;
+ Col : Positive;
begin
Start := L.Pos;
+ Line := L.Line;
+ Col := L.Col;
while true loop
if Is_Alphanumeric (Peek (L)) or Peek (L) = '_' then
@@ -100,17 +104,41 @@ package body Lexer is
TK := Ident;
Word := To_Unbounded_String (L.Source (Start .. L.Pos - 1));
- if Word = "print" then
- Put_Line ("eureka");
- end if;
- 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);
+
+ declare
+ W : constant String := To_String (Word);
+ begin
+ if W = "use"
+ or W = "ns"
+ or W = "from"
+ or W = "ffi"
+ or W = "as"
+ or W = "and"
+ or W = "or"
+ or W = "struct"
+ or W = "enum"
+ or W = "if"
+ or W = "else"
+ or W = "end"
+ or W = "pub"
+ or W = "pub"
+ or W = "fx"
+ or W = "fn"
+ or W = "do"
+ or W = "return"
+ or W = "continue"
+ or W = "break"
+ or W = "is"
+ or W = "for"
+ or W = "while"
+ then
+ TK := Keyword;
+ end if;
+ end;
+
+ T := (Kind => TK, Lexeme => Word, Line => Line, Col => Col);
return T;
end Make_Ident;
@@ -121,8 +149,12 @@ package body Lexer is
T : Token;
Start : Natural;
Is_Float : Boolean := false;
+ Line : Positive;
+ Col : Positive;
begin
Start := L.Pos;
+ Line := L.Line;
+ Col := L.Col;
if Peek (L) = '-' then
Nudge (L);
@@ -142,8 +174,8 @@ package body Lexer is
T :=
(Kind => Str_Literal,
Lexeme => To_Unbounded_String (L.Source (Start .. L.Pos - 1)),
- Line => L.Line,
- Col => L.Col);
+ Line => Line,
+ Col => Col);
return T;
end Make_Number;
@@ -154,8 +186,12 @@ package body Lexer is
C : Character;
T : Token;
Start : Natural;
+ Line : Positive;
+ Col : Positive;
begin
Start := L.Pos;
+ Line := L.Line;
+ Col := L.Col;
Nudge (L); -- advance `"`
while true loop
@@ -172,8 +208,8 @@ package body Lexer is
T :=
(Kind => Str_Literal,
Lexeme => To_Unbounded_String (L.Source (Start .. L.Pos)),
- Line => L.Line,
- Col => L.Col);
+ Line => Line,
+ Col => Col);
Nudge (L); -- advance `"`
return T;
@@ -190,7 +226,6 @@ package body Lexer is
begin
Skip_Spaces_Comments (L);
- Put (Peek (L));
if L.Pos < L.Source'Length then
TK := Unknown;
else
diff --git a/src/lexer.ads b/src/lexer.ads
@@ -8,6 +8,7 @@ package Lexer is
Int_Literal,
Str_Literal,
Bool_Literal,
+ Keyword,
Unknown,
EOF);