forked from GraceBuri/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
45 lines (42 loc) · 1.02 KB
/
exec.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"
/**
* execute - function that runs the opcode
* @stack: pointer to the stack
* @tokens: split arguments of the line
* @line_number: bytecode is executed
*/
void execute(char **tokens, stack_t **stack, unsigned int line_number)
{
instruction_t ops[] = {
{"push", push}, {"pall", pall},
{"pint", pint}, {"pop", pop},
{"swap", swap}, {"add", add},
{"sub", sub}, {"mul", mul},
{"div", divs}, {"mod", mod},
{"nop", nop}, {"pchar", pchar},
{"pstr", pstr}, {"rotl", rotl},
{"rotr", rotr}, {"stack", fstack},
{"queue", queue}, {NULL, NULL}
};
int i = 0;
if (input.num_tok == 2)
input.arg = tokens[1];
if (tokens && tokens[0][0] == '#')
return;
if (tokens[0] == NULL)
{
fprintf(stderr, "L%u: unknown instruction %s\n", line_number, tokens[0]);
exit(EXIT_FAILURE);
}
while (ops[i].opcode != NULL)
{
if (strcmp(tokens[0], ops[i].opcode) == 0)
{
ops[i].f(stack, line_number);
return;
}
i++;
}
fprintf(stderr, "L%u: unknown instruction %s\n", line_number, tokens[0]);
exit(EXIT_FAILURE);
}