-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_opcode.c
64 lines (59 loc) · 1.35 KB
/
get_opcode.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
63
64
#include "monty.h"
/**
* _isnumber - checks leading character of operand read
* @s: argument read
* Return: 1 if true else 0
*
*/
int _isnumber(char *s)
{
int i;
for (i = 0; s[i]; i++)
{
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] == '+') || (s[i] == '-')))
return (0);
}
return (1);
}
/**
* get_opcode - gets and executes opcode
* @head: pointer to list head
* @line_number: command line number
*/
void get_opcode(stack_t **head, unsigned int line_number)
{
int l = 0;
instruction_t opcodes[] = {
{"nop", nop}, {"pall", pall},
{"pint", pint}, {"swap", swap},
{"pop", pop}, {"add", add}, {"pstr", pstr},
{"sub", sub}, {"div", _div}, {"pchar", pchar},
{"mul", mul}, {"mod", mod},
{"rotl", rotl}, {"rotr", rotr}, {"stack", stack}};
while (l < 15)
{
if (strcmp((const char *)opcode_read[0], "push") == 0)
{
if (!opcode_read[1])
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_grid(opcode_read);
free_list(*head);
exit(EXIT_FAILURE);
}
push(head, opcode_read[1], line_number);
return;
}
if (strcmp((const char *)opcode_read[0],
(const char *)opcodes[l].opcode) == 0)
{
opcodes[l].f(head, line_number);
return;
}
l++;
}
fprintf(stderr, "L%d: unknown instruction %s\n", line_number, opcode_read[0]);
free_list(*head);
free_grid(opcode_read);
exit(EXIT_FAILURE);
}