diff --git a/lox.c b/lox.c index e8a598d..0968203 100644 --- a/lox.c +++ b/lox.c @@ -21,19 +21,13 @@ int main(int argc, char** argv) { } void Print(TokenList* list) { - char lexeme[100]; - for(int i = 0; i < list->size; i++) { if (list->tokens[i]->type == EndOF) { printf("[Line %d] EOF\n", list->tokens[i]->line);\ return; } - printf("[Line %d] ", list->tokens[i]->line); - for(int j = 0; j < list->tokens[i]->length; j++) { - printf("%c", list->tokens[i]->lexeme[j]); - } - printf("\n"); + printf("[Line %d] %s\n", list->tokens[i]->line, list->tokens[i]->lexeme); } } @@ -41,10 +35,10 @@ void RunFile(const char* path) { printf("Running '%s'\n", path); char* contents = GetFileContents(path); TokenList* tokens = ScanTokens(contents); + free(contents); Print(tokens); DestroyTokenList(tokens); - free(contents); } char* GetFileContents(const char* path) { diff --git a/parser.c b/parser.c index 6a45487..4914fce 100644 --- a/parser.c +++ b/parser.c @@ -1,9 +1,4 @@ #include "parser.h" -#include "expr.h" -#include "scanner.h" -#include -#include -#include Expr* Expression(void); Expr* Equality(void); @@ -12,17 +7,23 @@ Expr* Term(void); Expr* Factor(void); Expr* Unary(void); Expr* Primary(void); -int Match(int, ...); +int SMatch(int, ...); int Check(TokenType); -int IsAtEnd(void); -Token* Peek(void); +int SIsAtEnd(void); +Token* SPeek(void); Token* Previous(void); -Token* Advance(void); -Token* CreateToken(char*, TokenType); +Token* SAdvance(void); +//Token* CreateToken(char*, TokenType); -const TokenList* tokens; +const TokenList* ListOfTokens; int Current = 0; +Expr* GenerateExpressionTree(const TokenList* list) { + ListOfTokens = list; + + return Expression(); +} + //Simply expands the equality rule Expr* Expression() { return Equality(); @@ -31,7 +32,7 @@ Expr* Expression() { Expr* Equality() { Expr* expr = Comparison(); - while(Match(2, Bang_Equal, Equal_Equal)) { + while(SMatch(2, Bang_Equal, Equal_Equal)) { Token* operator = Previous(); Expr* right = Comparison(); Expr* temp = calloc(1, sizeof(Expr)); @@ -48,7 +49,7 @@ Expr* Equality() { Expr* Comparison() { Expr* expr = Term(); - while(Match(4, Greater, Greater_Equal, Less, Less_Equal)) { + while(SMatch(4, Greater, Greater_Equal, Less, Less_Equal)) { Token* operator = Previous(); Expr* right = Term(); Expr* temp = calloc(1, sizeof(Expr)); @@ -65,7 +66,7 @@ Expr* Comparison() { Expr* Term() { Expr* expr = Factor(); - while(Match(2, Minus, Plus)) { + while(SMatch(2, Minus, Plus)) { Token* operator = Previous(); Expr* right = Factor(); Expr* temp = calloc(1, sizeof(Expr)); @@ -82,7 +83,7 @@ Expr* Term() { Expr* Factor() { Expr* expr = Unary(); - while(Match(2, Slash, Star)) { + while(SMatch(2, Slash, Star)) { Token* operator = Previous(); Expr* right = Unary(); Expr* temp = calloc(1, sizeof(Expr)); @@ -97,7 +98,7 @@ Expr* Factor() { } Expr* Unary() { - if (Match(2, Bang, Minus)) { + if (SMatch(2, Bang, Minus)) { Token* operator = Previous(); Expr* right = Unary(); Expr* expr = calloc(1, sizeof(Expr)); @@ -114,25 +115,25 @@ Expr* Primary() { Expr* expr = calloc(1, sizeof(Expr)); expr->type = LITERAL; - if (Match(1, FALSE)) { - expr->expression.Literal.type = CreateToken("false", FALSE); - return expr; - } - if (Match(1, TRUE)) { - expr->expression.Literal.type = CreateToken("true", TRUE); - return expr; - } - if (Match(1, NIL)) { - expr->expression.Literal.type = CreateToken("nil", NIL); + if (SMatch(3, FALSE, TRUE, NIL)) { + expr->expression.Literal.type = SPeek();//CreateToken("false", FALSE); return expr; } + // if (SMatch(1, TRUE)) { + // expr->expression.Literal.type = //CreateToken("true", TRUE); + // return expr; + // } + // if (SMatch(1, NIL)) { + // expr->expression.Literal.type = //CreateToken("nil", NIL); + // return expr; + // } - if (Match(2, Number, String)) { + if (SMatch(2, Number, String)) { expr->expression.Literal.type = Previous(); return expr; } - if (Match(1, LParen)) { + if (SMatch(1, LParen)) { free(expr); expr = Expression(); //Consume(RParen, "Expect ')' after expression."); @@ -143,13 +144,13 @@ Expr* Primary() { return expr; } -int Match(int count, ...) { +int SMatch(int count, ...) { va_list list; va_start(list, count); for(int i = 0; i < count; i++) { if(Check(va_arg(list, TokenType))) { - Advance(); + SAdvance(); return 1; } } @@ -158,33 +159,33 @@ int Match(int count, ...) { } int Check(TokenType type) { - if (IsAtEnd()) return 0; - return Peek()->type == type; + if (SIsAtEnd()) return 0; + return SPeek()->type == type; } -int IsAtEnd() { - return Peek()->type == EndOF; +int SIsAtEnd() { + return SPeek()->type == EndOF; } -Token* Peek() { - return tokens->tokens[Current]; +Token* SPeek() { + return ListOfTokens->tokens[Current]; } Token* Previous() { - return tokens->tokens[Current - 1]; + return ListOfTokens->tokens[Current - 1]; } -Token* Advance() { - if (!IsAtEnd()) Current++; +Token* SAdvance() { + if (!SIsAtEnd()) Current++; return Previous(); } -Token* CreateToken(char* lexeme, TokenType type) { - Token* token = calloc(1, sizeof(Token)); - token->type = type; - token->lexeme = lexeme; - token->length = strlen(lexeme); +// Token* CreateToken(char* lexeme, TokenType type) { +// Token* token = calloc(1, sizeof(Token)); +// token->type = type; +// token->lexeme = lexeme; +// token->length = strlen(lexeme); - return token; -} \ No newline at end of file +// return token; +// } \ No newline at end of file diff --git a/parser.h b/parser.h index 85c1117..bd87896 100644 --- a/parser.h +++ b/parser.h @@ -1,6 +1,13 @@ #ifndef PARSER_H #define PARSER_H +#include "expr.h" +#include "scanner.h" +#include "token.h" +#include +#include +#include +Expr* GenerateExpressionTree(const TokenList* list); #endif \ No newline at end of file diff --git a/scanner.c b/scanner.c index a557371..5cac794 100644 --- a/scanner.c +++ b/scanner.c @@ -1,32 +1,5 @@ #include "scanner.h" -#include -#include -#include -#include - -typedef struct { - char* keyword; - TokenType type; -} KeywordPair; - -KeywordPair keywords[KEYWORD_COUNT] = { - { "and", AND }, - { "class", CLASS }, - { "else", ELSE }, - { "false", FALSE }, - { "for", FOR }, - { "fun", FUN }, - { "if", IF }, - { "nil", NIL }, - { "or", OR }, - { "print", PRINT }, - { "return", RETURN }, - { "super", SUPER }, - { "this", THIS }, - { "true", TRUE }, - { "var", VAR }, - { "while", WHILE } -}; +#include "token.h" const char* source_code; //Start and Current hold the offsets that index into the string source_code. @@ -39,7 +12,7 @@ const char* SAdvance(void); int SIsAtEnd(void); void ScanToken(TokenList*); TokenList* CreateList(void); -int AddTokenToList(TokenType, const char*, int, TokenList*); +int AddToTokenList(Token*, TokenList*); int SMatch(char); char SPeek(void); char PeekNext(void); @@ -47,7 +20,7 @@ void ParseString(TokenList *); void ParseNumber(TokenList *); void ParseIdentifier(TokenList *); int IsAlpha(char c); -KeywordPair* Get(char*); +KeyValuePair* Get(const char*); TokenList* ScanTokens(const char* source) { if (!source) return NULL; @@ -64,8 +37,7 @@ TokenList* ScanTokens(const char* source) { ScanToken(tokens); } - //Add EOF token and return list once that's set up. - AddTokenToList(EndOF, NULL, 0, tokens); + AddToTokenList(CreateToken(NULL, line, EndOF), tokens); return tokens; } @@ -96,7 +68,10 @@ void DestroyTokenList(TokenList* list) { if (!list) return; for(int i = 0; i < list->size; i++) { - //free(list->tokens[i]->lexeme); This shouldn't be needed since the lexeme is a pointer into the source code. + //Only free objects that required allocation, i.e. not in the TokenTypeMappings + if (list->tokens[i]->type == String || list->tokens[i]->type == Number || list->tokens[i]->type == Identifier) + free((void *) list->tokens[i]->lexeme); + free(list->tokens[i]); } @@ -104,34 +79,24 @@ void DestroyTokenList(TokenList* list) { free(list); } -int AddTokenToList(TokenType type, const char* lexeme, int length, TokenList* tokens) { - if (!tokens) return 0; - Token* token = calloc(1, sizeof(Token)); +int AddToTokenList(Token* token, TokenList* list) { + if (!list) return 0; + if (!token) return 0; - if (!token) { - fprintf(stderr, "Failed to calloc memory for new Token.\n"); - return 0; - } - - token->lexeme = lexeme; - token->type = type; - token->length = length; //Set the length of the lexeme (which is just a pointer into the complete source listing). - token->line = line; - - if ((tokens->size + 1) > tokens->capacity) { - void* new_ptr = realloc(tokens->tokens, sizeof(Token*) * tokens->capacity * 2); + if ((list->size + 1) > list->capacity) { + void* new_ptr = realloc(list->tokens, sizeof(Token*) * list->capacity * 2); if (!new_ptr) { - fprintf(stderr, "Failed to realloc TokenList to size %d.\n", tokens->capacity * 2); + fprintf(stderr, "Failed to realloc TokenList to size %d.\n", list->capacity * 2); return 0; } - tokens->tokens = new_ptr; - tokens->capacity = tokens->capacity * 2; + list->tokens = new_ptr; + list->capacity = list->capacity * 2; } - tokens->tokens[tokens->size] = token; - tokens->size++; + list->tokens[list->size] = token; + list->size++; return 1; } @@ -141,58 +106,55 @@ void ScanToken(TokenList* tokens) { switch (*c) { case '(': - AddTokenToList(LParen, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, LParen), tokens); break; case ')': - AddTokenToList(RParen, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, LParen), tokens); break; case '{': - AddTokenToList(LBrace, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, LParen), tokens); break; case '}': - AddTokenToList(RBrace, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, RParen), tokens); break; case ',': - AddTokenToList(Comma, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, Comma), tokens); break; case '.': - AddTokenToList(Dot, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, Dot), tokens); break; case '-': - AddTokenToList(Minus, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, Minus), tokens); break; case '+': - AddTokenToList(Plus, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, Plus), tokens); break; case ';': - AddTokenToList(Semicolon, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, Semicolon), tokens); break; case '*': - AddTokenToList(Star, c, 1, tokens); + AddToTokenList(CreateToken(NULL, line, Star), tokens); break; case '!': - if (SMatch('=')) AddTokenToList(Bang_Equal, "!=", 2, tokens); - else AddTokenToList(Bang, c, 1, tokens); + if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Bang_Equal), tokens); + else AddToTokenList(CreateToken(NULL, line, Bang), tokens); break; case '=': - if (SMatch('=')) AddTokenToList(Equal_Equal, "==", 2, tokens); - else AddTokenToList(Equal, c, 1, tokens); + if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Equal_Equal), tokens); + else AddToTokenList(CreateToken(NULL, line, Equal), tokens); break; case '<': - if (SMatch('=')) AddTokenToList(Less_Equal, "<=", 2, tokens); - else AddTokenToList(Less, c, 1, tokens); + if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Less_Equal), tokens); + else AddToTokenList(CreateToken(NULL, line, Less), tokens); break; case '>': - if (SMatch('=')) AddTokenToList(Greater_Equal, ">=", 2, tokens); - else AddTokenToList(Greater, c, 1, tokens); + if (SMatch('=')) AddToTokenList(CreateToken(NULL, line, Greater_Equal), tokens); + else AddToTokenList(CreateToken(NULL, line, Greater), tokens); break; case '/': - if (SMatch('/')) { - while(SPeek() != '\n' && !SIsAtEnd()) SAdvance(); - } - else { - AddTokenToList(Slash, c, 1, tokens); - } + if (SMatch('/')) while(SPeek() != '\n' && !SIsAtEnd()) { SAdvance(); } + else AddToTokenList(CreateToken(NULL, line, Slash), tokens); + break; case '"': ParseString(tokens); @@ -254,7 +216,16 @@ void ParseString(TokenList *list) { return; } - AddTokenToList(String, &source_code[start], current - start, list); + char* lexeme = calloc(current - start + 1, sizeof(char)); + + if (!lexeme) { + fprintf(stderr, "Failed to calloc for string lexeme. %s\n", strerror(errno)); + return; + } + + snprintf(lexeme, current - start, "%s", &source_code[start]); + + AddToTokenList(CreateToken(lexeme, line, String), list); SAdvance(); // The closing ". } @@ -268,7 +239,16 @@ void ParseNumber(TokenList* list) { while(isdigit(SPeek())) SAdvance(); } - AddTokenToList(Number, &source_code[start], current - start, list); + char* lexeme = calloc(current - start + 2, sizeof(char)); + + if (!lexeme) { + fprintf(stderr, "Failed to calloc for number lexeme. %s\n", strerror(errno)); + return; + } + + snprintf(lexeme, current - start + 1, "%s", &source_code[start]); + + AddToTokenList(CreateToken(lexeme, line, Number), list); } char PeekNext() { @@ -280,16 +260,18 @@ char PeekNext() { void ParseIdentifier(TokenList * list) { while(IsAlpha(SPeek())) SAdvance(); - char* lexeme = calloc(sizeof(char*), (current - start + 1)); + char* lexeme = calloc(current - start + 2, sizeof(char)); snprintf(lexeme, current - start + 1, "%s", &source_code[start]); + printf("%s\n", lexeme); + KeyValuePair* result = Get(lexeme); - KeywordPair* result = Get(lexeme); + if (result) { + free(lexeme); - if (result) AddTokenToList(result->type, &source_code[start], current - start, list); - else AddTokenToList(Identifier, &source_code[start], current - start, list); - - free(lexeme); + AddToTokenList(CreateToken(NULL, line, result->type), list); + } + else AddToTokenList(CreateToken(lexeme, line, Identifier), list); } int IsAlpha(char c) { @@ -298,11 +280,11 @@ int IsAlpha(char c) { (c == '_'); } -KeywordPair* Get(char* text) { +KeyValuePair* Get(const char* text) { if (!text) return NULL; - - for (int i = 0; i < KEYWORD_COUNT; i++) - if (strcmp(text, keywords[i].keyword) == 0) return &keywords[i]; + + for (int i = 0; i < TOKENTYPE_MAPPINGS_COUNT; i++) + if (strcmp(text, TokenTypeMappings[i].lexeme) == 0) return &TokenTypeMappings[i]; return NULL; } \ No newline at end of file diff --git a/scanner.h b/scanner.h index f476881..07220e6 100644 --- a/scanner.h +++ b/scanner.h @@ -1,41 +1,13 @@ #ifndef SCANNER_H #define SCANNER_H +#include "token.h" +#include +#include +#include +#include + #define DEFAULT_TOKENLIST_SIZE 32 -#define KEYWORD_COUNT 16 - -typedef enum { - //Single-character tokens - LParen, RParen, - LBrace, RBrace, - Comma, - Dot, - Minus, Plus, - Semicolon, - Slash, Star, - //One or two character tokens - Bang, Bang_Equal, - Equal, Equal_Equal, - Greater, Greater_Equal, - Less, Less_Equal, - //Literals - Identifier, - String, - Number, - //Keywords - AND, CLASS, ELSE, FALSE, FUN, - FOR, IF, NIL, OR, PRINT, RETURN, - SUPER, THIS, TRUE, VAR, WHILE, - - EndOF -} TokenType; - -typedef struct { - TokenType type; - const char* lexeme; - int line; - int length; -} Token; typedef struct { Token** tokens; diff --git a/token.c b/token.c new file mode 100644 index 0000000..8e3afb9 --- /dev/null +++ b/token.c @@ -0,0 +1,51 @@ +#include "token.h" + +KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT] = { + { "(", LParen}, { ")", RParen}, { ",", Comma }, { ".", Dot }, + { "-", Minus }, { "+", Plus }, { ";", Semicolon }, { "/", Slash}, { "*", Star }, + { "!", Bang }, { "!=", Bang_Equal }, { "=", Equal }, { "==", Equal_Equal }, + { ">", Greater }, { ">=", Greater_Equal }, { "<", Less }, { "<=", Less_Equal }, + { "and", AND }, { "class", CLASS }, { "else", ELSE }, + { "false", FALSE }, { "for", FOR }, { "fun", FUN }, + { "if", IF }, { "nil", NIL }, { "or", OR }, + { "print", PRINT }, { "return", RETURN }, { "super", SUPER }, + { "this", THIS }, { "true", TRUE }, { "var", VAR }, { "while", WHILE }, + { "", EndOF } +}; + +const char* GetLexemeMapping(TokenType); + +Token* CreateToken(const char* lexeme, int line, TokenType type) { + Token* token = calloc(1, sizeof(Token)); + + if (!token) { + fprintf(stderr, "Failed to calloc token. %s", strerror(errno)); + return NULL; + } + + if (lexeme) token->lexeme = lexeme; + else { + const char* mapping_result = GetLexemeMapping(type); + + if (!mapping_result) { + fprintf(stderr, "Failed to get the mapping for %s\n", lexeme); + free(token); + return NULL; + } + + token->lexeme = mapping_result; + } + + token->line = line; + token->type = type; + + return token; +} + +const char* GetLexemeMapping(TokenType type) { + for (int i = 0; i < TOKENTYPE_MAPPINGS_COUNT; i++) { + if (TokenTypeMappings[i].type == type) return TokenTypeMappings[i].lexeme; + } + + return NULL; +} \ No newline at end of file diff --git a/token.h b/token.h new file mode 100644 index 0000000..ea114c6 --- /dev/null +++ b/token.h @@ -0,0 +1,54 @@ +#ifndef TOKEN_H +#define TOKEN_H + +#include +#include +#include +#include + +#define TOKENTYPE_MAPPINGS_COUNT 34 + +typedef enum { + //Single-character tokens + LParen, RParen, + LBrace, RBrace, + Comma, + Dot, + Minus, Plus, + Semicolon, + Slash, Star, + //One or two character tokens + Bang, Bang_Equal, + Equal, Equal_Equal, + Greater, Greater_Equal, + Less, Less_Equal, + //Literals + Identifier, + String, + Number, + //Keywords + AND, CLASS, ELSE, FALSE, FUN, + FOR, IF, NIL, OR, PRINT, RETURN, + SUPER, THIS, TRUE, VAR, WHILE, + + EndOF +} TokenType; + +typedef struct { + const char* lexeme; + TokenType type; +} KeyValuePair; + +typedef struct { + TokenType type; + const char* lexeme; + int line; + int length; +} Token; + +extern KeyValuePair TokenTypeMappings[TOKENTYPE_MAPPINGS_COUNT]; + +//If the first parameter is NULL, the token creation will attempt to infer the lexeme from the TokenType. +Token* CreateToken(const char*, int, TokenType); + +#endif \ No newline at end of file