168 lines
3.8 KiB
C
168 lines
3.8 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "main.h"
|
|
#include "token.h"
|
|
#include "lexer.h"
|
|
#include "node.h"
|
|
|
|
void writeTokenFile(Token* startToken);
|
|
|
|
int
|
|
main(int argc, const char** argv)
|
|
{
|
|
|
|
/*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]);
|
|
if (l == NULL) {
|
|
printf("lexer borked\n");
|
|
return 3;
|
|
}
|
|
|
|
Token* current = NULL;
|
|
Token* firstToken = NULL;
|
|
|
|
TokenType tt;
|
|
do
|
|
{
|
|
Token* t = NextToken(l);
|
|
tt = t->type;
|
|
if (current != NULL) {
|
|
current->next = t;
|
|
}
|
|
current = t;
|
|
if (firstToken == NULL) {
|
|
firstToken = t;
|
|
}
|
|
}
|
|
while(tt != TT_EOF);
|
|
|
|
FreeLexer(l);
|
|
|
|
writeTokenFile(firstToken);
|
|
Node* firstNode = ParseNodes(firstToken);
|
|
Node* node = firstNode;
|
|
|
|
printf("nodes:\n");
|
|
while(node != NULL)
|
|
{
|
|
switch (node->type)
|
|
{
|
|
case NT_Header1:
|
|
case NT_Header2:
|
|
case NT_Header3:
|
|
case NT_Header4:
|
|
{
|
|
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);
|
|
}
|
|
break;
|
|
|
|
case NT_Paragraph:
|
|
{
|
|
ParagraphNode* pnode = (ParagraphNode*)node;
|
|
printf("{ParagraphNode ptype:%s}\n", ParagraphTypeString(pnode->ptype));
|
|
Token* content = pnode->content;
|
|
while(content != NULL)
|
|
{
|
|
if (content->type == TT_WHITESPACE)
|
|
{
|
|
printf(" ");
|
|
}
|
|
else
|
|
{
|
|
printf("%s", content->literal);
|
|
}
|
|
|
|
content = content->next;
|
|
}
|
|
printf("\n");
|
|
|
|
}
|
|
break;
|
|
|
|
default:
|
|
printf("%s\n", NodeTypeString(node->type));
|
|
}
|
|
|
|
node = node->next;
|
|
}
|
|
|
|
node = firstNode;
|
|
firstNode = NULL;
|
|
|
|
while ((node = FreeNode(node)) != NULL);
|
|
|
|
if (node != NULL)
|
|
printf("last node != NULL\n");
|
|
|
|
if (firstNode != NULL)
|
|
{
|
|
printf("firstNode != NULL\n");
|
|
printf("%s\n", NodeTypeString(firstNode->type));
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
writeTokenFile(Token* startToken)
|
|
{
|
|
int count;
|
|
FILE* fp = fopen("tokens.txt", "w");
|
|
if (fp == NULL)
|
|
{
|
|
printf("unable to open output.txt\n");
|
|
return;
|
|
}
|
|
|
|
Token* current = startToken;
|
|
for(count = 0; current != NULL; count++) {
|
|
if (count == 0 && current == NULL)
|
|
{
|
|
printf("first token null\n");
|
|
break;
|
|
}
|
|
|
|
/*printf("writeTokenFile(): %s\n", TokenString(current->token));*/
|
|
fprintf(fp, "%s\n", TokenString(current));
|
|
current = current->next;
|
|
}
|
|
fclose(fp);
|
|
|
|
if (count == 0) {
|
|
printf("nothing written to file!\n");
|
|
}
|
|
|
|
printf("Token count: %d\n", count);
|
|
}
|