|
|
@@ -6,6 +6,7 @@
|
|
|
#include <stdarg.h>
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
+#include <string.h>
|
|
|
|
|
|
typedef struct {
|
|
|
int line;
|
|
|
@@ -56,7 +57,6 @@ static bool isTokenEnd(char c) {
|
|
|
}
|
|
|
|
|
|
static const char* tokenizerAddLiteral(TState* t, const char* s) {
|
|
|
- tAddToken(t, LITERAL);
|
|
|
size_t index = 0;
|
|
|
char buffer[256] = {};
|
|
|
buffer[index++] = *s;
|
|
|
@@ -71,8 +71,17 @@ static const char* tokenizerAddLiteral(TState* t, const char* s) {
|
|
|
}
|
|
|
buffer[index++] = c;
|
|
|
}
|
|
|
- if(bufferWriteString(&t->tokenizer->buffer, buffer)) {
|
|
|
- tTooMuchTokens(t);
|
|
|
+ if(strcmp(buffer, "if") == 0) {
|
|
|
+ tAddToken(t, IF);
|
|
|
+ } else if(strcmp(buffer, "then") == 0) {
|
|
|
+ tAddToken(t, THEN);
|
|
|
+ } else if(strcmp(buffer, "endif") == 0) {
|
|
|
+ tAddToken(t, ENDIF);
|
|
|
+ } else {
|
|
|
+ tAddToken(t, LITERAL);
|
|
|
+ if(bufferWriteString(&t->tokenizer->buffer, buffer)) {
|
|
|
+ tTooMuchTokens(t);
|
|
|
+ }
|
|
|
}
|
|
|
return s;
|
|
|
}
|
|
|
@@ -227,18 +236,20 @@ void tokenizerDestroy(Tokenizer* t) {
|
|
|
*t = (Tokenizer){};
|
|
|
}
|
|
|
|
|
|
+#define TOKEN_PRINTER(token, format, ...) \
|
|
|
+ case token: snprintf(buffer, n, format __VA_OPT__(, ) __VA_ARGS__); return
|
|
|
+
|
|
|
void tokenizerPrintToken(const Token* token, char* buffer, size_t n) {
|
|
|
switch(token->type) {
|
|
|
- case LITERAL:
|
|
|
- snprintf(buffer, n, "Literal(%s)", token->stringValue);
|
|
|
- break;
|
|
|
- case INT64: snprintf(buffer, n, "Int64(%ld)", token->intValue); break;
|
|
|
- case STRING:
|
|
|
- snprintf(buffer, n, "String(%s)", token->stringValue);
|
|
|
- break;
|
|
|
- case PLUS: snprintf(buffer, n, "Plus"); break;
|
|
|
- case NEWLINE: snprintf(buffer, n, "Newline"); break;
|
|
|
- case END: snprintf(buffer, n, "End"); break;
|
|
|
- default: snprintf(buffer, n, "Unknown"); break;
|
|
|
- }
|
|
|
+ TOKEN_PRINTER(LITERAL, "Literal(%s)", token->stringValue);
|
|
|
+ TOKEN_PRINTER(INT64, "Int64(%ld)", token->intValue);
|
|
|
+ TOKEN_PRINTER(STRING, "String(%s)", token->stringValue);
|
|
|
+ TOKEN_PRINTER(PLUS, "Plus");
|
|
|
+ TOKEN_PRINTER(IF, "If");
|
|
|
+ TOKEN_PRINTER(THEN, "Then");
|
|
|
+ TOKEN_PRINTER(ENDIF, "EndIf");
|
|
|
+ TOKEN_PRINTER(NEWLINE, "Newline");
|
|
|
+ TOKEN_PRINTER(END, "End");
|
|
|
+ }
|
|
|
+ snprintf(buffer, n, "Unknown");
|
|
|
}
|