forked from FearGod12/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_command.c
44 lines (42 loc) · 952 Bytes
/
get_command.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
#include "monty.h"
/**
* get_command - matches a token to a command
* @token: token to be matched
* @head: pointer to the top of the stack
* @line_number: lune number
*/
void get_command(char *token, stack_t **head, unsigned int line_number)
{
instruction_t cmd[] = {
{"push", push_func},
{"pall", pall_func},
{"pint", pint_func},
{"pop", pop_func},
{"swap", swap_func},
{"add", add_func},
{"nop", nop_func},
{"sub", sub_func},
{"div", div_func},
{"mul", mul_func},
{"mod", mod_func},
{"pchar", pchar_func},
{"pstr", pstr_func},
{"rotl", rotl_func},
{"rotr", rotr_func},
{"stack", change_mode_func},
{"queue", change_mode_func},
{NULL, NULL}
};
int index = 0;
while (cmd[index].opcode != NULL)
{
if (strcmp(token, cmd[index].opcode) == 0)
{
cmd[index].f(head, line_number);
return;
}
index++;
}
fprintf(stderr, "L%d: unknown instruction %s\n", line_number, token);
exit(EXIT_FAILURE);
}