Added a dedicated function to the tokenizer for getting string literals.

This commit is contained in:
2022-01-31 19:15:27 +00:00
parent 375fd46045
commit f88cfda90a
2 changed files with 31 additions and 21 deletions
+6 -6
View File
@@ -81,14 +81,14 @@ Token* GetNextToken(char *string) {
if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token);
if (IsRegister(string_token)) return CreateToken(TK_Register, string_token);
char* next = SplitOnBasicGrammar(NULL);
// char* next = SplitOnBasicGrammar(NULL);
if (next && strcmp(next, ":") == 0) {
free(next);
return CreateToken(TK_Label, string_token);
}
// if (next && strcmp(next, ":") == 0) {
// free(next);
// return CreateToken(TK_Label, string_token);
// }
if (next) free(next);
// if (next) free(next);
return CreateToken(TK_String, string_token);
}
+25 -15
View File
@@ -1,4 +1,8 @@
#include "../includes/tokenizer.h"
#include <stdio.h>
#include <string.h>
void GetStringLiteral(char*, unsigned long, char**, unsigned long*);
char* SplitOnBasicGrammar(char* line) {
static char* string;
@@ -9,20 +13,12 @@ char* SplitOnBasicGrammar(char* line) {
position = 0;
}
char* token = calloc(1, strlen(string) + 1);
int parsing_string = 0;
unsigned long length = strlen(string);
char* token = calloc(1, length + 1);
//int parsing_string = 0;
for (int i = 0; position < strlen(string); i++, position++) {
for (int i = 0; position < length; i++, position++) {
if (parsing_string) {
if (string[position] == '\n') break;
if (string[position] == '"') break;
token[i] = string[position];
continue;
}
if (string[position] == ';') break;
if (string[position] == ':') {
@@ -35,9 +31,9 @@ char* SplitOnBasicGrammar(char* line) {
}
if (string[position] == '"') {
parsing_string = 1;
i--;
continue;
position++;
GetStringLiteral(string, length, &token, &position);
break;
}
if (string[position] == ' ') {
@@ -63,6 +59,20 @@ char* SplitOnBasicGrammar(char* line) {
return token;
}
void GetStringLiteral(char* text, unsigned long length, char** token, unsigned long* position) {
if (!text) return;
if (!token) return;
for(int i = 0; *position < length; (*position)++, i++) {
if (text[*position] == '"') {
(*position)++;
break;
}
(*token)[i] = text[*position];
}
}
// TokenType GetOperatorType(char c) {
// switch (c){
// case '+':