-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_2.c
151 lines (147 loc) · 3 KB
/
main_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "main.h"
#define MAX_LINE_LENGTH 256
/**
* main - Entry point for the test program.
*
* @argc: Number of command-line arguments.
* @argv: Array of command-line argument strings.
*
* Return: Always 0.
*/
int main(int argc, char const *argv[])
{
int checkIntger, lineNumber = 0;
char *nonWhiteSpace, *opcode, *argument, line[MAX_LINE_LENGTH];
char *lineToken, *fileBuffer = NULL;
FILE *montyFile;
stack_t *stack = NULL;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
montyFile = openfiles(argv[1]);
if (montyFile == NULL)
{
exit(EXIT_FAILURE);
}
while (fgets(line, MAX_LINE_LENGTH, montyFile) != NULL)
{
lineNumber++;
nonWhiteSpace = line;
while (*nonWhiteSpace != '\0' && isspace(*nonWhiteSpace))
{
nonWhiteSpace++;
}
if (*nonWhiteSpace == '\0')
{
continue;
}
lineToken = strtok(nonWhiteSpace, " ");
if (lineToken == NULL)
{
fprintf(stderr, "Error: empty line at L%d\n", lineNumber);
exit(EXIT_FAILURE);
}
line[strcspn(line, "\n")] = '\0';
opcode = lineToken;
argument = strtok(NULL, " ");
if (strcmp(opcode, "push") == 0)
{
if (argument == NULL || *argument == '\0')
{
fprintf(stderr, "L%d: usage: push integer\n", lineNumber);
exit(EXIT_FAILURE);
}
checkIntger = isStringInteger(argument);
if (checkIntger == 0)
{
fprintf(stderr, "L%d: usage: push integer\n", lineNumber);
exit(EXIT_FAILURE);
}
push(&stack, atoi(argument));
}
else if (strcmp(opcode, "pall") == 0)
{
pall_func(&stack);
}
else if (strcmp(opcode, "pint") == 0)
{
pint(&stack, lineNumber);
}
else if (strcmp(opcode, "pop") == 0)
{
pop(&stack, lineNumber);
}
else if (strcmp(opcode, "swap") == 0)
{
swap(&stack, lineNumber);
}
else if (strcmp(opcode, "add") == 0)
{
add(&stack, lineNumber);
}
else if (strcmp(opcode, "nop") == 0)
{
nop(&stack, lineNumber);
}
else if (strcmp(opcode, "sub") == 0)
{
sub(&stack, lineNumber);
}
else if (strcmp(opcode, "div") == 0)
{
div_op(&stack, lineNumber);
}
else if (strcmp(opcode, "mul") == 0)
{
mul(&stack, lineNumber);
}
else if (strcmp(opcode, "mod") == 0)
{
mod(&stack, lineNumber);
}
else if (strcmp(opcode, "pchar") == 0)
{
pchar(&stack, lineNumber);
}
else if (strcmp(opcode, "pstr") == 0)
{
pstr(&stack, lineNumber);
}
else if (strcmp(opcode, "rotl") == 0)
{
rotl(&stack, lineNumber);
}
else if (strcmp(opcode, "rotr") == 0)
{
rotr(&stack, lineNumber);
}
else if (strcmp(opcode, "stack") == 0)
{
stack2(&stack, lineNumber);
}
else if (strcmp(opcode, "queue") == 0)
{
queue(&stack, lineNumber);
}
else if (strcmp(opcode, "#") == 0)
{
nop(&stack, lineNumber);
}
else
{
fprintf(stderr, "L%d: unknown instruction %s\n", lineNumber, opcode);
exit(EXIT_FAILURE);
}
}
fclose(montyFile);
free(fileBuffer);
/* Free memory */
free_stack(&stack);
return (0);
}