-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
191 lines (186 loc) · 3.35 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "monty.h"
store_t command;
/**
* createNode - check the code
* @data: first variable
* Return: void.
*/
stack_t *createNode(int data)
{
stack_t *node;
node = malloc(sizeof(stack_t));
if (node == NULL)
{
perror("Error: malloc failed");
exit(EXIT_FAILURE);
}
node->n = data;
node->next = NULL;
return (node);
}
/**
* free_dlistint - check the code
* @line: first variable
* Return: void.
*/
void slaughterLine(char *line)
{
command.data = strtok(line, " ");
if (strcmp(command.data, "push") == 0)
{
command.val = strtok(NULL, " ");
}
}
/**
* free_dlistint - check the code
* @head: first variable
* Return: void.
*/
void free_dlistint(stack_t *head)
{
stack_t *p;
while (head != NULL)
{
p = head;
head = head->prev;
free(p);
}
free(head);
}
/**
* checkNumber - check the code
* Return: void.
*/
int checkNumber()
{
int checkNumber;
int j;
j = 0;
while (command.val[j] != '\0')
{
if ((command.val[j] >= 48 && command.val[j] <= 57) || command.val[j] == '-')
{
checkNumber = 1;
j++;
}
else
{
checkNumber = 0;
break;
}
j++;
}
return (checkNumber);
}
/**
* Error - check the code
* @f: first variable
* @top: second
* @lineNumber: third
* @errorStr: forth
* Return: void.
*/
void Error(FILE *f, stack_t *top, int lineNumber, char *errorStr)
{
fprintf(stderr, "L%u: %s", lineNumber, errorStr);
free_dlistint(top);
fclose(f);
exit(EXIT_FAILURE);
}
/**
* main - check the code
* @f: first variable
* @Dictionnary: Second variable
* Return: void.
*/
void readLine(FILE *f, instruction_t *Dictionnary)
{
stack_t *top = NULL;
char line[100], *noSpace;
unsigned int lineNumber = 0;
int checkNum, i = 0;
while (fgets(line, sizeof(line), f))
{
command.val = NULL;
lineNumber++;
line[strcspn(line, "\n")] = '\0';
noSpace = line;
while (*noSpace == ' ')
noSpace++;
if (*noSpace != '\0' && *noSpace != '#')
{
slaughterLine(noSpace);
i = 0;
while (Dictionnary[i].opcode != NULL)
{
if (strcmp(command.data, Dictionnary[i].opcode) == 0)
{
if (strcmp(command.data, "push") == 0)
{
if (command.val == NULL)
Error(f, top, lineNumber, "usage: push integer");
checkNum = checkNumber();
if (checkNum == 1)
{
command.number = atoi(command.val);
Dictionnary[i].f(&top, lineNumber);
break;
}
else
Error(f, top, lineNumber, "usage: push integer");
}
else
{
Dictionnary[i].f(&top, lineNumber);
break;
}
}
i++;
}
}
if (Dictionnary[i].opcode == NULL)
{
fprintf(stderr, "L%d: unknown instruction %s\n", lineNumber, command.data);
free_dlistint(top);
fclose(f);
exit(EXIT_FAILURE);
}
}
free_dlistint(top);
}
/**
* main - check the code
* @argc: first variable
* @argv: Second variable
* Return: void.
*/
int main(int argc, char *argv[])
{
instruction_t Dictionnary[] =
{
{"push", pushNode},
{"pall", pallStack},
{"pint", pintStack},
{"pop", popStack},
{"swap", swapStack},
{"add", addStack},
{"nop", nopStack},
{"sub", subStack},
{"div", divStack},
{NULL, NULL}};
FILE *f;
if (argc != 2)
{
perror("USAGE: monty file");
exit(EXIT_FAILURE);
}
f = fopen(argv[1], "r");
if (f == NULL)
{
fprintf(stderr, "Error: Can't open file '%s': ", argv[1]);
exit(EXIT_FAILURE);
}
readLine(f, Dictionnary);
fclose(f);
return (0);
}