From f1a468aebca9219672b29a49a13f05ccd6e85691 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 14 Feb 2022 19:38:49 +0000 Subject: [PATCH] Added support for string literals. --- lox.c | 2 +- scanner.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lox.c b/lox.c index 8881c00..e8a598d 100644 --- a/lox.c +++ b/lox.c @@ -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; } diff --git a/scanner.c b/scanner.c index 7cee204..d685a0d 100644 --- a/scanner.c +++ b/scanner.c @@ -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 ". } \ No newline at end of file