Skip to content

Commit

Permalink
added utility files
Browse files Browse the repository at this point in the history
  • Loading branch information
sajibsrs committed Feb 2, 2025
1 parent add736f commit 9576d02
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 66 deletions.
39 changes: 7 additions & 32 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "src/utils.h"
#include "src/lexer.h"
#include "src/parser.h"

void print_tree(const Node *node, int depth) {
if (!node) {
printf("NULL node at depth %d\n", depth);
return;
}

// indentation based on depth
for (int i = 0; i < depth; i++) {
printf("- "); // Two spaces for each level
}

// print node information
printf(
" %-18s %-26s (%d child%s)\n", node->str ? node->str : "NULL", ntypestr[node->type],
node->count,
node->count == 1 ? "" : "ren" // proper grammar for singular/plural
);

// recursively print children, if any
for (int i = 0; i < node->count; i++) {
print_tree(node->nodes[i], depth + 1);
}
}

int main() {
clock_t stime = clock();

const char *src = "../../source.cx";
TokenList *list = scan(src);

// print_tokenlist(list);
// print_tokenlist(list); // Print parsed tokens

Parser *prs = make_parser(list);
Node *node = parse_prog(prs);

// print_tree(node, 0);

// cleanup
purge_parser(prs);
purge_tokenlist(list);
// print_ast(node, 0); // Prints AST

clock_t etime = clock();
double ttime = ((double)(etime - stime)) / CLOCKS_PER_SEC * 1000;
printf("Total time: %f ms\n", ttime);

// cleanup
purge_parser(prs);
purge_tokenlist(list);

return 0;
}
37 changes: 3 additions & 34 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <ctype.h>

#include "utils.h"
#include "lexer.h"

// Token type to token string lookup table.
Expand Down Expand Up @@ -141,21 +142,6 @@ const char *ttypestr[] = {

#define HASH_SIZE 256

/**
* @brief Returns hash for token string (Fowler–Noll–Vo hash).
* @param str Token string.
* @return
*/
unsigned int hashfnv(const char *str) {
unsigned int hash = 2166136261u; // FNV offset basis
while (*str) {
hash ^= (unsigned char)(*str); // XOR with byte
hash *= 16777619u; // FNV prime
str++;
}
return hash % HASH_SIZE;
}

// keyword mapper
KWMap *kwmap[HASH_SIZE] = {NULL};

Expand Down Expand Up @@ -238,7 +224,7 @@ void init_kwmap() {
* @param token Token string.
*/
void kwmap_add(TokenType type, const char *token) {
unsigned int idx = hashfnv(token);
unsigned int idx = hashfnv(token, HASH_SIZE);
KWMap *entry = malloc(sizeof(KWMap));
if (!entry) {
perror("Memory allocation failed");
Expand All @@ -260,7 +246,7 @@ void kwmap_add(TokenType type, const char *token) {
* @return
*/
KWMap *search_kwmap(const char *key) {
unsigned int idx = hashfnv(key);
unsigned int idx = hashfnv(key, HASH_SIZE);
KWMap *current = kwmap[idx];

while (current) {
Expand Down Expand Up @@ -920,23 +906,6 @@ void purge_lexer(Lexer *lexer) {
free(lexer);
}

/**
* @brief Print formatted token to the terminal.
* @param list
*/
void print_tokenlist(const TokenList *list) {
printf("Scanned %d tokens:\n\n", list->count);

Token token;
for (int i = 0; i < list->count; i++) {
token = list->tokens[i];
printf(
"%-16s %-10s typ:%-4d lin:%-4d col:%d\n", //
ttypestr[token.type], token.str, token.type, token.line, token.column
);
}
}

/**
* @brief Cleanup allocated memory from `tokens`.
* @param list
Expand Down
67 changes: 67 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "stdio.h"
#include "utils.h"

/**
* @brief Returns hash for token string (Fowler–Noll–Vo hash).
* @param str Token string.
* @param size Hash map size.
* @return
*/
unsigned int hashfnv(const char *str, const int size) {
unsigned int hash = 2166136261u; // FNV offset basis
while (*str) {
hash ^= (unsigned char)(*str); // XOR with byte
hash *= 16777619u; // FNV prime
str++;
}

return hash % size;
}

/**
* @brief Print abstract syntax tree (AST).
* @param node Node to start with.
* @param depth Initial depth.
*/
void print_ast(const Node *node, int depth) {
if (!node) {
printf("NULL node at depth %d\n", depth);

return;
}

// Indentation based on depth
for (int i = 0; i < depth; i++) {
printf("- "); // Two spaces for each level
}

// Print node information
printf(
" %-18s %-26s (%d child%s)\n", node->str ? node->str : "NULL", ntypestr[node->type],
node->count,
node->count == 1 ? "" : "ren" // Singular/plural
);

// Recursively print children
for (int i = 0; i < node->count; i++) {
print_ast(node->nodes[i], depth + 1);
}
}

/**
* @brief Print formatted token to the terminal.
* @param list
*/
void print_tokenlist(const TokenList *list) {
printf("Scanned %d tokens:\n\n", list->count);

Token token;

for (int i = 0; i < list->count; i++) {
token = list->tokens[i];
printf(
"%-16s %-10s typ:%-4d lin:%-4d col:%d\n", //
ttypestr[token.type], token.str, token.type, token.line, token.column
);
}
}
11 changes: 11 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _UTIL_H_
#define _UTIL_H_

#include "lexer.h"
#include "parser.h"

unsigned int hashfnv(const char *str, const int size);
void print_ast(const Node *node, int depth);
void print_tokenlist(const TokenList *list);

#endif

0 comments on commit 9576d02

Please sign in to comment.