Created the list structure for the tokens to be stored and some helper functions.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "scanner.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* source_code;
|
||||
@@ -11,14 +13,15 @@ int line = 1;
|
||||
char Advance(void);
|
||||
int IsAtend(void);
|
||||
void ScanToken(void);
|
||||
TokenList* CreateList(void);
|
||||
|
||||
void ScanTokens(const char* source) {
|
||||
if (!source) return;
|
||||
TokenList* ScanTokens(const char* source) {
|
||||
if (!source) return NULL;
|
||||
|
||||
source_code = source;
|
||||
length = strlen(source);
|
||||
|
||||
if (length == 0) return;
|
||||
if (length == 0) return NULL;
|
||||
|
||||
while(!IsAtend()) {
|
||||
start = current;
|
||||
@@ -28,6 +31,70 @@ void ScanTokens(const char* source) {
|
||||
//Add EOF token and return list once that's set up.
|
||||
}
|
||||
|
||||
TokenList* CreateList() {
|
||||
TokenList* list = calloc(1, sizeof(TokenList));
|
||||
|
||||
if (!list) {
|
||||
fprintf(stderr, "Failed to calloc TokenList.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list->tokens = calloc(DEFAULT_TOKENLIST_SIZE, sizeof(Token*));
|
||||
|
||||
if (!list->tokens) {
|
||||
free(list);
|
||||
fprintf(stderr, "Failed to calloc tokens.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list->capacity = DEFAULT_TOKENLIST_SIZE;
|
||||
list->size = 0;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void DestroyTokenList(TokenList* list) {
|
||||
if (!list) return;
|
||||
|
||||
for(int i = 0; i < list->size; i++) {
|
||||
free(list->tokens[i]->lexeme);
|
||||
free(list->tokens[i]);
|
||||
}
|
||||
|
||||
free(list->tokens);
|
||||
free(list);
|
||||
}
|
||||
|
||||
int AddTokenToList(TokenType type, char* lexeme, TokenList* tokens) {
|
||||
if (!tokens) return 0;
|
||||
Token* token = calloc(1, sizeof(Token));
|
||||
|
||||
if (!token) {
|
||||
fprintf(stderr, "Failed to calloc memory for new Token.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
token->lexeme = lexeme;
|
||||
token->type = type;
|
||||
|
||||
if ((tokens->size + 1) > tokens->capacity) {
|
||||
void* new_ptr = realloc(tokens->tokens, sizeof(Token*) * tokens->capacity * 2);//calloc(tokens->capacity * 2, sizeof(Token*));
|
||||
|
||||
if (!new_ptr) {
|
||||
fprintf(stderr, "Failed to realloc TokenList to size %d.\n", tokens->capacity * 2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
tokens->tokens = new_ptr;
|
||||
tokens->capacity = tokens->capacity * 2;
|
||||
}
|
||||
|
||||
tokens->tokens[tokens->size] = token;
|
||||
tokens->size++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ScanToken() {
|
||||
char c = Advance();
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef SCANNER_H
|
||||
#define SCANNER_H
|
||||
|
||||
#define DEFAULT_TOKENLIST_SIZE 32
|
||||
|
||||
typedef enum {
|
||||
//Single-character tokens
|
||||
LParen, RParen,
|
||||
@@ -27,6 +29,19 @@ typedef enum {
|
||||
EndOF
|
||||
} TokenType;
|
||||
|
||||
void ScanTokens(const char*);
|
||||
typedef struct {
|
||||
TokenType type;
|
||||
char* lexeme;
|
||||
int line;
|
||||
} Token;
|
||||
|
||||
typedef struct {
|
||||
Token** tokens;
|
||||
int capacity;
|
||||
int size;
|
||||
} TokenList;
|
||||
|
||||
TokenList* ScanTokens(const char*);
|
||||
void DestroyTokenList(TokenList*);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user