Implemented number parsing.

This commit is contained in:
2022-02-15 15:59:17 +00:00
parent f1a468aebc
commit aa7dff31ff
+26
View File
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
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];
}