-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_handler.c
50 lines (45 loc) · 960 Bytes
/
get_handler.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
#include "monty.h"
/**
* get_handler - Returns the correct function to perform an operation.
* @s: The operator passed.
*
* Return: A pointer to the appropriate function.
*/
void (*get_handler(char *token))(stack_t**, unsigned int)
{
int i = 0;
instruction_t ops_stack[] = {
{"push", push},
{"pall", pall},
{"pint", pint},
{"pchar", pchar},
{"pstr", pstr},
{"pop", pop},
{"swap", swap},
{"add", add},
{"sub", sub},
{"div", _div},
{"mul", mul},
{"mod", mod},
{"nop", nop},
{"rotl", rotl},
{"rotr", rotr},
{"queue", queue_handler},
{"stack", stack_handler},
{"#", nop},
{NULL, NULL},
};
strip_newline(token);
while (ops_stack[i].opcode != NULL)
{
if (strncmp(token, ops_stack[i].opcode, strlen(ops_stack[i].opcode)) == 0)
{
if (token[strlen(ops_stack[i].opcode)] == '\0')
return (ops_stack[i].f);
else if (token[0] == '#')
return (ops_stack[i].f);
}
i++;
}
return (ops_stack[i].f);
}