#include #include #include #include "Parser.h" #include "AST/Function.h" #include "AST/Namespace.h" #include "AST/Structure.h" static unsigned int count = 0; void print_dot(const char * label) { printf("I_%u [label=\"%s\"] ;\n", count++, label); } void print_dot(int n) { printf("I_%u [label=\"%d\"] ;\n", count++, n); } void print_dot(unsigned int n) { printf("I_%u [label=\"%u\"] ;\n", count++, n); } void print_dot(long n) { printf("I_%u [label=\"%ld\"] ;\n", count++, n); } void print_dot(unsigned long n) { printf("I_%u [label=\"%lu\"] ;\n", count++, n); } void print_dot(float n) { printf("I_%u [label=\"%f\"] ;\n", count++, n); } void print_dot(double n) { printf("I_%u [label=\"%f\"] ;\n", count++, n); } void print_identifier(SL::Identifier * pNode) { print_dot(pNode->getValue()); } void print_number(SL::Number * pNode) { switch(pNode->getFormat()) { case SL::Number::INT: { print_dot(pNode->getValueAsInt()); } break; case SL::Number::UINT: { print_dot(pNode->getValueAsUInt()); } break; case SL::Number::LONG: { print_dot(pNode->getValueAsLong()); } break; case SL::Number::ULONG: { print_dot(pNode->getValueAsULong()); } break; case SL::Number::FLOAT: { print_dot(pNode->getValueAsFloat()); } break; case SL::Number::DOUBLE: { print_dot(pNode->getValueAsDouble()); } break; } } void print_keyword(SL::Keyword * pNode) { switch(pNode->getKeyword()) { case SL::Keyword::NAMESPACE: { print_dot("namespace"); } break; case SL::Keyword::CLASS: { print_dot("class"); } break; case SL::Keyword::STRUCT: { print_dot("struct"); } break; case SL::Keyword::ENUM: { print_dot("enum"); } break; case SL::Keyword::IF: { print_dot("if"); } break; case SL::Keyword::FOR: { print_dot("for"); } break; case SL::Keyword::WHILE: { print_dot("while"); } break; case SL::Keyword::DO: { print_dot("do"); } break; } } void print_operator(SL::Operator * pNode) { switch(pNode->getOperator()) { case SL::Operator::PLUS: { print_dot("+"); } break; case SL::Operator::MINUS: { print_dot("-"); } break; case SL::Operator::MUL: { print_dot("*"); } break; case SL::Operator::DIV: { print_dot("/"); } break; case SL::Operator::MODULO: { print_dot("%"); } break; case SL::Operator::INCREMENT: { print_dot("++"); } break; case SL::Operator::EQUAL: { print_dot("="); } break; case SL::Operator::DECREMENT: { print_dot("--"); } break; case SL::Operator::EQ: { print_dot("=="); } break; case SL::Operator::NEQ: { print_dot("!="); } break; } } void print_recurse(const AST::Node * pCurrentNode) { if (nullptr == pCurrentNode) return; // should be an assert !!! switch (pCurrentNode->getType()) { case AST::Node::NAMESPACE: { printf(" (ns %s", static_cast(pCurrentNode)->getName().c_str()); } break; case AST::Node::STRUCTURE: { printf(" (struct %s", static_cast(pCurrentNode)->getName().c_str()); } break; case AST::Node::FUNCTION: { printf(" (fn %s %s", static_cast(pCurrentNode)->getReturnType().c_str(), static_cast(pCurrentNode)->getName().c_str()); } break; } for (const AST::Node * pNode : pCurrentNode->getNodes()) { print_recurse(pNode); } printf(")"); } void print_ast(const std::vector & aNodes) { for (const AST::Node * pNode : aNodes) { print_recurse(pNode); } printf("\n"); } int main(int argc, char ** argv) { if (2 != argc) { printf("Usage: ./compiler \n"); return(1); } FILE * f = fopen(argv[1], "r"); // obtain file size fseek(f , 0 , SEEK_END); long lSize = ftell(f); rewind(f); char * source = (char *)malloc(sizeof(char)*lSize); size_t result = fread(source, sizeof(char), lSize, f); assert(result == sizeof(char)*lSize); fclose(f); Parser p(source); p.buildAST(); print_ast(p.getAST()); #if 0 printf("graph NS_GLOBAL {\n"); for (SL::Token * pNode : p.getAST_v1()) { assert(nullptr != pNode); switch(pNode->getType()) { case SL::Token::NUMBER: { print_number(static_cast(pNode)); } break; case SL::Token::IDENTIFIER: { print_identifier(static_cast(pNode)); } break; case SL::Token::KEYWORD: { print_keyword(static_cast(pNode)); } break; case SL::Token::OPERATOR: { print_operator(static_cast(pNode)); } break; case SL::Token::SEPARATOR: { print_dot("SEPARATOR "); } break; case SL::Token::ENDMARK: { //assert(false); } break; default: { assert(false); } } } printf("} \n"); #endif return(0); }