Implemented number parsing.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
const char* source_code;
|
const char* source_code;
|
||||||
//Start and Current hold the offsets that index into the string 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 AddTokenToList(TokenType, const char*, int, TokenList*);
|
||||||
int Match(char);
|
int Match(char);
|
||||||
char Peek(void);
|
char Peek(void);
|
||||||
|
char PeekNext(void);
|
||||||
void ParseString(TokenList *);
|
void ParseString(TokenList *);
|
||||||
|
void ParseNumber(TokenList *);
|
||||||
|
|
||||||
TokenList* ScanTokens(const char* source) {
|
TokenList* ScanTokens(const char* source) {
|
||||||
if (!source) return NULL;
|
if (!source) return NULL;
|
||||||
@@ -175,6 +178,11 @@ void ScanToken(TokenList* tokens) {
|
|||||||
line++;
|
line++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (isdigit(*c)) {
|
||||||
|
ParseNumber(tokens);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Unexcpedted character %c\n", *c);
|
fprintf(stderr, "Unexcpedted character %c\n", *c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -219,4 +227,22 @@ void ParseString(TokenList *list) {
|
|||||||
AddTokenToList(String, &source_code[start], current - start, list);
|
AddTokenToList(String, &source_code[start], current - start, list);
|
||||||
|
|
||||||
Advance(); // The closing ".
|
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];
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user