11#include "definitions.h"
22#include "parsers.h"
33
4+ int lineNum = 1 ;
45/* utils */
56/**
67 * swap last char /n for /0
@@ -325,7 +326,7 @@ char *iterator(char *line, FILE *inp, const int *errors) {
325326 while ((fgets (line , MAX_LENGTH , inp )) != NULL ) { /* read from .as file and save macro data to .am file */
326327 if (strlen (line ) > MAX_LENGTH ) {
327328 errors += 1 ;
328- printf ("--->line to long\n" );
329+ printf ("%d --->line to long\n" , lineNum );
329330 continue ;
330331 }
331332 if (line [0 ] == '\n' ) continue ;
@@ -341,7 +342,7 @@ char *iterator(char *line, FILE *inp, const int *errors) {
341342 * @param error
342343 */
343344void printError (char * error ) {
344- printf ("\n--- >%s\n" , error );
345+ printf ("\n-->%s\n" , error );
345346}
346347
347348/**
@@ -403,14 +404,24 @@ void setAdditionalLines(machineCode *mCode, long *IC, sortType sort, int *L, cha
403404 * @param sort
404405 * @return
405406 */
406- int regNumber (char * reg , sortType sort ) {
407+ int regNumber (char * reg , sortType sort , int * errors ) {
408+ int num ;
407409 if (sort == sort2 ) {
408410 strtok (reg , "[" );
409411 reg = strtok (NULL , "" ); /* for regs in []*/
410412 reg [strlen (reg ) - 1 ] = '\0' ;
413+ ++ reg ;
414+ num = atoi (reg );
415+ if (num > 15 || num < 10 ) {
416+ * errors += 1 ;
417+ printf ("%d --->expected regs between 10 and 15\n" , lineNum );
418+ return -1 ;
419+ }
420+ } else {
421+ ++ reg ;
422+ num = atoi (reg );
411423 }
412- ++ reg ;
413- return atoi (reg );
424+ return num ;
414425}
415426
416427/**
@@ -500,7 +511,7 @@ void setCode(machineCode *mCode, long *IC, func *f, char **parsedLine, char *lab
500511 mCode [* IC ].word .code -> destSort = destSort ;
501512
502513 if (destSort == sort3 || destSort == sort2 ) {
503- destReg = regNumber (parsedLine [1 ], destSort );
514+ destReg = regNumber (parsedLine [1 ], destSort , errors );
504515 mCode [* IC ].word .code -> destReg = destReg ;
505516 }
506517 setOperandLabel (destSort , sourceSort , labelName , mCode , parsedLine , IC , f -> operands );
@@ -515,11 +526,11 @@ void setCode(machineCode *mCode, long *IC, func *f, char **parsedLine, char *lab
515526 mCode [* IC ].word .code -> destSort = destSort ;
516527
517528 if (sourceSort == sort3 || sourceSort == sort2 ) {
518- sourceReg = regNumber (parsedLine [1 ], sourceSort );
529+ sourceReg = regNumber (parsedLine [1 ], sourceSort , errors );
519530 mCode [* IC ].word .code -> sourceReg = sourceReg ;
520531 }
521532 if (destSort == sort3 || destSort == sort2 ) {
522- destReg = regNumber (parsedLine [2 ], destSort );
533+ destReg = regNumber (parsedLine [2 ], destSort , errors );
523534 mCode [* IC ].word .code -> destReg = destReg ;
524535 }
525536 setOperandLabel (destSort , sourceSort , labelName , mCode , parsedLine , IC , f -> operands );
@@ -548,18 +559,32 @@ void errorHandler(int *errors, char *currLine) {
548559 checkMalloc (lineForErrorHandling );
549560 stringCopy (lineForErrorHandling , currLine );
550561
551- while (lineForErrorHandling [i ] != '\0' ){
552- if (lineForErrorHandling [i ] == ',' ){
553- if (isComma == TRUE){
562+ while (lineForErrorHandling [i ] != '\0' ) {
563+ if (lineForErrorHandling [i ] == ',' ) {
564+ if (isComma == TRUE) {
554565 * errors += 1 ;
555- printf ("--->Too many commas in 1 line\n" );
566+ printf ("%d --->Too many commas in 1 line\n" , lineNum );
556567 }
557568 isComma = TRUE;
558569 }
559570 i ++ ;
560571 }
561- i = 0 ;
562-
572+ if (strstr (lineForErrorHandling , ".string" )) {
573+ i = 0 ;
574+ isComma = FALSE;
575+ while (lineForErrorHandling [i ] != '\0' ) {
576+ if (lineForErrorHandling [i ] == '"' && isComma == FALSE) {
577+ isComma = TRUE;
578+ i ++ ;
579+ continue ;
580+ }
581+ if (isComma == TRUE && lineForErrorHandling [i ] == '"' ){
582+ return ;
583+ }
584+ i ++ ;
585+ }
586+ printf ("%d --->String not declared properly\n" , lineNum );
587+ }
563588}
564589
565590/**
0 commit comments