Split up some code to more in line with what its actually doing.
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef LEXER_H
|
||||||
|
#define LEXER_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include "list.h"
|
||||||
|
#include "tokenizer.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TK_Add,
|
||||||
|
TK_Sub,
|
||||||
|
TK_Mul,
|
||||||
|
TK_Div,
|
||||||
|
TK_Power,
|
||||||
|
TK_LParam,
|
||||||
|
TK_RParam,
|
||||||
|
TK_LBracket,
|
||||||
|
TK_RBracket,
|
||||||
|
TK_String,
|
||||||
|
TK_Number,
|
||||||
|
TK_Hex,
|
||||||
|
TK_Opcode,
|
||||||
|
TK_Register,
|
||||||
|
TK_Label
|
||||||
|
} TokenType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
TokenType type;
|
||||||
|
char* value;
|
||||||
|
} Token;
|
||||||
|
|
||||||
|
List* GenerateTokensFromFile(const char*);
|
||||||
|
|
||||||
|
#endif
|
||||||
+1
-26
@@ -6,31 +6,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
typedef enum {
|
char* SplitOnBasicGrammar(char*);
|
||||||
TK_Add,
|
|
||||||
TK_Sub,
|
|
||||||
TK_Mul,
|
|
||||||
TK_Div,
|
|
||||||
TK_Power,
|
|
||||||
TK_LParam,
|
|
||||||
TK_RParam,
|
|
||||||
TK_Comma,
|
|
||||||
TK_LBracket,
|
|
||||||
TK_RBracket,
|
|
||||||
TK_Colon,
|
|
||||||
TK_Text,
|
|
||||||
TK_Number,
|
|
||||||
TK_Hex,
|
|
||||||
TK_Opcode,
|
|
||||||
TK_Register,
|
|
||||||
TK_Invalid
|
|
||||||
} TokenType;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
TokenType type;
|
|
||||||
char* value;
|
|
||||||
} Token;
|
|
||||||
|
|
||||||
List* TokenizeString(const char *);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+138
@@ -0,0 +1,138 @@
|
|||||||
|
#include "../includes/lexer.h"
|
||||||
|
#include "../includes/opcodes.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
Token* CreateToken(TokenType, char*);
|
||||||
|
Token* GetNextToken(char *);
|
||||||
|
int TokenIsNumeric(const char*, int *);
|
||||||
|
|
||||||
|
List* GenerateTokensFromFile(const char* file_path) {
|
||||||
|
FILE *file;
|
||||||
|
int line_count = 1;
|
||||||
|
char* line = NULL;
|
||||||
|
size_t len = 0;
|
||||||
|
ssize_t bytes_read;
|
||||||
|
|
||||||
|
file = fopen(file_path, "r");
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
printf("Failed to open '%s' for reading.\n", file_path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//char* context;// = malloc(sizeof(char *));
|
||||||
|
|
||||||
|
while((bytes_read = getline(&line, &len, file)) != -1) {
|
||||||
|
Token* token = GetNextToken(line);
|
||||||
|
|
||||||
|
while (token) {
|
||||||
|
|
||||||
|
if (!token) break;
|
||||||
|
|
||||||
|
if(token->type == TK_Number) {
|
||||||
|
printf("Found number: '%s'\n", token->value);
|
||||||
|
}
|
||||||
|
else if (token->type == TK_Hex) {
|
||||||
|
printf("Hex number: '%s'\n", token->value);
|
||||||
|
}
|
||||||
|
else if (token->type == TK_Opcode) {
|
||||||
|
printf("Found op: '%s'\n", token->value);
|
||||||
|
}
|
||||||
|
else if (token->type == TK_Register) {
|
||||||
|
printf("Found Reg: '%s'\n", token->value);
|
||||||
|
}
|
||||||
|
else if(token->type == TK_Label) {
|
||||||
|
printf("Label: '%s'\n", token->value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Found text: '%s'\n", token->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(token->value);
|
||||||
|
free(token);
|
||||||
|
|
||||||
|
token = GetNextToken(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
line_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
if (line) free(line);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Token* GetNextToken(char *string) {
|
||||||
|
char* string_token = SplitOnBasicGrammar(string);
|
||||||
|
int base = 0;
|
||||||
|
|
||||||
|
while (strlen(string_token) != 0) {
|
||||||
|
|
||||||
|
if (TokenIsNumeric(string_token, &base)) {
|
||||||
|
if (base == 10) return CreateToken(TK_Number, string_token);
|
||||||
|
if (base == 16) return CreateToken(TK_Hex, string_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
//if (strcmp(string_token, ":") == 0) return CreateToken(TK_Colon, string_token);
|
||||||
|
//if (strcmp(string_token, ",") == 0) return CreateToken(TK_Comma, string_token);
|
||||||
|
if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token);
|
||||||
|
if (IsRegister(string_token)) return CreateToken(TK_Register, string_token);
|
||||||
|
|
||||||
|
char* next = SplitOnBasicGrammar(NULL);
|
||||||
|
|
||||||
|
if (next && strcmp(next, ":") == 0) {
|
||||||
|
free(next);
|
||||||
|
return CreateToken(TK_Label, string_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next) free(next);
|
||||||
|
|
||||||
|
return CreateToken(TK_String, string_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Token* CreateToken(TokenType type, char* value) {
|
||||||
|
Token* token = calloc(1, sizeof(Token));
|
||||||
|
|
||||||
|
if (!token) return NULL;
|
||||||
|
|
||||||
|
token->type = type;
|
||||||
|
token->value = value;
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TokenIsNumeric(const char* token, int *base) {
|
||||||
|
*base = 0;
|
||||||
|
|
||||||
|
if (!token) return 0;
|
||||||
|
|
||||||
|
unsigned long length = strlen(token);
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
if (length == 0) return 0;
|
||||||
|
|
||||||
|
*base = 10;
|
||||||
|
|
||||||
|
if (length > 2) {
|
||||||
|
if (token[0] == '0' && token[1] == 'x') {
|
||||||
|
*base = 16;
|
||||||
|
i = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(; i < length; i++) {
|
||||||
|
if (!isdigit(token[i])) {
|
||||||
|
if (*base == 16 && token[i] >= 'A' && token[i] <= 'F') continue;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
+2
-2
@@ -2,7 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "../includes/list.h"
|
#include "../includes/list.h"
|
||||||
#include "../includes/tokenizer.h"
|
#include "../includes/lexer.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *opcode;
|
char *opcode;
|
||||||
@@ -22,7 +22,7 @@ int main(int argc, char* args[]) {
|
|||||||
}
|
}
|
||||||
// char* test = "user_id";
|
// char* test = "user_id";
|
||||||
// printf("Key for '%s' is %d for size %d\n", test, GetKeyIndex(test, HASHTABLEDEFAULTSIZE), HASHTABLEDEFAULTSIZE);
|
// printf("Key for '%s' is %d for size %d\n", test, GetKeyIndex(test, HASHTABLEDEFAULTSIZE), HASHTABLEDEFAULTSIZE);
|
||||||
TokenizeString(args[1]);
|
GenerateTokensFromFile(args[1]);
|
||||||
|
|
||||||
// for (int i = 1; i < argc; i++) {
|
// for (int i = 1; i < argc; i++) {
|
||||||
// input_files[i - 1] = args[i];
|
// input_files[i - 1] = args[i];
|
||||||
|
|||||||
+25
-157
@@ -1,136 +1,4 @@
|
|||||||
#include "../includes/tokenizer.h"
|
#include "../includes/tokenizer.h"
|
||||||
#include "../includes/opcodes.h"
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
Token* GetNextToken(char *);
|
|
||||||
TokenType GetOperatorType(char);
|
|
||||||
char* SplitOnBasicGrammar(char*);
|
|
||||||
int TokenIsNumeric(const char*, int *);
|
|
||||||
Token* CreateToken(TokenType, char*);
|
|
||||||
|
|
||||||
List* TokenizeString(const char *file_path) {
|
|
||||||
FILE *file;
|
|
||||||
char* line = NULL;
|
|
||||||
size_t len = 0;
|
|
||||||
ssize_t bytes_read;
|
|
||||||
|
|
||||||
file = fopen(file_path, "r");
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
printf("Failed to open '%s' for reading.\n", file_path);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
while((bytes_read = getline(&line, &len, file)) != -1) {
|
|
||||||
Token* token = GetNextToken(line);
|
|
||||||
|
|
||||||
while (token) {
|
|
||||||
if (!token) break;
|
|
||||||
|
|
||||||
if(token->type == TK_Number) {
|
|
||||||
printf("Found number: '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
else if (token->type == TK_Hex) {
|
|
||||||
printf("Hex number: '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
else if(token->type == TK_Colon) {
|
|
||||||
printf("Colon found\n");
|
|
||||||
}
|
|
||||||
else if (token->type == TK_Comma) {
|
|
||||||
printf("Comma\n");
|
|
||||||
}
|
|
||||||
else if (token->type == TK_Opcode) {
|
|
||||||
printf("Found op: '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
else if (token->type == TK_Register) {
|
|
||||||
printf("Found Reg: '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf("Found text: '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(token->value);
|
|
||||||
free(token);
|
|
||||||
|
|
||||||
token = GetNextToken(NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
if (line) free(line);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Token* GetNextToken(char *string) {
|
|
||||||
char* string_token = SplitOnBasicGrammar(string);
|
|
||||||
int base = 0;
|
|
||||||
|
|
||||||
while (strlen(string_token) != 0) {
|
|
||||||
|
|
||||||
if (TokenIsNumeric(string_token, &base)) {
|
|
||||||
if (base == 10) return CreateToken(TK_Number, string_token);
|
|
||||||
if (base == 16) return CreateToken(TK_Hex, string_token);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(string_token, ":") == 0) return CreateToken(TK_Colon, string_token);
|
|
||||||
if (strcmp(string_token, ",") == 0) return CreateToken(TK_Comma, string_token);
|
|
||||||
if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token);
|
|
||||||
if (IsRegister(string_token)) return CreateToken(TK_Register, string_token);
|
|
||||||
|
|
||||||
return CreateToken(TK_Text, string_token);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(string_token);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Token* CreateToken(TokenType type, char* value) {
|
|
||||||
Token* token = calloc(1, sizeof(Token));
|
|
||||||
|
|
||||||
if (!token) return NULL;
|
|
||||||
|
|
||||||
token->type = type;
|
|
||||||
token->value = value;
|
|
||||||
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TokenIsNumeric(const char* token, int *base) {
|
|
||||||
*base = 0;
|
|
||||||
|
|
||||||
if (!token) return 0;
|
|
||||||
|
|
||||||
unsigned long length = strlen(token);
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
if (length == 0) return 0;
|
|
||||||
|
|
||||||
*base = 10;
|
|
||||||
|
|
||||||
if (length > 2) {
|
|
||||||
if (token[0] == '0' && token[1] == 'x') {
|
|
||||||
*base = 16;
|
|
||||||
i = 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(; i < length; i++) {
|
|
||||||
if (!isdigit(token[i])) {
|
|
||||||
if (*base == 16 && token[i] >= 'A' && token[i] <= 'F') continue;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* SplitOnBasicGrammar(char* line) {
|
char* SplitOnBasicGrammar(char* line) {
|
||||||
static char* string;
|
static char* string;
|
||||||
@@ -195,29 +63,29 @@ char* SplitOnBasicGrammar(char* line) {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenType GetOperatorType(char c) {
|
// TokenType GetOperatorType(char c) {
|
||||||
switch (c){
|
// switch (c){
|
||||||
case '+':
|
// case '+':
|
||||||
return TK_Add;
|
// return TK_Add;
|
||||||
case '-':
|
// case '-':
|
||||||
return TK_Sub;
|
// return TK_Sub;
|
||||||
case '*':
|
// case '*':
|
||||||
return TK_Mul;
|
// return TK_Mul;
|
||||||
case '/':
|
// case '/':
|
||||||
return TK_Div;
|
// return TK_Div;
|
||||||
case '^':
|
// case '^':
|
||||||
return TK_Power;
|
// return TK_Power;
|
||||||
case ':':
|
// case ':':
|
||||||
return TK_Colon;
|
// return TK_Colon;
|
||||||
case '(':
|
// case '(':
|
||||||
return TK_LParam;
|
// return TK_LParam;
|
||||||
case ')':
|
// case ')':
|
||||||
return TK_RParam;
|
// return TK_RParam;
|
||||||
case '[':
|
// case '[':
|
||||||
return TK_LBracket;
|
// return TK_LBracket;
|
||||||
case ']':
|
// case ']':
|
||||||
return TK_RBracket;
|
// return TK_RBracket;
|
||||||
};
|
// };
|
||||||
|
|
||||||
return TK_Invalid;
|
// return TK_Invalid;
|
||||||
}
|
// }
|
||||||
Reference in New Issue
Block a user