-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |