-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_cmd.c
62 lines (57 loc) · 1.15 KB
/
run_cmd.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
51
52
53
54
55
56
57
58
59
60
61
62
#include "monty.h"
/**
* get_cmd - return the requisite opcode to run
* Return: pointer to the function that executes the opcode
*/
void (*get_cmd(void))(stack_t **head, unsigned int line_number)
{
int i;
instruction_t cmds[] = {
{"push", pusher},
{"pall", paller},
{"pint", pinter},
{"pop", popper},
{"nop", nopper},
{"swap", swapper},
{"add", adder},
{"sub", subber},
{"div", divider},
{"mul", multpler},
{"mod", modder},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotl},
{"rotr", rotr},
{"stack", changemode},
{"queue", changemode},
{NULL, NULL}
};
for (i = 0; cmds[i].opcode; i++)
{
if (strcmp(cmds[i].opcode, var.cmd) == 0)
break;
}
return (cmds[i].f);
}
/**
* run_cmd - run the command
* @bufline: line read from bytecode
* Return: no return
*/
void run_cmd(char *bufline)
{
void (*f)(stack_t **head, unsigned int line_number);
var.cmd = strtok(bufline, DELIM);
if (var.cmd && var.cmd[0] != '#')
{
f = get_cmd();
if (f)
{
if (strcmp(var.cmd, "push") == 0)
var.value = strtok(NULL, DELIM);
f(&(var.head), var.line_number);
}
else
erro(3, var.line_number, var.cmd), free_stack();
}
}