// File: /workspace/github_c_makefile_star2_O1/Gaurang18[P]Compiler-Project-BITS-Pilani/driver.c
// Function: main
int main (int argc, char* argv[]){
	
	int input;
	int a,b;
	int flagger =0; 
	int tyu = 0;
	int parsetreenodes[1] = {0};
    int astnodes[1] = {0};

	if(argc != 3){
		printf("Please enter according to the correct format\n");
		return 0;
	}
	printf("\nLevel 4: All Modules \n\tAbstract Syntax Tree\n\tsymboltable\n\tsemantic checking\n\ttypechecking\n\tcode generation\n\tSUCCESSFULLY IMPLEMENTED\n\n");
	printf("\n1 : for printing the token list \n2 : for parsing to verify the syntactic correctness of the input source code and produce parse tree on console\n3 : for creating the abstract syntax tree and printing it on console\n4 : for displaying the amount of allocated memory and number of nodes to each of parse tree and abstract syntax tree\n5 : for printing the Symbol Table\n6 : Type Checking and Semantic Verification\n7 : Code Generation\n0 : Exit\n\n");
	
	FILE* fp1 = fopen(argv[1],"a");
	FILE* fp2 = fopen(argv[2],"w");

	FILE *semantic = fopen("Type&SemanticErrors.txt", "w+");

	if(fp1==NULL)
    {
        printf("File not found\n");
        return 0;
    }
    if(fp2==NULL)
    {
        printf("File not found\n");
        return 0;
    }

	fprintf(fp1,"%s","\r\n$");
	fclose(fp1);
	createsymboltable();
	FILE* fp = fopen(argv[1], "r");
	int symcount = 0;
	parsetree head = NULL;

		scanf("%d", &input);

	do{

			if(input==1){
					rewind(fp);
					fseek( fp, 0, SEEK_SET);
					fp = getstream(fp);
				    
					node* Lex = getNextToken();					//Call method in lexer
				    int i = 1;
				    int j = 0;
					while(strcmp(Lex->tk->value,"$")!=0){
						if(strcmp(Lex->tk->token,"ERROR") == 0){
							j++;
						}else{
				        	printf("%d. token = %s, value = %s, line = %d\n", i++,Lex->tk->token, Lex->tk->value, Lex->tk->line);	//print tokens to console
				   		}
				        free(Lex);
				        node* Lex = getNextToken();
				    }
				    printf("\nTotal Tokens :- %d\n",i+j);
				    printf("Valid Tokens Found:- %d\n",i);
				    printf("Error Found:- %d\n",j);
			}


			if(input==2){
				int firsts[51][11] = {-1};
				int follows[51][15] = {-1};
				ComputeFirstAndFollowSets(G, firsts, follows);
				Initialise_table(parse_table);
    			parsing_table(parse_table,G);
    			initialise_stack();
       			head = parse_Source_Code(argv[1]);
       			countNodes(head, parsetreenodes);
       			populateLexemes(head, argv[1]);
			printf("\nTREE TRAVERSAL:  PRE ORDER\n");
       			printParseTree(head);
       			flagger =2;
			}

			if(input == 3){
				if(head == NULL || flagger < 2){
					printf("Please run parser first\n");
				}else{
				createAbstractSyntaxTree(head);
       			countNodes(head, astnodes);
			printf("\nTREE TRAVERSAL:  PRE ORDER\n");
       			printParseTree(head);
			flagger = 3;
				}
			}

			if(input== 4){
				if(flagger <3){
					printf("Please create AST\n");
				}else{
				a = parsetreenodes[0]*sizeof(struct Parse_Tree);
				b = astnodes[0]*sizeof(struct Parse_Tree)/2;
				printf("\nParse tree\t\tNumber of nodes = %d Allocated Memory = %d Bytes\n",parsetreenodes[0],parsetreenodes[0]*sizeof(struct Parse_Tree));
				printf("Abstract Syntax tree\tNumber of nodes = %d Allocated Memory = %d Bytes\n",astnodes[0]/2,astnodes[0]*sizeof(struct Parse_Tree)/2);
				float percentage = ((float)((a-b)*100)/(float)(a));
				printf("AST Compression percentage = %.2f\n",percentage);
				}	
			}


			if(input == 5){
				if(flagger <3){
					printf("Please create AST\n");
				}else{
				if(symcount == 0){
				char scope[100];
  				memset(scope, '\0', sizeof(scope));

  					
       				populateSymbolTable(semantic, head, scope);
       				displaySymbolTable();
				symcount =1;
				}else{
					printf("\nSymbol Table Printed Above\n");
				}
				}
			}

   			if(input == 6){

   				if(semantic!=NULL)
   				fclose(semantic);

   				FILE *fptr = fopen("Type&SemanticErrors.txt", "r");
   				char c;
   				fseek(fptr,0,SEEK_SET);
				
   				c = fgetc(fptr);
   				    while (c != EOF){
   				        printf ("%c", c);
   				        c = fgetc(fptr);
					tyu++;
   				    }
				flagger = 6;
   				fclose(fptr);
				if(tyu == 0){
					printf("\n Successfully Compiled Code\n")	;
				}

			// Typechecking
   				/*FILE* fptr = fopen("typecheck.txt","r");
   				fseek(fptr,0,SEEK_SET);
   				c = fgetc(fptr);
   				    while (c != EOF){
   				        printf ("%c", c);
   				        c = fgetc(fptr);
   				    }
   				fclose(fptr);*/
			}
       			
   			if(input == 7){

   				FILE *fpdummy = fopen("ignore.txt", "W");
				if(flagger < 6 || tyu != 0){
				  printf("Please Use Semantic and Typechecking first\n");
				}
				else{

				char scope[100];
				memset(scope, '\0', sizeof(scope));
				codegen(head, fp2);

   			 	GenerateCodeStmts(fpdummy, head, fp2, scope);
   			 	// assert(0);
				}
   			 	 fclose(fp2); 

   			 	// fclose(fpdummy);
			}
       			 	 
			printf("\n1 : for printing the token list \n2 : for parsing to verify the syntactic correctness of the input source code and produce parse tree on console\n3 : for creating the abstract syntax tree and printing it on console\n4 : for displaying the amount of allocated memory and number of nodes to each of parse tree and abstract syntax tree\n5 : for printing the Symbol Table\n6 : Type Checking and Semantic Verification\n7 : Code Generation\n0 : Exit\n\n");
			scanf("%d", &input);
	}
		while(input==1 || input==2||input ==3 || input ==4 || input==5 || input==6||input ==7);
		printf("\nThank You For Using Compiler\n");

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1518, %rsp           # imm = 0x1518
xorl %eax, %eax
movl %eax, -0x3c(%rbp)
movl %eax, -0x38(%rbp)
cmpl $0x3, %edi
jne 0x8111
movq %rsi, %r14
leaq 0x2d12(%rip), %rdi      # 0xa867
callq 0x2070
leaq 0x2e37(%rip), %rdi      # 0xa998
callq 0x2070
movq 0x8(%r14), %rdi
leaq 0x2957(%rip), %rsi      # 0xa4c8
callq 0x2170
movq %rax, %rbx
movq 0x10(%r14), %rdi
leaq 0x2946(%rip), %rsi      # 0xa4ca
callq 0x2170
movq %rax, -0x58(%rbp)
leaq 0x2938(%rip), %rdi      # 0xa4cc
leaq 0x2949(%rip), %rsi      # 0xa4e4
callq 0x2170
leaq 0x2fc6(%rip), %rdi      # 0xab6d
testq %rbx, %rbx
je 0x8118
cmpq $0x0, -0x58(%rbp)
je 0x8118
movq %rax, %r15
leaq 0x2922(%rip), %rdi      # 0xa4e7
movq %rbx, %rsi
callq 0x20e0
movq %rbx, %rdi
callq 0x2090
callq 0x59fa
movq 0x8(%r14), %rdi
leaq 0x2b11(%rip), %rsi      # 0xa6f6
callq 0x2170
movq %rax, -0x68(%rbp)
leaq 0x1f5c(%rip), %rdi      # 0x9b51
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x21a0
movl $0x0, -0x34(%rbp)
movq $0x0, -0x48(%rbp)
xorl %r13d, %r13d
movl $0x0, -0x30(%rbp)
movq %r14, -0x78(%rbp)
movq %r15, -0x70(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x7d3e
movl %r13d, -0x5c(%rbp)
movq -0x68(%rbp), %rbx
movq %rbx, %rdi
callq 0x20c0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %rbx, %rdi
callq 0x2441
movq %rax, -0x68(%rbp)
callq 0x255b
movq %rax, %r15
movq (%rax), %rbx
leaq 0x1e(%rbx), %r13
movq %r13, %rdi
leaq 0x287d(%rip), %rsi      # 0xa4e9
callq 0x2120
testl %eax, %eax
je 0x7ced
movl $0x1, %r14d
movq $0x0, -0x50(%rbp)
movq %rbx, %rdi
leaq 0x1742(%rip), %rsi      # 0x93cf
callq 0x2120
testl %eax, %eax
je 0x7cba
leal 0x1(%r14), %r12d
movl 0x3c(%rbx), %r8d
leaq 0x2846(%rip), %rdi      # 0xa4eb
movl %r14d, %esi
movq %rbx, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x20b0
movl %r12d, %r14d
jmp 0x7cc4
movq -0x50(%rbp), %rax
incl %eax
movq %rax, -0x50(%rbp)
movq %r15, %rdi
callq 0x2030
callq 0x255b
movq (%r15), %rbx
leaq 0x1e(%rbx), %r13
movq %r13, %rdi
leaq 0x2807(%rip), %rsi      # 0xa4e9
callq 0x2120
testl %eax, %eax
jne 0x7c83
jmp 0x7cfb
movq $0x0, -0x50(%rbp)
movl $0x1, %r14d
movq -0x50(%rbp), %rbx
leal (%rbx,%r14), %esi
leaq 0x2808(%rip), %rdi      # 0xa512
xorl %eax, %eax
callq 0x20b0
leaq 0x280f(%rip), %rdi      # 0xa527
movl %r14d, %esi
xorl %eax, %eax
callq 0x20b0
leaq 0x2817(%rip), %rdi      # 0xa540
movl %ebx, %esi
xorl %eax, %eax
callq 0x20b0
movq -0x78(%rbp), %r14
movq -0x70(%rbp), %r15
movl -0x5c(%rbp), %r13d
cmpl $0x2, -0x2c(%rbp)
jne 0x7e29
movl $0x8c4, %edx            # imm = 0x8C4
leaq -0x1540(%rbp), %rdi
xorl %esi, %esi
callq 0x20f0
movl %r13d, %r12d
movq %r14, %r13
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, -0x1540(%rbp)
movl $0xbf4, %edx            # imm = 0xBF4
movq %r15, %rbx
leaq -0xc70(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x20f0
movl %r14d, -0xc70(%rbp)
leaq 0x6a3b(%rip), %r14      # 0xe7d0
movq %r14, %rdi
leaq -0x1540(%rbp), %rsi
movq %r15, %rdx
movq %rbx, %r15
callq 0x3e00
movl $0x2e38, %edx           # imm = 0x2E38
leaq 0x12baa(%rip), %rbx     # 0x1a960
movq %rbx, %rdi
movl $0xff, %esi
callq 0x20f0
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %r14
movl %r12d, %r13d
callq 0x3ef8
movl $0xc8, %edx
leaq 0x12ab0(%rip), %rdi     # 0x1a890
movl $0xff, %esi
callq 0x20f0
movq 0x8(%r14), %rdi
callq 0x41e3
movq %rax, %rbx
movq %rax, %rdi
leaq -0x3c(%rbp), %rsi
callq 0x3bc9
movq %rbx, %rdi
callq 0x4157
leaq 0x2add(%rip), %rdi      # 0xa8ee
callq 0x2070
movq %rbx, -0x48(%rbp)
movq %rbx, %rdi
callq 0x4643
movl $0x2, -0x30(%rbp)
cmpl $0x3, -0x2c(%rbp)
jne 0x7e8c
movq -0x48(%rbp), %rbx
testq %rbx, %rbx
sete %al
cmpl $0x2, -0x30(%rbp)
setl %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x7e55
leaq 0x2abc(%rip), %rdi      # 0xa90a
callq 0x2070
jmp 0x7e8c
movq %rbx, %rdi
callq 0x39b7
movq %rbx, %rdi
callq 0x3b89
movq %rbx, %rdi
leaq -0x38(%rbp), %rsi
callq 0x3bc9
leaq 0x2a76(%rip), %rdi      # 0xa8ee
callq 0x2070
movq %rbx, %rdi
callq 0x4643
movl $0x3, -0x30(%rbp)
cmpl $0x4, -0x2c(%rbp)
jne 0x7f36
cmpl $0x2, -0x30(%rbp)
jg 0x7ead
leaq 0x2a9b(%rip), %rdi      # 0xa93e
callq 0x2070
jmp 0x7f36
movslq -0x3c(%rbp), %rsi
movq %rsi, %rbx
shlq $0x6, %rbx
movq %r15, %r12
movq %r14, %r15
movl -0x38(%rbp), %r14d
shll $0x5, %r14d
leaq 0x2685(%rip), %rdi      # 0xa552
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20b0
movslq -0x38(%rbp), %rdx
movl %edx, %esi
shrl $0x1f, %esi
addl %edx, %esi
sarl %esi
shlq $0x5, %rdx
movabsq $0x7fffffffffffffe0, %rax # imm = 0x7FFFFFFFFFFFFFE0
andq %rax, %rdx
leaq 0x2695(%rip), %rdi      # 0xa591
xorl %eax, %eax
callq 0x20b0
xorps %xmm0, %xmm0
cvtsi2ss %ebx, %xmm0
subl %r14d, %ebx
movq %r15, %r14
movq %r12, %r15
imull $0x64, %ebx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x26a9(%rip), %rdi      # 0xa5d8
movb $0x1, %al
callq 0x20b0
cmpl $0x5, -0x2c(%rbp)
jne 0x7fba
cmpl $0x2, -0x30(%rbp)
jg 0x7f50
leaq 0x29f5(%rip), %rdi      # 0xa93e
callq 0x2070
jmp 0x7fba
cmpl $0x0, -0x34(%rbp)
je 0x7f64
leaq 0x29c5(%rip), %rdi      # 0xa922
callq 0x2070
jmp 0x7fb3
xorps %xmm0, %xmm0
movaps %xmm0, -0xc20(%rbp)
movaps %xmm0, -0xc30(%rbp)
movaps %xmm0, -0xc40(%rbp)
movaps %xmm0, -0xc50(%rbp)
movaps %xmm0, -0xc60(%rbp)
movaps %xmm0, -0xc70(%rbp)
movl $0x0, -0xc10(%rbp)
movq %r15, %rdi
movq -0x48(%rbp), %rsi
leaq -0xc70(%rbp), %rdx
callq 0x5f3e
callq 0x5016
movl $0x1, -0x34(%rbp)
cmpl $0x6, -0x2c(%rbp)
jne 0x8038
testq %r15, %r15
je 0x7fcd
movq %r15, %rdi
callq 0x2090
leaq 0x24f8(%rip), %rdi      # 0xa4cc
leaq 0x271b(%rip), %rsi      # 0xa6f6
callq 0x2170
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %rbx, %rdi
callq 0x2100
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x8018
sarl $0x18, %eax
movl %eax, %edi
callq 0x2040
movq %rbx, %rdi
callq 0x2100
incl %r13d
jmp 0x7ff7
movq %rbx, %rdi
callq 0x2090
movl $0x6, -0x30(%rbp)
testl %r13d, %r13d
jne 0x8038
leaq 0x291d(%rip), %rdi      # 0xa950
callq 0x2070
cmpl $0x7, -0x2c(%rbp)
jne 0x80dc
leaq 0x25b2(%rip), %rdi      # 0xa5fb
leaq 0x25b6(%rip), %rsi      # 0xa606
callq 0x2170
movq %rax, %rbx
cmpl $0x6, -0x30(%rbp)
setl %al
testl %r13d, %r13d
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x807a
leaq 0x28fa(%rip), %rdi      # 0xa96d
callq 0x2070
jmp 0x80d3
xorps %xmm0, %xmm0
movaps %xmm0, -0xc20(%rbp)
movaps %xmm0, -0xc30(%rbp)
movaps %xmm0, -0xc40(%rbp)
movaps %xmm0, -0xc50(%rbp)
movaps %xmm0, -0xc60(%rbp)
movaps %xmm0, -0xc70(%rbp)
movl $0x0, -0xc10(%rbp)
movq -0x58(%rbp), %r12
movq %r12, %rsi
callq 0x6c7f
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq %r12, %rdx
leaq -0xc70(%rbp), %rcx
callq 0x772f
movq -0x58(%rbp), %rdi
callq 0x2090
leaq 0x28b5(%rip), %rdi      # 0xa998
callq 0x2070
leaq 0x1a62(%rip), %rdi      # 0x9b51
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x21a0
movl -0x2c(%rbp), %eax
decl %eax
cmpl $0x7, %eax
jb 0x7c21
leaq 0x2a40(%rip), %rdi      # 0xab4f
jmp 0x8118
leaq 0x2a64(%rip), %rdi      # 0xab7c
callq 0x2070
xorl %eax, %eax
addq $0x1518, %rsp           # imm = 0x1518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq