// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, -0x40(%rbp)
andl $0x0, -0x3c(%rbp)
cmpl $0x3, %edi
jne 0x7955
movq %rsi, %r12
leaq 0x243c(%rip), %rdi      # 0x9867
callq 0x2070
leaq 0x2561(%rip), %rdi      # 0x9998
callq 0x2070
movq 0x8(%r12), %rdi
leaq 0x2080(%rip), %rsi      # 0x94c8
callq 0x2180
movq %rax, %rbx
movq 0x10(%r12), %rdi
leaq 0x206e(%rip), %rsi      # 0x94ca
callq 0x2180
movq %rax, %r15
leaq 0x2061(%rip), %rdi      # 0x94cc
leaq 0x2072(%rip), %rsi      # 0x94e4
callq 0x2180
testq %rbx, %rbx
je 0x795e
testq %r15, %r15
je 0x795e
movq %rax, -0x50(%rbp)
leaq 0x2053(%rip), %rdi      # 0x94e7
movq %rbx, %rsi
callq 0x20e0
movq %rbx, %rdi
callq 0x2090
callq 0x5394
movq 0x8(%r12), %rdi
leaq 0x2241(%rip), %rsi      # 0x96f6
callq 0x2180
movq %rax, -0x68(%rbp)
leaq 0x168c(%rip), %rdi      # 0x8b51
leaq -0x2c(%rbp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x21b0
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 0x75d6
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 0x2170
movq %rbx, %rdi
callq 0x2451
movq %rax, -0x68(%rbp)
callq 0x2559
movq %rax, %r15
pushq $0x1
popq %rax
movl %eax, %r12d
movq (%r15), %rbx
cmpb $0x24, 0x1e(%rbx)
jne 0x7545
cmpb $0x0, 0x1f(%rbx)
je 0x7590
movq %rbx, %rdi
leaq 0xe80(%rip), %rsi       # 0x83cf
callq 0x2120
testl %eax, %eax
je 0x757e
leaq 0x1e(%rbx), %rcx
leal 0x1(%r12), %r13d
movl 0x3c(%rbx), %r8d
leaq 0x1f7f(%rip), %rdi      # 0x94eb
movl %r12d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20b0
movl %r13d, %r12d
jmp 0x7581
incl %r14d
movq %r15, %rdi
callq 0x2030
callq 0x2559
jmp 0x7536
leal (%r14,%r12), %esi
leaq 0x1f77(%rip), %rdi      # 0x9512
xorl %eax, %eax
callq 0x20b0
leaq 0x1f7e(%rip), %rdi      # 0x9527
movl %r12d, %esi
xorl %eax, %eax
callq 0x20b0
leaq 0x1f86(%rip), %rdi      # 0x9540
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 0x7661
movl %ebx, %r13d
leaq 0x61e7(%rip), %rbx      # 0xd7d0
movq %rbx, %rdi
leaq -0x1530(%rbp), %rsi
leaq -0xc60(%rbp), %rdx
callq 0x3b4e
leaq 0x1235a(%rip), %r14     # 0x19960
movq %r14, %rdi
callq 0x32cc
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %ebx
callq 0x3c32
callq 0x3621
movq 0x8(%r12), %rdi
callq 0x3e89
movq %rax, %r14
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
callq 0x3926
movq %r14, %rdi
callq 0x3e2a
leaq 0x22a5(%rip), %rdi      # 0x98ee
callq 0x2070
movq %r14, -0x38(%rbp)
movq %r14, %rdi
callq 0x4207
movl -0x2c(%rbp), %eax
pushq $0x2
popq %r14
cmpl $0x3, %eax
jne 0x76c8
cmpq $0x0, -0x38(%rbp)
je 0x76b9
cmpl $0x1, %r14d
jle 0x76b9
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x390a
movq %r14, %rdi
leaq -0x3c(%rbp), %rsi
callq 0x3926
leaq 0x225c(%rip), %rdi      # 0x98ee
callq 0x2070
movq %r14, %rdi
callq 0x4207
movl -0x2c(%rbp), %eax
pushq $0x3
popq %r14
cmpl $0x4, %eax
je 0x76e8
cmpl $0x5, %eax
je 0x7791
jmp 0x77fb
leaq 0x224a(%rip), %rdi      # 0x990a
callq 0x2070
movl -0x2c(%rbp), %eax
cmpl $0x4, %eax
jne 0x7778
cmpl $0x2, %r14d
jg 0x76e8
leaq 0x2260(%rip), %rdi      # 0x993e
callq 0x2070
jmp 0x7775
movslq -0x40(%rbp), %rsi
movl %r14d, %r12d
movq %rsi, %r14
shlq $0x6, %r14
movl %ebx, %r13d
movslq -0x3c(%rbp), %rbx
movl %ebx, %r15d
shll $0x5, %r15d
leaq 0x1e47(%rip), %rdi      # 0x9552
movq %r14, %rdx
xorl %eax, %eax
callq 0x20b0
movl %ebx, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
shlq $0x5, %rbx
movabsq $0x7fffffffffffffe0, %rcx # imm = 0x7FFFFFFFFFFFFFE0
andq %rcx, %rbx
leaq 0x1e5c(%rip), %rdi      # 0x9591
movl %eax, %esi
movq %rbx, %rdx
movl %r13d, %ebx
xorl %eax, %eax
callq 0x20b0
cvtsi2ss %r14d, %xmm0
subl %r15d, %r14d
movq -0x58(%rbp), %r15
imull $0x64, %r14d, %eax
movl %r12d, %r14d
movq -0x60(%rbp), %r12
cvtsi2ss %eax, %xmm1
divss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
leaq 0x1e6a(%rip), %rdi      # 0x95d8
movb $0x1, %al
callq 0x20b0
movl -0x2c(%rbp), %eax
cmpl $0x5, %eax
jne 0x77f8
cmpl $0x2, %r14d
jg 0x7791
leaq 0x21b4(%rip), %rdi      # 0x993e
callq 0x2070
jmp 0x77f8
cmpl $0x0, -0x30(%rbp)
je 0x77a5
leaq 0x2184(%rip), %rdi      # 0x9922
callq 0x2070
jmp 0x77f2
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)
andl $0x0, -0xc00(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0xc60(%rbp), %rdx
callq 0x58b2
callq 0x4ae2
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl $0x6, %eax
jne 0x7876
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x780e
callq 0x2090
leaq 0x1cb7(%rip), %rdi      # 0x94cc
leaq 0x1eda(%rip), %rsi      # 0x96f6
callq 0x2180
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq %r14, %rdi
callq 0x2100
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x7858
sarl $0x18, %eax
movl %eax, %edi
callq 0x2040
movq %r14, %rdi
callq 0x2100
incl %ebx
jmp 0x7838
movq %r14, %rdi
callq 0x2090
pushq $0x6
popq %r14
testl %ebx, %ebx
jne 0x7876
leaq 0x20e1(%rip), %rdi      # 0x9950
callq 0x2070
xorl %ebx, %ebx
cmpl $0x7, -0x2c(%rbp)
jne 0x791f
leaq 0x1d74(%rip), %rdi      # 0x95fb
leaq 0x1d78(%rip), %rsi      # 0x9606
callq 0x2180
movl %ebx, %ecx
movq %rax, %rbx
movl %r14d, %r13d
cmpl $0x6, %r14d
setl %al
movl %ecx, %r14d
testl %ecx, %ecx
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x78bf
leaq 0x20b5(%rip), %rdi      # 0x996d
callq 0x2070
jmp 0x7911
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)
andl $0x0, -0xc00(%rbp)
movq %r15, %rsi
callq 0x654e
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
leaq -0xc60(%rbp), %rcx
callq 0x7006
movq %r15, %rdi
callq 0x2090
movl %r14d, %ebx
movl %r13d, %r14d
leaq 0x2072(%rip), %rdi      # 0x9998
callq 0x2070
leaq 0x121f(%rip), %rdi      # 0x8b51
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x21b0
movl -0x2c(%rbp), %eax
leal -0x1(%rax), %ecx
cmpl $0x7, %ecx
jb 0x74f1
leaq 0x21fc(%rip), %rdi      # 0x9b4f
jmp 0x7965
leaq 0x2220(%rip), %rdi      # 0x9b7c
jmp 0x7965
leaq 0x2208(%rip), %rdi      # 0x9b6d
callq 0x2070
xorl %eax, %eax
addq $0x1508, %rsp           # imm = 0x1508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq