-
Notifications
You must be signed in to change notification settings - Fork 1
/
m_get_opcode.c
46 lines (43 loc) · 881 Bytes
/
m_get_opcode.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
#include "monty.h"
/**
* get_opcode - Get the opcode object
* @opcode:the code to execute
* @stack: the stack to push
* @line_number: the line of the m file
* Return: nothing
*/
int get_opcode(const char *opcode, stack_t **stack, unsigned int line_number)
{
int i = 0;
instruction_t opcode_list[] = {
{"pall", pall},
{"pint", pint},
{"pop", pop},
{"pint", pint},
{"swap", swap},
{"nop", nop},
{"add", add},
{"sub", sub},
{"mul", mul},
{"div", _div},
{"mod", mod},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotl},
{"rotr", rotr},
{"stack", stk},
{"queue", queue},
};
int size = sizeof(opcode_list) / sizeof(instruction_t);
while (i < size)
{
if (strcmp(opcode, opcode_list[i].opcode) == 0)
{
opcode_list[i].f(stack, line_number);
return (0);
}
i++;
}
fprintf(stderr, "L%d: unknown instruction %s\12", line_number, opcode);
exit(EXIT_FAILURE);
}