aboutsummaryrefslogtreecommitdiff
path: root/src/token.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/token.cc')
-rw-r--r--src/token.cc33
1 files changed, 33 insertions, 0 deletions
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);
+}