forked from fatimasgit/holbertonschool-monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_ops.c
55 lines (54 loc) · 960 Bytes
/
_ops.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
#include "monty.h"
/**
* _ops - compare functions to tokens and executes them
* @token: command recieved
* @stk: stack
* @linenum: number of line
* Return: void
*/
void _ops(char *token, stack_t **stk, unsigned int linenum)
{
int a = 0;
instruction_t op[] = {
{"push", push},
{"pall", pall},
{"pint", pint},
{"pop", pop},
{"add", add},
{"swap", swap},
{"nop", nop},
{NULL, NULL}
};
if (strcmp(token, "stack") == 0)
{
variables.check = 0;
return;
}
if (strcmp(token, "queue") == 0)
{
variables.check = 1;
return;
}
if (strcmp(token, "swap") == 0)
{
swap(stk, linenum);
return;
}
if (variables.check == 1 && strcmp(token, "push") == 0)
{
_queue(stk, linenum);
return;
}
while (op[a].opcode != NULL)
{
if (strcmp(token, op[a].opcode) == 0)
{
op[a].f(stk, linenum);
return;
}
a++;
}
dprintf(2, "L%d: unknown instruction %s\n", linenum, token);
free_stk(stk, linenum);
exit(EXIT_FAILURE);
}