Fixed a bug where the tokenizer wasn't parsing hexadecimal numbers correctly. Added support for tokenizing the colon and comma characters.

This commit is contained in:
2025-01-08 23:22:43 -06:00
parent 428d674f54
commit ebfd7cf94c
6 changed files with 86 additions and 21 deletions
+4 -1
View File
@@ -1,6 +1,8 @@
#ifndef ARRAY_H #ifndef ARRAY_H
#define ARRAY_H #define ARRAY_H
#include <stdbool.h>
typedef struct _array { typedef struct _array {
int Capacity; int Capacity;
int Size; int Size;
@@ -8,6 +10,7 @@ typedef struct _array {
} Array; } Array;
Array* ArrayCreate(void); Array* ArrayCreate(void);
int ArrayAdd(Array* array, void* item); bool ArrayAdd(Array* array, void* item);
void* ArrayPeek(Array* array);
#endif #endif
+3 -1
View File
@@ -12,6 +12,8 @@ typedef enum {
Number, Number,
Symbol, Symbol,
LineEnd = '\n', LineEnd = '\n',
Colon = ':',
Comma = ',',
Plus = '+', Plus = '+',
Minus = '-', Minus = '-',
Star = '*', Star = '*',
@@ -37,7 +39,7 @@ typedef struct _token {
} Token; } Token;
Token* TokenCreate(TokenType type, int lineNumber, int columnNumber, bool resolved); Token* TokenCreate(TokenType type, int lineNumber, int columnNumber, bool resolved);
char* TokenStringify(const Token* token, bool valueOnly); char* TokenStringify(const Token* token);
int StringifyTokenType(TokenType type, char buffer[32]); int StringifyTokenType(TokenType type, char buffer[32]);
#endif #endif
+11 -3
View File
@@ -12,8 +12,8 @@ struct _array* ArrayCreate(void) {
return array; return array;
} }
int ArrayAdd(struct _array* array, void* item) { bool ArrayAdd(struct _array* array, void* item) {
if (!array) return 0; if (!array) return false;
if (array->Size == array->Capacity) { if (array->Size == array->Capacity) {
array->Items = realloc(array->Items, array->Capacity * 2 * sizeof(void*)); array->Items = realloc(array->Items, array->Capacity * 2 * sizeof(void*));
@@ -24,5 +24,13 @@ int ArrayAdd(struct _array* array, void* item) {
array->Items[array->Size] = item; array->Items[array->Size] = item;
array->Size++; array->Size++;
return 1; return true;
}
void* ArrayPeek(struct _array* array) {
if (!array) return NULL;
if (array->Size == 0 || array->Capacity == 0) return NULL;
return array->Items[array->Size - 1];
} }
+15 -1
View File
@@ -30,10 +30,24 @@ int main(int argc, char** argv) {
return 0; return 0;
} }
char* buffer;
for(int i = 0; i < tokens->Size; i++) { for(int i = 0; i < tokens->Size; i++) {
Token* token = tokens->Items[i]; Token* token = tokens->Items[i];
printf("%s ", TokenStringify(token, false)); if (token->Type == LineEnd) {
printf("\n");
continue;
}
buffer = TokenStringify(token);
if (buffer) {
printf("%s ", buffer);
free(buffer);
}
} }
printf("\n"); printf("\n");
+26 -9
View File
@@ -11,26 +11,43 @@ struct _token* TokenCreate(TokenType type, int lineNumber, int columnNumber, boo
return token; return token;
} }
char* TokenStringify(const struct _token* token, bool valueOnly) { char* TokenStringify(const struct _token* token) {
if (!token) return NULL; if (!token) return false;
char buffer[32];
char* buffer = NULL;
switch(token->Type) { switch(token->Type) {
case Empty:
return "<>";
case String: case String:
case Symbol: case Symbol:
return token->Value.String; buffer = calloc(strlen(token->Value.String) + 1, sizeof(char));
strncpy(buffer, token->Value.String, strlen(token->Value.String));
break;
case Empty:
buffer = calloc(8, sizeof(char));
strncpy(buffer, "<>", sizeof(char) * 2);
break;
case Number: case Number:
buffer = calloc(32, sizeof(char));
sprintf(buffer, "%d", token->Value.Number); sprintf(buffer, "%d", token->Value.Number);
break; break;
case LineEnd: case LineEnd:
return "\n"; buffer = calloc(8, sizeof(char));
strncpy(buffer, "\n", sizeof(char));
break;
case FileEnd: case FileEnd:
return NULL; break;
default: default:
strncpy(buffer, token->Value.Operator, 8); buffer = calloc(16, sizeof(char));
strncpy(buffer, token->Value.Operator, sizeof(char));
break; break;
} }
+27 -6
View File
@@ -64,8 +64,13 @@ Array* Tokenize(const char* sourceCode, Dictionary** variables) {
continue; continue;
} }
Token* token = NULL;
switch(c) { switch(c) {
case '\n': case '\n':
token = (Token*) ArrayPeek(Tokens);
//Is there a previous token and if so, was it a line break?
if (token && token->Type == LineEnd) break; //Discard empty line.
case '+': case '+':
case '-': case '-':
case '*': case '*':
@@ -76,7 +81,8 @@ Array* Tokenize(const char* sourceCode, Dictionary** variables) {
case '[': case '[':
case ']': case ']':
case ':': case ':':
Token* token = TokenCreate((TokenType)c, SourceLineNumber, SourceColumnNumber, true); case ',':
token = TokenCreate((TokenType)c, SourceLineNumber, SourceColumnNumber, true);
token->Value.Operator[0] = c; token->Value.Operator[0] = c;
@@ -106,18 +112,18 @@ void TokenizerExpectNumber(void) {
break; break;
} }
if (!isdigit(c)) break;
if (PeekTokenizer() == 'x' || PeekTokenizer() == 'X') if (PeekTokenizer() == 'x' || PeekTokenizer() == 'X')
{ {
AdvanceTokenizer(); AdvanceTokenizer();
word[wordLength] = c; word[wordLength] = c;
word[wordLength + 1] = PeekTokenizer(); word[wordLength + 1] = 'x';
wordLength += 2; wordLength += 2;
continue; continue;
} }
if (!isxdigit(c)) break;
if (wordLength == 255) break; //Sync, error etc here at some point. if (wordLength == 255) break; //Sync, error etc here at some point.
@@ -190,7 +196,7 @@ void TokenizerExpectString(void) {
break; break;
} }
if (c == '"') break; if (c == '"') break;
if (c == '\\') { if (c == '\\') {
@@ -267,7 +273,22 @@ char AdvanceTokenizer() {
bool BackTokenzier(void) { bool BackTokenzier(void) {
if (SourceCodeIndex == 0) return false; if (SourceCodeIndex == 0) return false;
SourceColumnNumber--; if (SourceCode[SourceCodeIndex - 1] == '\n') {
SourceColumnNumber = 1;
for(int i = SourceCodeIndex - 1; i >= 0; i--) {
char c = SourceCode[i];
if (c == '\n') break;
SourceColumnNumber++;
}
SourceLineNumber--;
}
else
SourceColumnNumber--;
SourceCodeIndex--; SourceCodeIndex--;
return true; return true;