More functional Makefile.
[Jack-Compiler.git] / error.c
blobc8ff8dc9e57193b3019a63bc0261019a9ac643c6
1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
6 #include "error.h"
8 void exit_error(const int err_num, const char *err_msg)
10 fprintf(stderr, "ERROR %d: %s\n", err_num, err_msg);
12 if(pInfile != NULL) { fclose(pInfile); }
13 if(pOutfile != NULL) { fclose(pOutfile); }
15 exit(EXIT_FAILURE);
18 void warning_error(const int err_num, const char *err_msg)
20 fprintf(stderr, "WARNING: %d: %s\n", err_num, err_msg);
23 void compiler_error(const int comp_num, const char *err_msg, char *pS, char *pC, char pT[])
25 char *pTemp = pC;
26 char *pCount = pC;
27 int sp_count = 0; /* space count */
28 int line_num = 1;
29 int token_len = 0;
31 printf("\n");
32 fflush(stdout);
34 /* backup pointer to token to beginning of previous token. */
35 token_len = strlen(pT);
36 do {
37 pC--;
38 } while (strncmp(pT, pC, token_len) != 0);
40 pTemp = pC;
41 /* set pTemp to beginning of current line */
42 while(pTemp > pS && *pTemp != '\n')
44 sp_count++;
45 pTemp--;
48 /* count line number to beginning of file print for user later */
49 while(pS <= pC)
51 if(*pC == '\n') { line_num++; }
52 pC--;
54 pTemp++; /* move past newline */
55 pCount = pTemp;
57 /* print last line to stderr */
58 while((*pTemp != '\n') && (*pTemp != '\0'))
60 putc(*pTemp, stderr);
61 pTemp++;
63 fprintf(stderr, "\n");
65 sp_count--; /* off by one because of arrow printed at end of line*/
67 /* print arrow pointing to token compiler cannot comprehend */
68 while(sp_count >= 0)
70 if(sp_count == 0)
72 fprintf(stderr, "^\n");
73 } else {
74 if(*pCount == '\t')
76 fprintf(stderr, "\t");
77 } else {
78 fprintf(stderr, " ");
81 sp_count--;
82 pCount++;
85 fprintf(stderr, "ERROR %d: %s On Line %d.\n\n", comp_num, err_msg, line_num);
87 exit(EXIT_FAILURE);
90 void line_notification(const int line_num)
92 fprintf(stderr, "ERROR FOUND ON LINE: %d.\n", line_num);