From aa7dff31ffd98e5f3a8e5f485459736527ba24c2 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 15 Feb 2022 15:59:17 +0000 Subject: [PATCH] Implemented number parsing. --- scanner.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/scanner.c b/scanner.c index d685a0d..4e88100 100644 --- a/scanner.c +++ b/scanner.c @@ -2,6 +2,7 @@ #include #include #include +#include const char* source_code; //Start and Current hold the offsets that index into the string source_code. @@ -17,7 +18,9 @@ TokenList* CreateList(void); int AddTokenToList(TokenType, const char*, int, TokenList*); int Match(char); char Peek(void); +char PeekNext(void); void ParseString(TokenList *); +void ParseNumber(TokenList *); TokenList* ScanTokens(const char* source) { if (!source) return NULL; @@ -175,6 +178,11 @@ void ScanToken(TokenList* tokens) { line++; break; default: + if (isdigit(*c)) { + ParseNumber(tokens); + break; + } + fprintf(stderr, "Unexcpedted character %c\n", *c); break; } @@ -219,4 +227,22 @@ void ParseString(TokenList *list) { AddTokenToList(String, &source_code[start], current - start, list); Advance(); // The closing ". +} + +void ParseNumber(TokenList* list) { + while(isdigit(Peek())) Advance(); + + if (Peek() == '.' && isdigit(PeekNext())) { + Advance(); + + while(isdigit(Peek())) Advance(); + } + + AddTokenToList(Number, &source_code[start], current - start, list); +} + +char PeekNext() { + if (current + 1 >= length) return '\0'; + + return source_code[current + 1]; } \ No newline at end of file