-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0-3_opcodes.c
122 lines (107 loc) · 2.59 KB
/
0-3_opcodes.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
#include "monty.h"
/* global variable <essential> make available in this file */
trackg_t essential;
/**
* push_opcode - pushes an element to the stack
*
* @stack: pointer to the pointer to the top most element/node
* of the stack
* @line_number: line number of the executing opcode
*
* Return: nothing
*/
void push_opcode(stack_t **stack, unsigned int line_number)
{
int verdict;
(void)line_number;
if (essential.mode == true)
verdict = stack_push(stack, essential.data);
else
verdict = en_queue(&essential.st_bottom, essential.data);
if (!verdict)
return;
fprintf(stderr, "Error: malloc failed\n");
fclose(essential.fp);
stack_free_all(*stack);
exit(EXIT_FAILURE);
}
/**
* pall_opcode - prints all the values on the stack, starting
* from the top of the stack
*
* @stack: pointer to the pointer to the top most element/node
* of the stack
* @line_number: line number of the executing opcode
*
* Return: nothing
*/
void pall_opcode(stack_t **stack, unsigned int line_number)
{
(void)line_number;
if (stack_is_empty(stack))
return;
stack_display(*stack);
}
/**
* pint_opcode - prints the value at the top of the stack,
* followed by a new line
*
* @stack: pointer to the pointer to the top most element/node
* of the stack
* @line_number: line number of the executing opcode
*
* Return: nothing
*/
void pint_opcode(stack_t **stack, unsigned int line_number)
{
if (stack_is_empty(stack))
{
fprintf(stderr, "L%u: can't pint, stack empty\n", line_number);
fclose(essential.fp);
exit(EXIT_FAILURE);
}
printf("%d\n", stack_top(stack));
}
/**
* pop_opcode - removes the top element of the stack
*
* @stack: pointer to the pointer to the top most element/node
* of the stack
* @line_number: line number of the executing opcode
*
* Return: nothing
*/
void pop_opcode(stack_t **stack, unsigned int line_number)
{
if (stack_is_empty(stack))
{
fprintf(stderr, "L%u: can't pop an empty stack\n", line_number);
fclose(essential.fp);
exit(EXIT_FAILURE);
}
stack_pop(stack);
}
/**
* swap_opcode - swaps the top two elements of the stack
*
* @stack: pointer to the pointer to the top most element/node
* of the stack
* @line_number: line number of the executing opcode
*
* Return: nothing
*/
void swap_opcode(stack_t **stack, unsigned int line_number)
{
int temp;
if (essential.stack_size < 2)
{
fprintf(stderr, "L%u: can't swap, stack too short\n", line_number);
fclose(essential.fp);
if (essential.stack_size == 1)
stack_free_all(*stack);
exit(EXIT_FAILURE);
}
temp = (*stack)->n;
(*stack)->n = (*stack)->prev->n;
(*stack)->prev->n = temp;
}