-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_func.c
executable file
·97 lines (89 loc) · 1.98 KB
/
get_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
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "monty.h"
#include "lists.h"
#include <stdio.h>
#include <stdlib.h>
/**
* get_func - selects the right function
* @parsed: line from the bytecode file passed to main
*
* Return: pointer to the selected function, or NULL on failure
*/
void (*get_func(char **parsed))(stack_t **, unsigned int)
{
instruction_t func_arr[] = {
{"push", push_handler},
{"pall", pall_handler},
{"pint", pint_handler},
{"pop", pop_handler},
{"swap", swap_handler},
{"add", add_handler},
{"nop", nop_handler},
{"sub", sub_handler},
{"div", div_handler},
{"mul", mul_handler},
{"mod", mod_handler},
{"pchar", pchar_handler},
{"pstr", pstr_handler},
{"rotl", rotl_handler},
{"rotr", rotr_handler},
{"stack", stack_handler},
{"queue", queue_handler},
{NULL, NULL}
};
int codes = 17, i;
for (i = 0; i < codes; i++)
{
if (strcmp(func_arr[i].opcode, parsed[0]) == 0)
{
return (func_arr[i].f);
}
}
return (NULL);
}
/**
* push_handler - handles the push instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void push_handler(stack_t **stack, unsigned int line_number)
{
stack_t *new;
int num = 0, i;
if (data.words[1] == NULL)
{
dprintf(STDERR_FILENO, PUSH_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
for (i = 0; data.words[1][i]; i++)
{
if (isalpha(data.words[1][i]) != 0)
{
dprintf(STDERR_FILENO, PUSH_FAIL, line_number);
free_all(1);
exit(EXIT_FAILURE);
}
}
num = atoi(data.words[1]);
if (data.qflag == 0)
new = add_dnodeint(stack, num);
else if (data.qflag == 1)
new = add_dnodeint_end(stack, num);
if (!new)
{
dprintf(STDERR_FILENO, MALLOC_FAIL);
free_all(1);
exit(EXIT_FAILURE);
}
}
/**
* pall_handler - handles the pall instruction
* @stack: double pointer to the stack to push to
* @line_number: number of the line in the file
*/
void pall_handler(stack_t **stack, unsigned int line_number)
{
(void)line_number;
if (*stack)
print_dlistint(*stack);
}