2021-07-14 08:15:46 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "lexer.h"
|
|
|
|
#include "node.h"
|
|
|
|
|
|
|
|
void writeTokenFile(TokenList* tl);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, const char** argv)
|
|
|
|
{
|
2023-10-15 15:30:59 -07:00
|
|
|
|
|
|
|
/*int i;
|
|
|
|
for(i = 0; i < argc; i++) {
|
|
|
|
printf("[%d:%d] %s\n", i, argc, argv[i]);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
if (argc <= 1) {
|
|
|
|
printf("Missing input file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
printf("Too many arguments\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Lexer* l = NewLexer(argv[1]);
|
2021-07-14 08:15:46 -07:00
|
|
|
TokenList* current = malloc(sizeof(TokenList));
|
|
|
|
TokenList* tl = current;//= malloc(sizeof(TokenList));
|
|
|
|
current->token = NULL;
|
2023-10-15 15:30:59 -07:00
|
|
|
current->next = NULL;
|
2021-07-14 08:15:46 -07:00
|
|
|
|
|
|
|
TokenType tt;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Token* t = NextToken(l);
|
|
|
|
tt = t->type;
|
|
|
|
current = TokenListAdd(current, t);
|
|
|
|
}
|
|
|
|
while(tt != TT_EOF);
|
|
|
|
|
|
|
|
writeTokenFile(tl);
|
2023-10-20 17:19:34 -07:00
|
|
|
Node* node = ParseNodes(tl);
|
2023-10-15 15:30:59 -07:00
|
|
|
|
|
|
|
printf("nodes:\n");
|
|
|
|
while(node != NULL)
|
|
|
|
{
|
|
|
|
/*PrintNodeType(node->type);*/
|
|
|
|
|
|
|
|
switch (node->type) {
|
|
|
|
case NT_Header1:
|
|
|
|
case NT_Header2:
|
|
|
|
case NT_Header3:
|
|
|
|
case NT_Header4:
|
2023-10-15 17:55:12 -07:00
|
|
|
{
|
|
|
|
HeaderNode* hnode = (HeaderNode*)node;
|
|
|
|
printf("{HeaderNode type:%s text:%s}\n", NodeTypeString(hnode->type), hnode->rawText);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NT_BlockCode:
|
|
|
|
{
|
|
|
|
CodeBlockNode* cnode = (CodeBlockNode*)node;
|
|
|
|
printf("{CodeBlockNode text:%s}\n", cnode->rawText);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NT_Error:
|
|
|
|
{
|
|
|
|
ErrorNode* enode = (ErrorNode*)node;
|
|
|
|
printf("{ErrorNode error:%s}\n", enode->error);
|
|
|
|
}
|
2023-10-15 15:30:59 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("%s\n", NodeTypeString(node->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
}
|
2021-07-14 08:15:46 -07:00
|
|
|
|
|
|
|
printf("rawLen: %d position: %d readPosition: %d ch: %c line: %d column: %d\n",
|
|
|
|
l->rawLen,
|
|
|
|
l->position,
|
|
|
|
l->readPosition,
|
|
|
|
l->ch,
|
|
|
|
l->line,
|
|
|
|
l->column
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
writeTokenFile(TokenList* tl)
|
|
|
|
{
|
2023-10-15 15:30:59 -07:00
|
|
|
printf("writeTokenFile() start\n");
|
2021-07-14 08:15:46 -07:00
|
|
|
int count;
|
|
|
|
FILE* fp = fopen("tokens.txt", "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
printf("unable to open output.txt\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenList* current = tl;
|
2023-10-15 15:30:59 -07:00
|
|
|
for(count = 0; current != NULL; count++) {
|
2021-07-14 08:15:46 -07:00
|
|
|
if (count == 0 && current->token == NULL)
|
|
|
|
{
|
|
|
|
printf("first token null\n");
|
2023-10-15 15:30:59 -07:00
|
|
|
break;
|
2021-07-14 08:15:46 -07:00
|
|
|
}
|
2023-10-15 15:30:59 -07:00
|
|
|
|
|
|
|
/*printf("writeTokenFile(): %s\n", TokenString(current->token));*/
|
2021-07-14 08:15:46 -07:00
|
|
|
fprintf(fp, "%s\n", TokenString(current->token));
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2023-10-15 15:30:59 -07:00
|
|
|
if (count == 0) {
|
|
|
|
printf("nothing written to file!\n");
|
|
|
|
}
|
|
|
|
|
2021-07-14 08:15:46 -07:00
|
|
|
printf("Token count: %d\n", count);
|
|
|
|
}
|