feat: implement lexer+parser for example program

This commit is contained in:
samsonium
2026-10-09 15:30:50 +03:00
parent a46f79d538
commit 45206e3d07
12 changed files with 776 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
#include <compiler/fzl_reader.h>
#include <compiler/fzl_parser.h>
#include <compiler/fzl_debug.h>
int main(int argc, char** argv) {
if (argc < 2)
{
printf("No files provided >:(\n");
return -1;
}
else if (argc > 2)
{
printf("warn: flags are not supported yet\n");
}
printf("> analyze\n-> reading file...");
char source[8192];
FILE *fd = fopen(argv[argc - 1], "r");
if (!fd)
{
printf("failed to open file '%s'\n", argv[argc-1]);
return -1;
}
fseek(fd, 0L, SEEK_END);
size_t fsz = ftell(fd);
rewind(fd);
fread(source, fsz, 1, fd);
fclose(fd);
printf("ok!\n");
printf("-> tokenizing...");
fzl_reader_t *rd = fzl_mkreader(source);
int result = fzl_read(rd);
if (result != 0)
{
printf("error: %s\n", fzl_reader_geterror());
return -1;
}
printf("ok!\n");
fzl_reader_ll_t *entry = rd->first;
printf("--> structure: ");
while (entry)
{
if (entry->token)
{
if (entry->token->store_value)
printf("<TOK:%d:%s>", entry->token->type, (char*)entry->token->value);
else
printf("<TOK:%d>", entry->token->type);
}
entry = entry->next;
}
printf("\n");
printf("-> parsing...");
fzl_parser_t *parser = fzl_mkparser(rd->first);
result = fzl_parse(parser);
if (result != 0)
{
printf("error: %s\n", fzl_parser_geterror());
return -1;
}
printf("ok!\n");
fzl_debug_print_ast(parser->root);
fzl_freereader(rd);
fzl_freeparser(parser);
return 0;
}