// File: /workspace/github_c_makefile_star2_O3/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 $0x1508, %rsp           # imm = 0x1508
xorl %eax, %eax
movl %eax, -0x40(%rbp)
movl %eax, -0x3c(%rbp)
cmpl $0x3, %edi
jne 0x800f
movq %rsi, %r12
leaq 0x2dc5(%rip), %rdi      # 0xa867
callq 0x2070
leaq 0x2eea(%rip), %rdi      # 0xa998
callq 0x2070
movq 0x8(%r12), %rdi
leaq 0x2a09(%rip), %rsi      # 0xa4c8
callq 0x2170
movq %rax, %rbx
movq 0x10(%r12), %rdi
leaq 0x29f7(%rip), %rsi      # 0xa4ca
callq 0x2170
movq %rax, %r15
leaq 0x29ea(%rip), %rdi      # 0xa4cc
leaq 0x29fb(%rip), %rsi      # 0xa4e4
callq 0x2170
testq %rbx, %rbx
je 0x8018
testq %r15, %r15
je 0x8018
movq %rax, -0x50(%rbp)
leaq 0x29dc(%rip), %rdi      # 0xa4e7
movq %rbx, %rsi
callq 0x20e0
movq %rbx, %rdi
callq 0x2090
callq 0x5920
movq 0x8(%r12), %rdi
leaq 0x2bca(%rip), %rsi      # 0xa6f6
callq 0x2170
movq %rax, -0x68(%rbp)
leaq 0x2015(%rip), %rdi      # 0x9b51
leaq -0x2c(%rbp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x21a0
movl (%rbx), %eax
movl $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r15, -0x58(%rbp)
movq %r12, -0x60(%rbp)
cmpl $0x1, %eax
jne 0x7c4d
movl %r14d, -0x44(%rbp)
movl %ebx, -0x48(%rbp)
movq -0x68(%rbp), %rbx
movq %rbx, %rdi
callq 0x20c0
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %rbx, %rdi
callq 0x243e
movq %rax, -0x68(%rbp)
movl $0x1, %r12d
callq 0x2558
movq %rax, %r15
movq (%r15), %rbx
cmpb $0x24, 0x1e(%rbx)
jne 0x7bbc
cmpb $0x0, 0x1f(%rbx)
je 0x7c07
movq %rbx, %rdi
leaq 0x1809(%rip), %rsi      # 0x93cf
callq 0x2120
testl %eax, %eax
je 0x7bf5
leaq 0x1e(%rbx), %rcx
leal 0x1(%r12), %r13d
movl 0x3c(%rbx), %r8d
leaq 0x2908(%rip), %rdi      # 0xa4eb
movl %r12d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20b0
movl %r13d, %r12d
jmp 0x7bf8
incl %r14d
movq %r15, %rdi
callq 0x2030
callq 0x2558
jmp 0x7bad
leal (%r14,%r12), %esi
leaq 0x2900(%rip), %rdi      # 0xa512
xorl %eax, %eax
callq 0x20b0
leaq 0x2907(%rip), %rdi      # 0xa527
movl %r12d, %esi
xorl %eax, %eax
callq 0x20b0
leaq 0x290f(%rip), %rdi      # 0xa540
movl %r14d, %esi
xorl %eax, %eax
callq 0x20b0
movl -0x2c(%rbp), %eax
movq -0x60(%rbp), %r12
movq -0x58(%rbp), %r15
movl -0x48(%rbp), %ebx
movl -0x44(%rbp), %r14d
cmpl $0x2, %eax
jne 0x7cf5
movl %ebx, %r13d
leaq 0x6b70(%rip), %rbx      # 0xe7d0
movq %rbx, %rdi
leaq -0x1530(%rbp), %rsi
leaq -0xc60(%rbp), %rdx
callq 0x3d84
movl $0x2e38, %edx           # imm = 0x2E38
leaq 0x12cde(%rip), %r14     # 0x1a960
movq %r14, %rdi
movl $0xff, %esi
callq 0x20f0
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %ebx
callq 0x3e7c
movl $0xc8, %edx
leaq 0x12be7(%rip), %rdi     # 0x1a890
movl $0xff, %esi
callq 0x20f0
movq 0x8(%r12), %rdi
callq 0x410b
movq %rax, %r14
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
callq 0x3b52
movq %r14, %rdi
callq 0x40ac
leaq 0x2c13(%rip), %rdi      # 0xa8ee
callq 0x2070
movq %r14, -0x38(%rbp)
movq %r14, %rdi
callq 0x4576
movl $0x2, %r14d
movl -0x2c(%rbp), %eax
cmpl $0x3, %eax
jne 0x7d66
cmpq $0x0, -0x38(%rbp)
je 0x7d57
cmpl $0x1, %r14d
jle 0x7d57
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x3947
movq %r14, %rdi
callq 0x3b0f
movq %r14, %rdi
leaq -0x3c(%rbp), %rsi
callq 0x3b52
leaq 0x2bc0(%rip), %rdi      # 0xa8ee
callq 0x2070
movq %r14, %rdi
callq 0x4576
movl $0x3, %r14d
movl -0x2c(%rbp), %eax
cmpl $0x4, %eax
je 0x7d8b
cmpl $0x5, %eax
je 0x7e3c
jmp 0x7eaa
leaq 0x2bac(%rip), %rdi      # 0xa90a
callq 0x2070
movl -0x2c(%rbp), %eax
cmpl $0x4, %eax
jne 0x7e23
cmpl $0x2, %r14d
jg 0x7d86
leaq 0x2bc2(%rip), %rdi      # 0xa93e
callq 0x2070
jmp 0x7e20
movl %r14d, %r12d
jmp 0x7d91
movl $0x3, %r12d
movl %ebx, %r13d
movslq -0x40(%rbp), %rsi
movq %rsi, %r14
shlq $0x6, %r14
movslq -0x3c(%rbp), %rbx
movl %ebx, %r15d
shll $0x5, %r15d
leaq 0x27a1(%rip), %rdi      # 0xa552
movq %r14, %rdx
xorl %eax, %eax
callq 0x20b0
movl %ebx, %esi
shrl $0x1f, %esi
addl %ebx, %esi
sarl %esi
shlq $0x5, %rbx
movabsq $0x7fffffffffffffe0, %rax # imm = 0x7FFFFFFFFFFFFFE0
andq %rax, %rbx
leaq 0x27b5(%rip), %rdi      # 0xa591
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20b0
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
subl %r15d, %r14d
imull $0x64, %r14d, %eax
cvtsi2ss %eax, %xmm1
divss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x27cd(%rip), %rdi      # 0xa5d8
movb $0x1, %al
callq 0x20b0
movq -0x58(%rbp), %r15
movl %r13d, %ebx
movl %r12d, %r14d
movq -0x60(%rbp), %r12
movl -0x2c(%rbp), %eax
cmpl $0x5, %eax
jne 0x7ea7
cmpl $0x2, %r14d
jg 0x7e3c
leaq 0x2b09(%rip), %rdi      # 0xa93e
callq 0x2070
jmp 0x7ea7
cmpl $0x0, -0x30(%rbp)
je 0x7e50
leaq 0x2ad9(%rip), %rdi      # 0xa922
callq 0x2070
jmp 0x7ea0
xorps %xmm0, %xmm0
movaps %xmm0, -0xc10(%rbp)
movaps %xmm0, -0xc20(%rbp)
movaps %xmm0, -0xc30(%rbp)
movaps %xmm0, -0xc40(%rbp)
movaps %xmm0, -0xc50(%rbp)
movaps %xmm0, -0xc60(%rbp)
movl $0x0, -0xc00(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0xc60(%rbp), %rdx
callq 0x5e57
callq 0x4f24
movl $0x1, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl $0x6, %eax
jne 0x7f27
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x7ebd
callq 0x2090
leaq 0x2608(%rip), %rdi      # 0xa4cc
leaq 0x282b(%rip), %rsi      # 0xa6f6
callq 0x2170
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r14, %rdi
callq 0x2100
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x7f07
sarl $0x18, %eax
movl %eax, %edi
callq 0x2040
movq %r14, %rdi
callq 0x2100
incl %ebx
jmp 0x7ee7
movq %r14, %rdi
callq 0x2090
movl $0x6, %r14d
testl %ebx, %ebx
jne 0x7f27
leaq 0x2a30(%rip), %rdi      # 0xa950
callq 0x2070
xorl %ebx, %ebx
cmpl $0x7, -0x2c(%rbp)
jne 0x7fd9
movq %r12, %r13
leaq 0x26c0(%rip), %rdi      # 0xa5fb
leaq 0x26c4(%rip), %rsi      # 0xa606
callq 0x2170
movl %ebx, %ecx
movq %rax, %rbx
movl %r14d, %r12d
cmpl $0x6, %r14d
setl %al
movl %ecx, %r14d
testl %ecx, %ecx
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x7f73
leaq 0x2a01(%rip), %rdi      # 0xa96d
callq 0x2070
jmp 0x7fc8
xorps %xmm0, %xmm0
movaps %xmm0, -0xc10(%rbp)
movaps %xmm0, -0xc20(%rbp)
movaps %xmm0, -0xc30(%rbp)
movaps %xmm0, -0xc40(%rbp)
movaps %xmm0, -0xc50(%rbp)
movaps %xmm0, -0xc60(%rbp)
movl $0x0, -0xc00(%rbp)
movq %r15, %rsi
callq 0x6b86
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
leaq -0xc60(%rbp), %rcx
callq 0x7667
movq %r15, %rdi
callq 0x2090
movl %r14d, %ebx
movl %r12d, %r14d
movq %r13, %r12
leaq 0x29b8(%rip), %rdi      # 0xa998
callq 0x2070
leaq 0x1b65(%rip), %rdi      # 0x9b51
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x21a0
movl -0x2c(%rbp), %eax
leal -0x1(%rax), %ecx
cmpl $0x7, %ecx
jb 0x7b68
leaq 0x2b42(%rip), %rdi      # 0xab4f
jmp 0x801f
leaq 0x2b66(%rip), %rdi      # 0xab7c
jmp 0x801f
leaq 0x2b4e(%rip), %rdi      # 0xab6d
callq 0x2070
xorl %eax, %eax
addq $0x1508, %rsp           # imm = 0x1508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq