Added support for string literals.

This commit is contained in:
2022-02-14 19:38:49 +00:00
parent 5b90c98123
commit f1a468aebc
2 changed files with 35 additions and 1 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ void Print(TokenList* list) {
for(int i = 0; i < list->size; i++) {
if (list->tokens[i]->type == EndOF) {
printf("EOF\n");\
printf("[Line %d] EOF\n", list->tokens[i]->line);\
return;
}
+34
View File
@@ -17,6 +17,7 @@ TokenList* CreateList(void);
int AddTokenToList(TokenType, const char*, int, TokenList*);
int Match(char);
char Peek(void);
void ParseString(TokenList *);
TokenList* ScanTokens(const char* source) {
if (!source) return NULL;
@@ -143,6 +144,18 @@ void ScanToken(TokenList* tokens) {
if (Match('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens);
else AddTokenToList(Bang, c, 1, tokens);
break;
case '=':
if (Match('=')) AddTokenToList(Equal_Equal, "==", 2, tokens);
else AddTokenToList(Equal, c, 1, tokens);
break;
case '<':
if (Match('=')) AddTokenToList(Less_Equal, "<=", 2, tokens);
else AddTokenToList(Less, c, 1, tokens);
break;
case '>':
if (Match('=')) AddTokenToList(Greater_Equal, ">=", 2, tokens);
else AddTokenToList(Greater, c, 1, tokens);
break;
case '/':
if (Match('/')) {
while(Peek() != '\n' && !IsAtEnd()) Advance();
@@ -151,6 +164,9 @@ void ScanToken(TokenList* tokens) {
AddTokenToList(Slash, c, 1, tokens);
}
break;
case '"':
ParseString(tokens);
break;
case ' ':
case '\r':
case '\t':
@@ -185,4 +201,22 @@ char Peek() {
if (IsAtEnd()) return '\0';
return source_code[current];
}
void ParseString(TokenList *list) {
start = current; //The start is currently pointing to the first double quote so we need to move it
//to the next (first character) of the string literal.
while(Peek() != '"' && !IsAtEnd()) {
if (Peek() == '\n') line++;
Advance();
}
if (IsAtEnd()) {
fprintf(stderr, "Unterminated string.\n");
return;
}
AddTokenToList(String, &source_code[start], current - start, list);
Advance(); // The closing ".
}