-
Notifications
You must be signed in to change notification settings - Fork 0
/
supp_func.c
155 lines (143 loc) · 2.45 KB
/
supp_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "monty.h"
/**
* push_queue - push to the end of list
* @argument: int
*/
void push_queue(char *argument)
{
int data;
stack_t *new, *location;
if (!check_input(argument))
{
fprintf(stderr, "L%u: usage: push integer\n",
monty.line_number);
free_all();
exit(EXIT_FAILURE);
}
data = atoi(argument);
new = malloc(sizeof(stack_t));
if (!new)
{
fprintf(stderr, "Error: malloc failed\n");
free_all();
exit(EXIT_FAILURE);
}
location = monty.stack;
new->n = data;
new->next = NULL;
if (!location)
{
new->prev = NULL;
monty.stack = new;
return;
}
while (location->next)
{
location = location->next;
}
location->next = new;
new->prev = location;
}
/**
* check_input- check the int
*@str: the string we check
*
*Return: false til int
*
*/
bool check_input(char *str)
{
int i = 0;
if (!str)
{
return (false);
}
if (str[0] != '-' && !isdigit(str[0]))
{
return (false);
}
for (i = 1; str[i]; i++)
{
if (!isdigit(str[i]))
{
return (false);
}
}
return (true);
}
/**
* op_choose - distrubtion middleware to match and call func
* @stack: pointer to pointer of stack
* @opcode: opcode from parsed file
*/
void op_choose(stack_t **stack, char *opcode)
{
int i;
char *op;
instruction_t fncs[] = {
{"pall", pall},
{"pint", pint},
{"pop", pop},
{"swap", swap},
{"add", add},
{"nop", nop},
{"sub", sub},
{"div", _div},
{"mul", mul},
{"mod", mod},
{"pchar", pchar},
{"pstr", pstr},
{"rotl", rotl},
{"rotr", rotr},
{"stack", _stack},
{"queue", _queue},
{NULL, NULL}
};
op = strtok(opcode, "\n");
for (i = 0; fncs[i].opcode; i++)
{
if (strcmp(op, fncs[i].opcode) == 0)
{
fncs[i].f(stack, monty.line_number);
return;
}
}
if (strcmp(opcode, "push"))
{
fprintf(stderr, "L%u: ", monty.line_number);
fprintf(stderr, "unknown instruction %s\n", opcode);
}
else
fprintf(stderr, "L%u: usage: push integer\n", monty.line_number);
exit(EXIT_FAILURE);
}
/**
* push - add node to list
* @argument: int
*/
void push(char *argument)
{
int data;
stack_t *new;
if (!check_input(argument))
{
fprintf(stderr, "L%u: usage: push integer\n"
, monty.line_number);
free_all();
exit(EXIT_FAILURE);
}
data = atoi(argument);
new = malloc(sizeof(stack_t));
if (!new)
{
fprintf(stderr, "Error: malloc failed\n");
free_all();
exit(EXIT_FAILURE);
}
new->n = data;
new->next = monty.stack;
new->prev = NULL;
if (new->next)
new->next->prev = new;
monty.stack = new;
}