-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoose_func.c
84 lines (71 loc) · 1.69 KB
/
choose_func.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "monty.h"
/**
* pick_func - Picks a function based on the given string.
* @s: The string used to determine the function to be picked.
* Return: Pointer to a function that takes stack_t**
* and unsigned int as arguments,
* or NULL if no matching function is found.
*/
void (*pick_func(char *s))(stack_t **, unsigned int)
{
int i = 0;
instruction_t insts[] = {
{ "push", push_func},
{ "pall", pall},
{ "pint", pint_func},
{ "pop", pop_func},
{ "swap", swap_func},
{ "add", add_func},
{ "sub", sub_func},
{ "div", div_func},
{ "nop", nop_func},
{ "mul", mul_func},
{ "mod", mod_func},
{ "pchar", pchar_func},
{ "pstr", pstr_func},
{ "rotl", rotl_func},
{ "rotr", rotr_func},
{ NULL, NULL }
};
while (insts[i].opcode)
{
if (strcmp(s, insts[i].opcode) == 0)
return (insts[i].f);
++i;
}
return (NULL);
}
/**
* push_func - Pushes an element onto the stack.
* @stack: Double pointer to the head of the stack.
* @line_number: The line number where the function is called.
* Return: Nothing
*/
void push_func(stack_t **stack, unsigned int line_number)
{
char *value = strtok(NULL, "\t\n ");
int n;
if (!value)
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
if (!isdigit(value[0]) && value[0] != '-')
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
if (value == NULL || !is_integer(value))
{
fprintf(stderr, "L%d: usage: push integer\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
n = atoi(value);
if (mode == STACK_MODE)
add_dnodeint(stack, n);
else if (mode == QUEUE_MODE)
add_dnodeint_end(stack, n);
}