Code cleanup. Converting a number lexeme to a proper integer setup but not done yet. So number tokens don't have a valid value.
This commit is contained in:
+35
-24
@@ -1,8 +1,4 @@
|
||||
#include "../includes/scanner.h"
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* SourceCode;
|
||||
int Line = 0;
|
||||
@@ -96,18 +92,21 @@ Token* ParseNumber(void) {
|
||||
|
||||
if (length == 0) return NULL;
|
||||
|
||||
char* lexeme = calloc(sizeof(char), length);
|
||||
char* lexeme = calloc(sizeof(char), length + 1);
|
||||
|
||||
if (!lexeme) {
|
||||
fprintf(stderr, "Failed to calloc space for number.\n");
|
||||
fprintf(stderr, "Failed to calloc space for number. %s.\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(lexeme, &SourceCode[start], length);
|
||||
//Setting the base to zero means the function will detect the base.
|
||||
//https://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html
|
||||
printf("Number Parsed: %s (%ld)\n", lexeme, strtol(lexeme, NULL, 0));
|
||||
|
||||
if (base == 10) return CreateToken(NUMBER, lexeme);
|
||||
if (base == 10) return CreateToken(lexeme, lexeme, Line, NUMBER);
|
||||
|
||||
return CreateToken(HEX, lexeme);
|
||||
return CreateToken(lexeme, lexeme, Line, HEX);
|
||||
}
|
||||
|
||||
Token* ParseDirective(void) {
|
||||
@@ -119,17 +118,17 @@ Token* ParseDirective(void) {
|
||||
|
||||
if (length == 0) return NULL;
|
||||
|
||||
char* directive = calloc(sizeof(char), length);
|
||||
char* directive = calloc(sizeof(char), length + 1);
|
||||
|
||||
if (!directive) {
|
||||
fprintf(stderr, "Failed to calloc for the assmebler directive.\n");
|
||||
fprintf(stderr, "Failed to calloc for the assmebler directive. %s.\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(directive, &SourceCode[start], length);
|
||||
|
||||
if (strcmp(directive, ".db") == 0) {
|
||||
return CreateToken(DB, directive);
|
||||
return CreateToken(directive, directive, Line, DB);
|
||||
}
|
||||
else if (strcmp(directive, ".org") == 0) {
|
||||
fprintf(stderr, "[Warning] Org is not a supported directive.\n");
|
||||
@@ -164,12 +163,18 @@ Token* ParseString(void) {
|
||||
|
||||
if (length == 0) return NULL;
|
||||
|
||||
char* lexeme = calloc(sizeof(char), length);
|
||||
char* lexeme = calloc(sizeof(char), length + 1);
|
||||
|
||||
if (!lexeme) {
|
||||
fprintf(stderr, "Failed to calloc memory for string. %s.\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(lexeme, &SourceCode[start], length);
|
||||
|
||||
AdvanceScanner(); //Consume the trailing double quote.
|
||||
|
||||
Token* token = CreateToken(STRING, lexeme);
|
||||
Token* token = CreateToken(lexeme, lexeme, Line, STRING);
|
||||
|
||||
return token;
|
||||
}
|
||||
@@ -185,15 +190,21 @@ Token* ParseIdentifier(void) {
|
||||
|
||||
if (length == 0) return NULL;
|
||||
|
||||
TokenType type;
|
||||
char* lexeme = calloc(sizeof(char), length);
|
||||
TokenType type;
|
||||
char* lexeme = calloc(sizeof(char), length + 1);
|
||||
|
||||
if (!lexeme) {
|
||||
fprintf(stderr, "Failed to calloc memory for identifier. %s.\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(lexeme, &SourceCode[start], length);
|
||||
|
||||
if (IsOpcode(lexeme, &type)) return CreateToken(type, lexeme);
|
||||
if (IsRegister(lexeme, &type)) return CreateToken(type, lexeme);
|
||||
if (lexeme[length - 1] == ':') return CreateToken(LABEL, lexeme);
|
||||
if (IsOpcode(lexeme, &type)) return CreateToken(lexeme, lexeme, Line, type);
|
||||
if (IsRegister(lexeme, &type)) return CreateToken(lexeme, lexeme, Line, type);
|
||||
if (lexeme[length - 1] == ':') return CreateToken(lexeme, lexeme, Line, LABEL);
|
||||
|
||||
return CreateToken(IDENTIFIER, lexeme);
|
||||
return CreateToken(lexeme, lexeme, Line, IDENTIFIER);
|
||||
}
|
||||
|
||||
char PeekScanner(void) {
|
||||
@@ -234,15 +245,15 @@ int IsPunctuation(char c) {
|
||||
Token* ParsePunctuation(char c) {
|
||||
switch(c) {
|
||||
case '[':
|
||||
return CreateToken(LBRACKET, "[");
|
||||
return CreateToken("[", NULL, Line, LBRACKET);
|
||||
case ']':
|
||||
return CreateToken(RBracket, "]");
|
||||
return CreateToken("]", NULL, Line, RBracket);
|
||||
case '(':
|
||||
return CreateToken(LPARAM, "(");
|
||||
return CreateToken("(", NULL, Line, LPARAM);
|
||||
case ')':
|
||||
return CreateToken(RPARAM, ")");
|
||||
return CreateToken(")", NULL, Line, RPARAM);
|
||||
case ',':
|
||||
return CreateToken(COMMA, ",");
|
||||
return CreateToken(",", NULL, Line, COMMA);
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user