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