-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector.c
45 lines (42 loc) · 973 Bytes
/
selector.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
#include "monty.h"
/**
* op_selector - selects function of opcode related to the line of monty.
* @l: number of line in the monty code.
* @top: pointer of the top of the stack.
*
* Return: pointer to functions related to command.
*/
void (*op_selector(int l, stack_t **top))(stack_t **top, unsigned int line_n)
{
int i = 0;
instruction_t list[] = {
{"push", op_push},
{"pall", op_pall},
{"pint", op_pint},
{"pop", op_pop},
{"swap", op_swap},
{"add", op_add},
{"nop", op_nop},
{"sub", op_sub},
{"div", op_div},
{"mul", op_mul},
{"mod", op_mod},
{"pchar", op_pchar},
{"pstr", op_pstr},
{"rotl", op_rotl},
{"rotr", op_rotr},
{NULL, NULL}
};
for (i = 0; list[i].opcode; i++)
{
if (strcmp(global_var.command_array[0], list[i].opcode) == SAME)
{
return (list[i].f);
}
}
fprintf(stderr, "L%d: unknown instruction %s\n",
l, global_var.command_array[0]);
free_stack(top);
fclose(global_var.fp);
exit(EXIT_FAILURE);
}