// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1640, %rsp           # imm = 0x1640
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x20c0
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x20c0
cmpl $0x3, -0x8(%rbp)
je 0xb237
leaq 0x23f2(%rip), %rdi      # 0xd616
movb $0x0, %al
callq 0x2090
movl $0x0, -0x4(%rbp)
jmp 0xb86d
leaq 0x2406(%rip), %rdi      # 0xd644
movb $0x0, %al
callq 0x2090
leaq 0x2480(%rip), %rdi      # 0xd6cc
movb $0x0, %al
callq 0x2090
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2622(%rip), %rsi      # 0xd884
callq 0x2150
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x260c(%rip), %rsi      # 0xd886
callq 0x2150
movq %rax, -0x40(%rbp)
leaq 0x25fe(%rip), %rdi      # 0xd888
leaq 0x260f(%rip), %rsi      # 0xd8a0
callq 0x2150
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xb2bb
leaq 0x25fb(%rip), %rdi      # 0xd8a3
movb $0x0, %al
callq 0x2090
movl $0x0, -0x4(%rbp)
jmp 0xb86d
cmpq $0x0, -0x40(%rbp)
jne 0xb2dc
leaq 0x25da(%rip), %rdi      # 0xd8a3
movb $0x0, %al
callq 0x2090
movl $0x0, -0x4(%rbp)
jmp 0xb86d
movq -0x38(%rbp), %rdi
leaq 0x1eb4(%rip), %rsi      # 0xd19b
leaq 0x25c5(%rip), %rdx      # 0xd8b3
movb $0x0, %al
callq 0x2100
movq -0x38(%rbp), %rdi
callq 0x2070
callq 0x7f80
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2882(%rip), %rsi      # 0xdb94
callq 0x2150
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq $0x0, -0x60(%rbp)
leaq 0x196b(%rip), %rdi      # 0xcc9c
leaq -0x14(%rbp), %rsi
movb $0x0, %al
callq 0x2180
cmpl $0x1, -0x14(%rbp)
jne 0xb444
movq -0x50(%rbp), %rdi
callq 0x20a0
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2140
movq -0x50(%rbp), %rdi
callq 0x2590
movq %rax, -0x50(%rbp)
callq 0x2750
movq %rax, -0x68(%rbp)
movl $0x1, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
leaq 0x2521(%rip), %rsi      # 0xd8b5
callq 0x20f0
cmpl $0x0, %eax
je 0xb40e
movq -0x68(%rbp), %rax
movq (%rax), %rdi
leaq 0x1130(%rip), %rsi      # 0xc4dc
callq 0x20f0
cmpl $0x0, %eax
jne 0xb3c1
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xb3f7
movl -0x6c(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rdx
movq -0x68(%rbp), %rax
movq (%rax), %rcx
addq $0x1e, %rcx
movq -0x68(%rbp), %rax
movq (%rax), %rax
movl 0x3c(%rax), %r8d
leaq 0x24c7(%rip), %rdi      # 0xd8b7
movb $0x0, %al
callq 0x2090
movq -0x68(%rbp), %rdi
callq 0x2030
callq 0x2750
movq %rax, -0x78(%rbp)
jmp 0xb382
movl -0x6c(%rbp), %esi
addl -0x70(%rbp), %esi
leaq 0x24c3(%rip), %rdi      # 0xd8de
movb $0x0, %al
callq 0x2090
movl -0x6c(%rbp), %esi
leaq 0x24c7(%rip), %rdi      # 0xd8f3
movb $0x0, %al
callq 0x2090
movl -0x70(%rbp), %esi
leaq 0x24cf(%rip), %rdi      # 0xd90c
movb $0x0, %al
callq 0x2090
cmpl $0x2, -0x14(%rbp)
jne 0xb513
leaq -0x940(%rbp), %rdi
xorl %esi, %esi
movl $0x8c4, %edx            # imm = 0x8C4
callq 0x20c0
movl $0xffffffff, -0x940(%rbp) # imm = 0xFFFFFFFF
leaq -0x1540(%rbp), %rdi
xorl %esi, %esi
movl $0xbf4, %edx            # imm = 0xBF4
callq 0x20c0
movl $0xffffffff, -0x1540(%rbp) # imm = 0xFFFFFFFF
leaq -0x940(%rbp), %rsi
leaq -0x1540(%rbp), %rdx
leaq 0x6313(%rip), %rdi      # 0x117b0
callq 0x57a0
leaq 0x12807(%rip), %rdi     # 0x1dcb0
callq 0x48b0
leaq 0x127fb(%rip), %rdi     # 0x1dcb0
leaq 0x62f4(%rip), %rsi      # 0x117b0
callq 0x5930
callq 0x4dd0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5d00
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x54f0
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x5c60
leaq 0x2422(%rip), %rdi      # 0xd91e
movb $0x0, %al
callq 0x2090
movq -0x60(%rbp), %rdi
callq 0x61e0
movl $0x2, -0x20(%rbp)
cmpl $0x3, -0x14(%rbp)
jne 0xb56c
cmpq $0x0, -0x60(%rbp)
je 0xb526
cmpl $0x2, -0x20(%rbp)
jge 0xb536
leaq 0x240e(%rip), %rdi      # 0xd93b
movb $0x0, %al
callq 0x2090
jmp 0xb56a
movq -0x60(%rbp), %rdi
callq 0x54c0
movq -0x60(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x54f0
leaq 0x23cb(%rip), %rdi      # 0xd91e
movb $0x0, %al
callq 0x2090
movq -0x60(%rbp), %rdi
callq 0x61e0
movl $0x3, -0x20(%rbp)
jmp 0xb56c
cmpl $0x4, -0x14(%rbp)
jne 0xb621
cmpl $0x3, -0x20(%rbp)
jge 0xb58f
leaq 0x23d1(%rip), %rdi      # 0xd954
movb $0x0, %al
callq 0x2090
jmp 0xb61f
movslq -0x28(%rbp), %rax
shlq $0x6, %rax
movl %eax, -0x18(%rbp)
movslq -0x2c(%rbp), %rax
shlq $0x6, %rax
shrq %rax
movl %eax, -0x1c(%rbp)
movl -0x28(%rbp), %esi
movslq -0x28(%rbp), %rdx
shlq $0x6, %rdx
leaq 0x23ad(%rip), %rdi      # 0xd967
movb $0x0, %al
callq 0x2090
movl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %esi
movslq -0x2c(%rbp), %rdx
shlq $0x6, %rdx
shrq %rdx
leaq 0x23c6(%rip), %rdi      # 0xd9a6
movb $0x0, %al
callq 0x2090
movl -0x18(%rbp), %eax
subl -0x1c(%rbp), %eax
imull $0x64, %eax, %eax
cvtsi2ss %eax, %xmm0
cvtsi2ssl -0x18(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x1544(%rbp)
movss -0x1544(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x23d5(%rip), %rdi      # 0xd9ed
movb $0x1, %al
callq 0x2090
jmp 0xb621
cmpl $0x5, -0x14(%rbp)
jne 0xb68a
cmpl $0x3, -0x20(%rbp)
jge 0xb63d
leaq 0x2320(%rip), %rdi      # 0xd954
movb $0x0, %al
callq 0x2090
jmp 0xb688
cmpl $0x0, -0x54(%rbp)
jne 0xb678
leaq -0x15b0(%rbp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x20c0
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq -0x15b0(%rbp), %rdx
callq 0x87a0
callq 0x7140
movl $0x1, -0x54(%rbp)
jmp 0xb686
leaq 0x2391(%rip), %rdi      # 0xda10
movb $0x0, %al
callq 0x2090
jmp 0xb688
jmp 0xb68a
cmpl $0x6, -0x14(%rbp)
jne 0xb749
cmpq $0x0, -0x48(%rbp)
je 0xb6a4
movq -0x48(%rbp), %rdi
callq 0x2070
leaq 0x21dd(%rip), %rdi      # 0xd888
leaq 0x24e2(%rip), %rsi      # 0xdb94
callq 0x2150
movq %rax, -0x15b8(%rbp)
movq -0x15b8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2140
movq -0x15b8(%rbp), %rdi
callq 0x20d0
movb %al, -0x15b9(%rbp)
movsbl -0x15b9(%rbp), %eax
cmpl $-0x1, %eax
je 0xb720
movsbl -0x15b9(%rbp), %esi
leaq 0x2331(%rip), %rdi      # 0xda2d
movb $0x0, %al
callq 0x2090
movq -0x15b8(%rbp), %rdi
callq 0x20d0
movb %al, -0x15b9(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xb6e2
movl $0x6, -0x20(%rbp)
movq -0x15b8(%rbp), %rdi
callq 0x2070
cmpl $0x0, -0x24(%rbp)
jne 0xb747
leaq 0x22f0(%rip), %rdi      # 0xda30
movb $0x0, %al
callq 0x2090
jmp 0xb749
cmpl $0x7, -0x14(%rbp)
jne 0xb7c9
leaq 0x22f8(%rip), %rdi      # 0xda4e
leaq 0x22fc(%rip), %rsi      # 0xda59
callq 0x2150
movq %rax, -0x15c8(%rbp)
cmpl $0x6, -0x20(%rbp)
jl 0xb775
cmpl $0x0, -0x24(%rbp)
je 0xb785
leaq 0x22df(%rip), %rdi      # 0xda5b
movb $0x0, %al
callq 0x2090
jmp 0xb7c0
leaq -0x1630(%rbp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x20c0
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9da0
movq -0x15c8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq -0x1630(%rbp), %rcx
callq 0xac80
movq -0x40(%rbp), %rdi
callq 0x2070
leaq 0x1efc(%rip), %rdi      # 0xd6cc
movb $0x0, %al
callq 0x2090
leaq 0x14be(%rip), %rdi      # 0xcc9c
leaq -0x14(%rbp), %rsi
movb $0x0, %al
callq 0x2180
movb $0x1, %al
cmpl $0x1, -0x14(%rbp)
movb %al, -0x1631(%rbp)
je 0xb84a
movb $0x1, %al
cmpl $0x2, -0x14(%rbp)
movb %al, -0x1631(%rbp)
je 0xb84a
movb $0x1, %al
cmpl $0x3, -0x14(%rbp)
movb %al, -0x1631(%rbp)
je 0xb84a
movb $0x1, %al
cmpl $0x4, -0x14(%rbp)
movb %al, -0x1631(%rbp)
je 0xb84a
movb $0x1, %al
cmpl $0x5, -0x14(%rbp)
movb %al, -0x1631(%rbp)
je 0xb84a
movb $0x1, %al
cmpl $0x6, -0x14(%rbp)
movb %al, -0x1631(%rbp)
je 0xb84a
cmpl $0x7, -0x14(%rbp)
sete %al
movb %al, -0x1631(%rbp)
movb -0x1631(%rbp), %al
testb $0x1, %al
jne 0xb33c
leaq 0x2228(%rip), %rdi      # 0xda87
movb $0x0, %al
callq 0x2090
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1640, %rsp           # imm = 0x1640
popq %rbp
retq