Skip to content

Commit 4ca0e31

Browse files
committed
more errors handling
Signed-off-by: OpenUniProjects <cryos10@gmail.com>
1 parent 6a885d4 commit 4ca0e31

File tree

11 files changed

+82
-32
lines changed

11 files changed

+82
-32
lines changed

.github/workflows/CI.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ jobs:
2626
cmp tester/ps.ob tester/main_tester
2727
- uses: actions/upload-artifact@v3
2828
with:
29-
name: assembler
30-
path: assembler
29+
name: assembler.zip
30+
path: |
31+
misc
32+
passes
33+
tester
34+
assembler
35+
assembler.c
36+
Makefile
3137
3238
build_and_test_18_04:
3339
runs-on: ubuntu-18.04

assembler.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include "passes/pre_assembler.h"
44
#include "passes/first_pass.h"
55

6+
/**
7+
* main wrapper function that parses the user inputs and executes the runs
8+
* @param argc
9+
* @param argv
10+
*/
611
void mainRunner(int argc, char **argv) {
712
int errors = 0;
813
int inputFileCounter = 1;
@@ -16,6 +21,7 @@ void mainRunner(int argc, char **argv) {
1621
printf("Received %d files, starting assembler process.\n\n", argc-1);
1722
while (inputFileCounter < argc) { /* iterate over files from argv .as */
1823
errors = 0;
24+
lineNum = 1;
1925
memset(line, '0', 4);
2026
if ((inp = inputFileInit(argv, inp, &inputFileCounter)) == NULL) {
2127
printError("file not found, skipping to next");
@@ -32,6 +38,7 @@ void mainRunner(int argc, char **argv) {
3238
printf("===>>>>>> Pre assembler finished: Errors: %d\n", errors);
3339
if (errors == 0) {
3440
fclose(inp);
41+
lineNum = 1;
3542
firstPass(line, outP, &errors, outPutFileName);
3643
} else {
3744
fclose(outP);

misc/definitions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define MAX_COMMANDS 8192
1111

1212
/**
13-
* declare boolean type TRUE = 1 FLASE = 0 using enum
13+
* declare boolean type TRUE = 1 FALSE = 0 using enum
1414
*/
1515
typedef int bool;
1616

misc/parsers.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "definitions.h"
22
#include "utils.h"
3-
#include "utils.h"
43

54
/**
65
* functions array with each func declaration
@@ -107,7 +106,7 @@ int getOperandsCount(char *cmd, int *errors) {
107106
}
108107
}
109108
*errors += 1;
110-
printf("--->Error: CMD: [ %s ] not found.\n", cmdName);
109+
printf("%d --->Error: CMD: [ %s ] not found.\n", lineNum, cmdName);
111110
return -1;
112111
}
113112

@@ -170,13 +169,13 @@ char **chooseParser(char *input, int *errors) {
170169
}
171170
if (assertNumOfOperands(parsedLine, errors, numOfOperands) == FALSE) {
172171
swapLastCharIfNewLine(parsedLine[0]);
173-
printf("--->Wrong number of operands for command: %s\n", parsedLine[0]);
172+
printf("%d --->Wrong number of operands for command: %s\n", lineNum, parsedLine[0]);
174173
return NULL;
175174
}
176175
if (strtok(NULL, " ") != NULL) {
177176
swapLastCharIfNewLine(parsedLine[0]);
178177
*errors += 1;
179-
printf("--->Wrong number of operands for command: %s\n", parsedLine[0]);
178+
printf("%d --->Wrong number of operands for command: %s\n", lineNum, parsedLine[0]);
180179
return NULL;
181180
}
182181
return parsedLine;
@@ -202,7 +201,7 @@ void parseCmd(char **parsedLine, int *errors, char *cmd, machineCode *mCode, lon
202201
}
203202
}
204203
if (found == FALSE) {
205-
printf("--->Error: CMD: [ %s ] not found\n", cmd);
204+
printf("%d --->Error: CMD: [ %s ] not found\n", lineNum, cmd);
206205
*errors += 1;
207206
}
208207
setCode(mCode, IC, &functions[i], parsedLine, labelName, errors);
@@ -302,6 +301,10 @@ sortType getSortType(char *operand, int *errors) {
302301
if ((atoi(&operand[1]) >= 0 && atoi(&operand[1]) <= 9 && operand[2] == '\0') ||
303302
(atoi(&operand[1]) >= 10 && atoi(&operand[1]) <= 15 && operand[3] == '\0')) {
304303
return sort3;
304+
} else {
305+
*errors += 1;
306+
printf("%d --->reg: %s, wrong number\n", lineNum, operand);
307+
return unsorted;
305308
}
306309
}
307310
/*direct sort*/
@@ -311,7 +314,7 @@ sortType getSortType(char *operand, int *errors) {
311314
/*did not find appropriate sort*/
312315
else {
313316
*errors += 1;
314-
printf("--->Operand: %s, did not find matching sort type\n", operand);
317+
printf("%d --->Operand: %s, did not find matching sort type\n", lineNum, operand);
315318
return unsorted;
316319
}
317320
}

misc/utils.c

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
*/
343344
void 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
/**

misc/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _UTILS_
22
#define _UTILS_
33

4+
int lineNum;
45
/* utils */
56
void swapLastCharIfNewLine(char *string);
67

@@ -42,7 +43,7 @@ void setARE(int IC, machineCode *mCode, unsigned char A, unsigned char R, unsign
4243

4344
void setAdditionalLines(machineCode *mCode, long *IC, sortType sort, int *L, char *operand);
4445

45-
int regNumber(char *reg, sortType sort);
46+
int regNumber(char *reg, sortType sort, int *errors);
4647

4748
void setOperandLabel(sortType destSort, sortType sourceSort, const char *labelName,
4849
machineCode *mCode, char **parsedLine, const long *IC, int operands);

passes/first_pass.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int codeDataOrString(char *line, machineCode *mCode, long *DC, bool withLabel, c
4444
strtok(tempLine, "”");
4545
tempLine = strtok(NULL, "");
4646
if (tempLine == NULL) {
47-
printf("--->No characters after .string declaration\n");
47+
printf("%d --->No characters after .string declaration or string undeclared properly\n", lineNum-1);
4848
*errors += 1;
4949
return 0;
5050
}
@@ -85,7 +85,7 @@ int codeDataOrString(char *line, machineCode *mCode, long *DC, bool withLabel, c
8585
tempLine = strtok(tempLine, ".data");
8686
}
8787
if (tempLine == NULL) {
88-
printf("--->No data after .data declaration\n");
88+
printf("%d --->No data after .data declaration\n", lineNum);
8989
*errors += 1;
9090
return 0;
9191
}
@@ -163,7 +163,7 @@ bool labelAndDirectiveStep(char *line, symbol *head, long *IC, long *DC,
163163
if (assertLabelProperDeclaration(line) == FALSE) {
164164
*errors += 1;
165165
swapLastCharIfNewLine(line);
166-
printf("--->Undefined label: %s\n", line);
166+
printf("%d --->Undefined label: %s\n", lineNum, line);
167167
return FALSE;
168168
}
169169
}
@@ -280,16 +280,19 @@ bool firstPass(char *line, FILE *inp, int *errors, char *outPutFileName) {
280280
if (is == TRUE) {
281281
stringCopy(line, iterator(line, inp, errors));
282282
errorHandler(errors, line);
283+
lineNum++;
283284
continue;
284285
}
285286
if (checkIfEntryOrExtern(line) == 2) { /*extern*/
286287
externStep(line, head, errors, zero);
287288
stringCopy(line, iterator(line, inp, errors));
288289
errorHandler(errors, line);
290+
lineNum++;
289291
continue;
290292
} else if (checkIfEntryOrExtern(line) == 1) {
291293
stringCopy(line, iterator(line, inp, errors));
292294
errorHandler(errors, line);
295+
lineNum++;
293296
continue;
294297
} /*entry - doing nothing first pass*/
295298

@@ -305,6 +308,7 @@ bool firstPass(char *line, FILE *inp, int *errors, char *outPutFileName) {
305308
if (parsedLine == NULL) {
306309
stringCopy(line, iterator(line, inp, errors));
307310
errorHandler(errors, line);
311+
lineNum++;
308312
continue;
309313
}
310314
parseCmd(parsedLine, errors, tempLine, mCode, &IC, labelName);
@@ -314,6 +318,7 @@ bool firstPass(char *line, FILE *inp, int *errors, char *outPutFileName) {
314318
}
315319
stringCopy(line, iterator(line, inp, errors));
316320
errorHandler(errors, line);
321+
lineNum++;
317322
}
318323

319324
printf("===>>>>>> First pass finished: Errors: %d\n", *errors);

passes/pre_assembler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "../misc/definitions.h"
22
#include "../misc/utils.h"
3-
#include "../misc/parsers.h"
43

54
/**
65
* add macro to the macro's table during pre-assembler
@@ -74,10 +73,11 @@ void preAssembler(char *line, int *errors, FILE *inp, FILE *outP, macroTable *ta
7473
stringCopy(macroName, strtok(NULL, ":"));
7574
if (macroName[strlen(macroName) - 1] == '\n') macroName[strlen(macroName) - 1] = '\0';
7675
while ((fgets(line, 80, inp)) != NULL) { /* read from .as file and save macro data */
76+
lineNum++;
7777
if (*line == '\n') continue;
7878
if (strlen(line) > 80) {
7979
errors += 1;
80-
printf("--->line to long\n");
80+
printf("%d --->line to long\n", lineNum);
8181
continue;
8282
}
8383
if (strstr(line, "endm")) {

passes/second_pass.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void assertLabelsDeclaration(int *errors, machineCode *mCode, const long *IC, sy
388388
found = checkLabel(mCode[i].labelUsageSource, tempNode, found, errors);
389389
if (*errors > err) {
390390
err = *errors;
391-
printf("--->Usage of undeclared label found: %s, error\n", mCode[i].labelUsageSource);
391+
printf("--->Usage of undeclared label found: %s, error\n" , mCode[i].labelUsageSource);
392392
}
393393
tempNode = head;
394394
found = checkLabel(mCode[i].labelUsageDest, tempNode, found, errors);

ps4.as

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ jpr A
55
ABC DCA
66
mov #1, R23
77
clr 1
8-
clr REG
8+
clr REG
9+
.string "aaaaaa

0 commit comments

Comments
 (0)