-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
43 lines (34 loc) · 908 Bytes
/
execute.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
#include "monty.h"
/**
* execute - Execute opcode
*
* Return: 0 if sucessful, 1 if fail
*/
int execute(void)
{
struct instruction_s current_command;
int num_of_cmd;
int i;
char *cmd_str[] = {"push", "pall", "pint", "pop", "swap", "add",
"nop", "sub", "div", "mul", "mod"};
void (*cmd[])(stack_t **, unsigned int) = {&_push, &_pall, &_pint, &_pop,
&_swap, &_add, &_nop, &_sub, &_div, &_mul, &_mod};
/*Determine the number of commands*/
num_of_cmd = (sizeof(cmd_str) / sizeof(cmd_str[0]));
/*Match and execute the right function*/
for (i = 0; i < num_of_cmd; i++)
{
if (strcmp(gv->instruction[0], cmd_str[i]) == 0)
{
current_command.f = cmd[i];
current_command.f(&(gv->head), gv->line_number);
free(gv->instruction);
return (0);
}
}
fprintf(stderr, "L%d: unknown instruction %s\n", gv->line_number,
gv->instruction[0]);
kill(NULL);
/*Failure*/
return (1);
}