diff options
| author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-08 23:50:41 +0530 |
|---|---|---|
| committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-08 23:50:41 +0530 |
| commit | 73d8ee51a9bf3175a1b4b339b043f45d17c9d92a (patch) | |
| tree | 9285b29ad316df4dd7398ff8d25234ab7c231d72 | |
| parent | 1de0b57e396d1964aba2c8ddc663bd55d01f5058 (diff) | |
tokens: init
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | include/token.h | 71 | ||||
| -rw-r--r-- | src/main.cc | 14 | ||||
| -rw-r--r-- | src/token.cc | 33 |
4 files changed, 117 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index aa52d73..7faef9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ set(SRC src/main.cc src/scanner.cc src/filehandler.cc + src/token.cc ) add_executable(minni ${SRC}) diff --git a/include/token.h b/include/token.h new file mode 100644 index 0000000..c6a61cd --- /dev/null +++ b/include/token.h @@ -0,0 +1,71 @@ +#ifndef __TOKEN_H +#define __TOKEN_H + +enum TokenType { + // Single-character tokens. + LEFT_PAREN, + RIGHT_PAREN, + LEFT_BRACE, + RIGHT_BRACE, + 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, + FUN, + FOR, + IF, + NIL, + OR, + PRINT, + RETURN, + SUPER, + THIS, + VAR, + WHILE, + + TRUE, + FALSE, + + EOFF +}; + +class Token +{ +private: + TokenType type; + char *lexeme; + // literal + unsigned int line; + char *string; + +public: + Token(TokenType type, char *lexeme, unsigned int line); + + char *to_string(void); + void clean(void); +}; + +#endif diff --git a/src/main.cc b/src/main.cc index ea28324..f0d4b0c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,6 +2,7 @@ #include <scanner.h> #include <stdio.h> #include <stdlib.h> +#include <token.h> int main(int argc, char **argv) @@ -12,17 +13,26 @@ main(int argc, char **argv) } Filehandler f(argv[1]); - bool success = f.open(); - if (!success) { + + bool opened = f.open(); + if (!opened) { printf("Unable to open %s\n", argv[1]); return 1; } char *buffer = f.read(); + if (buffer == NULL) { + printf("Unable to read %s\n", argv[1]); + return 1; + } Scanner s(buffer); s.scan_tokens(); + Token t(EOFF, "EOF", 221); + printf("%s\n", t.to_string()); + t.clean(); + f.close(); return 0; } diff --git a/src/token.cc b/src/token.cc new file mode 100644 index 0000000..a776d01 --- /dev/null +++ b/src/token.cc @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <token.h> + +Token::Token(TokenType type, char *lexeme, unsigned int line) +{ + this->type = type; + this->lexeme = lexeme; + this->line = line; + this->string = NULL; +} + +char * +Token::to_string(void) +{ + unsigned int line_length = snprintf(NULL, 0, "%ul", line) - 1; + /* 2: ": ", 1: "\0" */ + unsigned int final_size = strlen(lexeme) + line_length + 2 + 1; + + char *result = (char *) calloc(1, final_size); + snprintf(result, final_size, "%d: %s", line, lexeme); + string = result; + + return result; +} + +void +Token::clean(void) +{ + if (string != NULL) + free(string); +} |
