-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcode_instructions.c
136 lines (118 loc) · 2.25 KB
/
opcode_instructions.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
#include "monty.h"
/**
* _push - pushes an element to the stack
*
* @doubly: head of the linked list
* @cline: line number
* Return: no return
*/
void _push(stack_t **doubly, unsigned int cline)
{
int n, j;
if (!vglo.arg)
{
dprintf(2, "L%u: ", cline);
dprintf(2, "usage: push integer\n");
free_vglo();
exit(EXIT_FAILURE);
}
for (j = 0; vglo.arg[j] != '\0'; j++)
{
if (!isdigit(vglo.arg[j]) && vglo.arg[j] != '-')
{
dprintf(2, "L%u: ", cline);
dprintf(2, "usage: push integer\n");
free_vglo();
exit(EXIT_FAILURE);
}
}
n = atoi(vglo.arg);
if (vglo.lifo == 1)
add_dnodeint(doubly, n);
else
add_dnodeint_end(doubly, n);
}
/**
* _pall - prints all values on the stack
*
* @doubly: head of the linked list
* @cline: line numbers
* Return: no return
*/
void _pall(stack_t **doubly, unsigned int cline)
{
stack_t *aux;
(void)cline;
aux = *doubly;
while (aux)
{
printf("%d\n", aux->n);
aux = aux->next;
}
}
/**
* _pint - prints the value at the top of the stack
*
* @doubly: head of the linked list
* @cline: line number
* Return: no return
*/
void _pint(stack_t **doubly, unsigned int cline)
{
(void)cline;
if (*doubly == NULL)
{
dprintf(2, "L%u: ", cline);
dprintf(2, "can't pint, stack empty\n");
free_vglo();
exit(EXIT_FAILURE);
}
printf("%d\n", (*doubly)->n);
}
/**
* _pop - removes the top element of the stack
*
* @doubly: head of the linked list
* @cline: line number
* Return: no return
*/
void _pop(stack_t **doubly, unsigned int cline)
{
stack_t *aux;
if (doubly == NULL || *doubly == NULL)
{
dprintf(2, "L%u: can't pop an empty stack\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
aux = *doubly;
*doubly = (*doubly)->next;
free(aux);
}
/**
* _swap - swaps the top two elements of the stack
*
* @doubly: head of the linked list
* @cline: line number
* Return: no return
*/
void _swap(stack_t **doubly, unsigned int cline)
{
int m = 0;
stack_t *aux = NULL;
aux = *doubly;
for (; aux != NULL; aux = aux->next, m++)
;
if (m < 2)
{
dprintf(2, "L%u: can't swap, stack too short\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
aux = *doubly;
*doubly = (*doubly)->next;
aux->next = (*doubly)->next;
aux->prev = *doubly;
(*doubly)->next = aux;
(*doubly)->prev = NULL;
}